Open Source Tomb Raider Engine
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Simulation.h 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /* -*- Mode: C++; tab-width: 3; indent-tabs-mode: t; c-basic-offset: 3 -*- */
  2. /*================================================================
  3. *
  4. * Project : Hel
  5. * Author : Terry 'Mongoose' Hendrix II
  6. * Website : http://www.westga.edu/~stu7440/
  7. * Email : stu7440@westga.edu
  8. * Object : Simulation
  9. * License : No use w/o permission (C) 2003 Mongoose
  10. * Comments: This class simulates mass physics
  11. *
  12. *
  13. * This file was generated using Mongoose's C++
  14. * template generator script. <stu7440@westga.edu>
  15. *
  16. *-- Test Defines -----------------------------------------------
  17. *
  18. * UNIT_TEST_SIMULATION - Builds Simulation class as a console unit test
  19. *
  20. *-- History ------------------------------------------------
  21. *
  22. * 2003.06.08:
  23. * Mongoose - Generic collision and basic resolution w/o generic
  24. * Hel Resolvers
  25. *
  26. * 2003.06.02:
  27. * Mongoose - Created, based on algorithms from Erkin Tunca
  28. ================================================================*/
  29. #ifndef GUARD__HEL_MONGOOSE_SIMULATION_H_
  30. #define GUARD__HEL_MONGOOSE_SIMULATION_H_
  31. #include <mstl/Vector.h>
  32. #include <hel/CollisionObject.h>
  33. #include <hel/Mass.h>
  34. #include <hel/math.h>
  35. class Simulation
  36. {
  37. public:
  38. ////////////////////////////////////////////////////////////
  39. // Constructors
  40. ////////////////////////////////////////////////////////////
  41. Simulation();
  42. /*------------------------------------------------------
  43. * Pre :
  44. * Post : Constructs an object of Simulation
  45. *
  46. *-- History ------------------------------------------
  47. *
  48. * 2003.06.02:
  49. * Mongoose - Created
  50. ------------------------------------------------------*/
  51. virtual ~Simulation();
  52. /*------------------------------------------------------
  53. * Pre : Simulation object is allocated
  54. * Post : Deconstructs an object of Simulation
  55. *
  56. *-- History ------------------------------------------
  57. *
  58. * 2003.06.02:
  59. * Mongoose - Created
  60. ------------------------------------------------------*/
  61. ////////////////////////////////////////////////////////////
  62. // Public Accessors
  63. ////////////////////////////////////////////////////////////
  64. Mass *getMass(unsigned int i);
  65. /*------------------------------------------------------
  66. * Pre :
  67. * Post : Get <i>th Mass element of this simulation
  68. *
  69. *-- History ------------------------------------------
  70. *
  71. * 2003.06.02:
  72. * Mongoose - Created
  73. ------------------------------------------------------*/
  74. ////////////////////////////////////////////////////////////
  75. // Public Mutators
  76. ////////////////////////////////////////////////////////////
  77. virtual void attachCollisionObject(CollisionObject *obj);
  78. /*------------------------------------------------------
  79. * Pre :
  80. * Post : Adds <Obj> to the collision list for this simulation
  81. *
  82. * TODO : Allow multiple types of collision to be excluded?
  83. *
  84. *-- History ------------------------------------------
  85. *
  86. * 2003.06.08:
  87. * Mongoose - Created
  88. ------------------------------------------------------*/
  89. virtual void clear();
  90. /*------------------------------------------------------
  91. * Pre :
  92. * Post : Removes all masses from this simulation
  93. *
  94. *-- History ------------------------------------------
  95. *
  96. * 2003.06.02:
  97. * Mongoose - Created
  98. ------------------------------------------------------*/
  99. virtual bool collisionDetected(Mass *mass);
  100. /*------------------------------------------------------
  101. * Pre :
  102. * Post : Returns true if <Mass> collided with object
  103. *
  104. *-- History ------------------------------------------
  105. *
  106. * 2003.06.08:
  107. * Mongoose - Created
  108. ------------------------------------------------------*/
  109. virtual void generateMasses(unsigned int count, vec_t mass);
  110. /*------------------------------------------------------
  111. * Pre :
  112. * Post : <Count> number of masses with <mass> value are added
  113. *
  114. *-- History ------------------------------------------
  115. *
  116. * 2003.06.02:
  117. * Mongoose - Created
  118. ------------------------------------------------------*/
  119. virtual void generateMasses(unsigned int count, vec_t mass,
  120. const Vector3d &position,
  121. const Vector3d &velocity);
  122. /*------------------------------------------------------
  123. * Pre :
  124. * Post : <Count> number of masses with
  125. * <mass>, <position>, and <velocity> are added
  126. *
  127. *-- History ------------------------------------------
  128. *
  129. * 2003.06.02:
  130. * Mongoose - Created
  131. ------------------------------------------------------*/
  132. virtual void init();
  133. /*------------------------------------------------------
  134. * Pre :
  135. * Post : Reset forces acting on masses
  136. *
  137. *-- History ------------------------------------------
  138. *
  139. * 2003.06.02:
  140. * Mongoose - Created
  141. ------------------------------------------------------*/
  142. virtual void resolveCollision(Mass *mass, CollisionObject *obj);
  143. /*------------------------------------------------------
  144. * Pre :
  145. * Post : Resolves a collision of <Obj> and <Mass>
  146. *
  147. * The default resolution here is to set mass pos
  148. * to intersection point with the object, there
  149. * is no affect on velocity
  150. *
  151. *-- History ------------------------------------------
  152. *
  153. * 2003.06.08:
  154. * Mongoose - Created
  155. ------------------------------------------------------*/
  156. virtual void run(vec_t timeDelta);
  157. /*------------------------------------------------------
  158. * Pre :
  159. * Post : Does full init, solve, simulate cycle
  160. *
  161. *-- History ------------------------------------------
  162. *
  163. * 2003.06.02:
  164. * Mongoose - Created
  165. ------------------------------------------------------*/
  166. virtual void simulate(vec_t timeDelta);
  167. /*------------------------------------------------------
  168. * Pre :
  169. * Post : Update change in time for each mass
  170. *
  171. *-- History ------------------------------------------
  172. *
  173. * 2003.06.02:
  174. * Mongoose - Created
  175. ------------------------------------------------------*/
  176. virtual void solve ();
  177. /*------------------------------------------------------
  178. * Pre :
  179. * Post : Have forces act on masses
  180. *
  181. *-- History ------------------------------------------
  182. *
  183. * 2003.06.02:
  184. * Mongoose - Created
  185. ------------------------------------------------------*/
  186. Vector<Mass *> mMasses; /* It's a collection of masses, silly */
  187. Vector<CollisionObject *> mObjects; /* Object(s) to test collision with */
  188. unsigned int mMassCount; /* Cache of number of masses in collection */
  189. private:
  190. ////////////////////////////////////////////////////////////
  191. // Private Accessors
  192. ////////////////////////////////////////////////////////////
  193. ////////////////////////////////////////////////////////////
  194. // Private Mutators
  195. ////////////////////////////////////////////////////////////
  196. };
  197. class GravitySimulation : public Simulation
  198. {
  199. public:
  200. GravitySimulation() : Simulation()
  201. {
  202. gravitation = Vector3d(0.0f, -9.81f, 0.0f);
  203. }
  204. virtual void solve()
  205. {
  206. Mass *m;
  207. // Apply gravitational force to each mass
  208. for (mMasses.start(); mMasses.forward(); mMasses.next())
  209. {
  210. m = mMasses.current();
  211. // Gravitational force, Force = (Mass)(Gravity)
  212. m->applyForce(gravitation * m->mMass);
  213. if (collisionDetected(m))
  214. {
  215. m->mVelocity = Vector3d(0, 0, 0);
  216. }
  217. }
  218. }
  219. Vector3d gravitation; /* Gravitational acceleration */
  220. };
  221. class SpringSimulation : public Simulation
  222. {
  223. public:
  224. SpringSimulation() : Simulation()
  225. {
  226. }
  227. virtual void generateMasses(unsigned int count, vec_t mass,
  228. const Vector3d &position,
  229. const Vector3d &velocity)
  230. {
  231. Simulation::generateMasses(count, mass,
  232. connectionPos + position, velocity);
  233. }
  234. virtual void solve()
  235. {
  236. Mass *m;
  237. // Apply spring force to each mass
  238. for (mMasses.start(); mMasses.forward(); mMasses.next())
  239. {
  240. m = mMasses.current();
  241. // Vector from mass to spring connection
  242. Vector3d springVector = m->mPos - connectionPos;
  243. // Apply spring force, spring force formulation
  244. m->applyForce(-springVector * springConstant);
  245. if (collisionDetected(m))
  246. {
  247. //m->mVelocity = Vector3d(0, 0, 0);
  248. }
  249. }
  250. }
  251. vec_t springConstant; /* Stiffness, higher values more spring force */
  252. Vector3d connectionPos; /* Location where mass is connected to spring */
  253. };
  254. #endif