Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

419 rader
13 KiB

  1. //
  2. // LolPhysics
  3. //
  4. // Copyright: (c) 2009-2013 Benjamin "Touky" Huet <huet.benjamin@gmail.com>
  5. // (c) 2012 Sam Hocevar <sam@hocevar.net>
  6. //
  7. #if !defined __LOLPHYSICS_H__
  8. #define __LOLPHYSICS_H__
  9. #include <cstring>
  10. #include <bullet/btBulletDynamicsCommon.h>
  11. #include <bullet/btBulletCollisionCommon.h>
  12. #include <BulletDynamics/Character/btKinematicCharacterController.h>
  13. #include "lolbtphysicsintegration.h"
  14. #include "easyphysics.h"
  15. #include "easyconstraint.h"
  16. namespace lol
  17. {
  18. namespace phys
  19. {
  20. enum eRaycastType
  21. {
  22. ERT_Closest,
  23. ERT_AllHit,
  24. ERT_AnyHit, //Will stop at the first hit. Hit data are supposed to be irrelevant
  25. ERT_MAX
  26. };
  27. struct RayCastResult
  28. {
  29. RayCastResult(int CollisionFilterGroup=1, int CollisionFilterMask=(0xFF))
  30. {
  31. memset(this, 0, sizeof(RayCastResult));
  32. m_collision_filter_group = CollisionFilterGroup;
  33. m_collision_filter_mask = CollisionFilterMask;
  34. }
  35. void Reset()
  36. {
  37. m_collider_list.Empty();
  38. m_hit_normal_list.Empty();
  39. m_hit_point_list.Empty();
  40. m_hit_fraction_list.Empty();
  41. }
  42. Array<EasyPhysic*> m_collider_list;
  43. Array<vec3> m_hit_normal_list;
  44. Array<vec3> m_hit_point_list;
  45. Array<float> m_hit_fraction_list;
  46. short int m_collision_filter_group;
  47. short int m_collision_filter_mask;
  48. unsigned int m_flags; //???
  49. };
  50. class Simulation : public Entity
  51. {
  52. public:
  53. Simulation() :
  54. m_broadphase(0),
  55. m_collision_configuration(0),
  56. m_dispatcher(0),
  57. m_solver(0),
  58. m_dynamics_world(0),
  59. m_timestep(1.f/60.f)
  60. {
  61. m_gamegroup = GAMEGROUP_SIMULATION;
  62. }
  63. ~Simulation()
  64. {
  65. Exit();
  66. }
  67. char const *GetName() { return "<Simulation>"; }
  68. public:
  69. void Init()
  70. {
  71. // Build the broadphase
  72. if (1)
  73. {
  74. m_Sweep_broadphase = new btAxisSweep3(LOL2BT_VEC3(m_world_min), LOL2BT_VEC3(m_world_max));
  75. m_Sweep_broadphase->getOverlappingPairCache()->setInternalGhostPairCallback(new btGhostPairCallback());
  76. m_broadphase = m_Sweep_broadphase;
  77. }
  78. else
  79. m_broadphase = new btDbvtBroadphase();
  80. // Set up the collision configuration and dispatcher
  81. m_collision_configuration = new btDefaultCollisionConfiguration();
  82. m_dispatcher = new btCollisionDispatcher(m_collision_configuration);
  83. // The actual physics solver
  84. m_solver = new btSequentialImpulseConstraintSolver;
  85. // The world.
  86. m_dynamics_world = new btDiscreteDynamicsWorld(m_dispatcher, m_broadphase, m_solver, m_collision_configuration);
  87. }
  88. virtual void TickGame(float seconds)
  89. {
  90. Entity::TickGame(seconds);
  91. //step the simulation
  92. if (m_dynamics_world)
  93. {
  94. //the "+1" is to have at least one Timestep and to ensure float to int .5f conversion.
  95. int steps = (int)(seconds / m_timestep) + 1;
  96. m_dynamics_world->stepSimulation(seconds, steps, m_timestep);
  97. }
  98. }
  99. //Rip-Off of the btKinematicClosestNotMeRayResultCallback
  100. class ClosestNotMeRayResultCallback : public btCollisionWorld::ClosestRayResultCallback
  101. {
  102. public:
  103. ClosestNotMeRayResultCallback(btCollisionObject* Me, const btVector3& rayFromWorld, const btVector3& rayToWorld) :
  104. btCollisionWorld::ClosestRayResultCallback(rayFromWorld, rayToWorld)
  105. {
  106. m_me = Me;
  107. }
  108. virtual btScalar addSingleResult(btCollisionWorld::LocalRayResult& rayResult,bool normalInWorldSpace)
  109. {
  110. if (rayResult.m_collisionObject == m_me)
  111. return 1.0;
  112. return ClosestRayResultCallback::addSingleResult(rayResult, normalInWorldSpace);
  113. }
  114. protected:
  115. btCollisionObject* m_me;
  116. };
  117. //Will stop at the first hit. Hit data are supposed to be irrelevant
  118. class AnyHitRayResultCallback : public btCollisionWorld::ClosestRayResultCallback
  119. {
  120. public:
  121. AnyHitRayResultCallback(const btVector3& rayFromWorld, const btVector3& rayToWorld) :
  122. btCollisionWorld::ClosestRayResultCallback(rayFromWorld, rayToWorld)
  123. {
  124. }
  125. virtual btScalar addSingleResult(btCollisionWorld::LocalRayResult& rayResult,bool normalInWorldSpace)
  126. {
  127. return .0f;
  128. }
  129. };
  130. //Returns true when hitting something. If SourceCaster is set, it will be ignored by Raycast.
  131. bool RayHits(RayCastResult& HitResult, eRaycastType RaycastType, const vec3& RayFrom, const vec3& RayTo, EasyPhysic* SourceCaster=NULL)
  132. {
  133. bool bResult = false;
  134. btCollisionWorld::RayResultCallback* BtRayResult = NULL;
  135. btCollisionWorld::ClosestRayResultCallback* BtRayResult_Closest;
  136. btCollisionWorld::AllHitsRayResultCallback* BtRayResult_AllHits;
  137. switch (RaycastType)
  138. {
  139. case ERT_Closest:
  140. {
  141. if (SourceCaster)
  142. BtRayResult_Closest = new ClosestNotMeRayResultCallback(SourceCaster->m_collision_object, LOL2BTU_VEC3(RayFrom), LOL2BTU_VEC3(RayTo));
  143. else
  144. BtRayResult_Closest = new btCollisionWorld::ClosestRayResultCallback(LOL2BTU_VEC3(RayFrom), LOL2BTU_VEC3(RayTo));
  145. BtRayResult = BtRayResult_Closest;
  146. break;
  147. }
  148. case ERT_AllHit:
  149. {
  150. BtRayResult_AllHits = new btCollisionWorld::AllHitsRayResultCallback(LOL2BTU_VEC3(RayFrom), LOL2BTU_VEC3(RayTo));
  151. BtRayResult = BtRayResult_AllHits;
  152. break;
  153. }
  154. case ERT_AnyHit:
  155. {
  156. BtRayResult_Closest = new AnyHitRayResultCallback(LOL2BTU_VEC3(RayFrom), LOL2BTU_VEC3(RayTo));
  157. BtRayResult = BtRayResult_Closest;
  158. break;
  159. }
  160. }
  161. m_dynamics_world->rayTest(LOL2BTU_VEC3(RayFrom), LOL2BTU_VEC3(RayTo), *BtRayResult);
  162. if (BtRayResult->hasHit())
  163. {
  164. bResult = true;
  165. switch (RaycastType)
  166. {
  167. case ERT_Closest:
  168. {
  169. HitResult.m_collider_list << (EasyPhysic*)BtRayResult_Closest->m_collisionObject->getUserPointer();
  170. HitResult.m_hit_normal_list << BT2LOLU_VEC3(BtRayResult_Closest->m_hitNormalWorld);
  171. HitResult.m_hit_point_list << BT2LOLU_VEC3(BtRayResult_Closest->m_hitPointWorld);
  172. HitResult.m_hit_fraction_list << BtRayResult_Closest->m_closestHitFraction;
  173. break;
  174. }
  175. case ERT_AllHit:
  176. {
  177. for (int i = 0; i < BtRayResult_AllHits->m_collisionObjects.size(); i++)
  178. {
  179. HitResult.m_collider_list << (EasyPhysic*)BtRayResult_AllHits->m_collisionObjects[i]->getUserPointer();
  180. HitResult.m_hit_normal_list << BT2LOLU_VEC3(BtRayResult_AllHits->m_hitNormalWorld[i]);
  181. HitResult.m_hit_point_list << BT2LOLU_VEC3(BtRayResult_AllHits->m_hitPointWorld[i]);
  182. HitResult.m_hit_fraction_list << BtRayResult_AllHits->m_hitFractions[i];
  183. }
  184. break;
  185. }
  186. }
  187. }
  188. delete BtRayResult;
  189. return bResult;
  190. }
  191. void Exit()
  192. {
  193. delete m_dynamics_world;
  194. delete m_solver;
  195. delete m_dispatcher;
  196. delete m_collision_configuration;
  197. delete m_broadphase;
  198. }
  199. btDiscreteDynamicsWorld* GetWorld()
  200. {
  201. return m_dynamics_world;
  202. }
  203. private:
  204. void CustomSetContinuousDetection(bool ShouldUseCCD)
  205. {
  206. if (m_dynamics_world)
  207. m_dynamics_world->getDispatchInfo().m_useContinuous = ShouldUseCCD;
  208. }
  209. void CustomSetGravity(vec3 &NewGravity)
  210. {
  211. if (m_dynamics_world)
  212. m_dynamics_world->setGravity(LOL2BT_VEC3(NewGravity * LOL2BT_UNIT));
  213. }
  214. void CustomSetWorldLimit(vec3 const &NewWorldMin, vec3 const &NewWorldMax)
  215. {
  216. }
  217. void CustomSetTimestep(float NewTimestep) { }
  218. //broadphase
  219. btBroadphaseInterface* m_broadphase;
  220. btAxisSweep3* m_Sweep_broadphase;
  221. // Set up the collision configuration and dispatc
  222. btDefaultCollisionConfiguration* m_collision_configuration;
  223. btCollisionDispatcher* m_dispatcher;
  224. // The actual physics solver
  225. btSequentialImpulseConstraintSolver* m_solver;
  226. // The world.
  227. btDiscreteDynamicsWorld* m_dynamics_world;
  228. public:
  229. //Main logic :
  230. //The Set*() functions do the all-lib-independent data storage.
  231. //And then it calls the CustomSet*() which are the specialized versions.
  232. //Sets the continuous collision detection flag.
  233. void SetContinuousDetection(bool ShouldUseCCD)
  234. {
  235. m_using_CCD = ShouldUseCCD;
  236. CustomSetContinuousDetection(ShouldUseCCD);
  237. }
  238. //Sets the simulation gravity.
  239. void SetGravity(vec3 &NewGravity)
  240. {
  241. m_gravity = NewGravity;
  242. CustomSetGravity(NewGravity);
  243. }
  244. //Sets the simulation gravity.
  245. void SetWorldLimit(vec3 const &NewWorldMin, vec3 const &NewWorldMax)
  246. {
  247. m_world_min = NewWorldMin;
  248. m_world_max = NewWorldMax;
  249. CustomSetWorldLimit(NewWorldMin, NewWorldMax);
  250. }
  251. //Sets the simulation fixed timestep.
  252. void SetTimestep(float NewTimestep)
  253. {
  254. if (NewTimestep > .0f)
  255. {
  256. m_timestep = NewTimestep;
  257. CustomSetTimestep(NewTimestep);
  258. }
  259. }
  260. private:
  261. friend class EasyPhysic;
  262. friend class EasyCharacterController;
  263. friend class EasyConstraint;
  264. enum eEasyPhysicType
  265. {
  266. EEPT_Dynamic,
  267. EEPT_Static,
  268. EEPT_Ghost,
  269. EEPT_CollisionObject,
  270. EEPT_CharacterController,
  271. EEPT_MAX
  272. };
  273. //m_owner_simulation
  274. //Adds the given EasyPhysic to the correct list.
  275. void ObjectRegistration(bool AddObject, EasyPhysic* NewEP, eEasyPhysicType CurType)
  276. {
  277. Array<EasyPhysic*>* SearchList = NULL;
  278. switch(CurType)
  279. {
  280. case EEPT_Dynamic:
  281. {
  282. SearchList = &m_dynamic_list;
  283. break;
  284. }
  285. case EEPT_Static:
  286. {
  287. SearchList = &m_static_list;
  288. break;
  289. }
  290. case EEPT_Ghost:
  291. {
  292. SearchList = &m_ghost_list;
  293. break;
  294. }
  295. case EEPT_CollisionObject:
  296. {
  297. SearchList = &m_collision_object_list;
  298. break;
  299. }
  300. case EEPT_CharacterController:
  301. {
  302. SearchList = &m_character_controller_list;
  303. break;
  304. }
  305. }
  306. if (AddObject)
  307. {
  308. NewEP->m_owner_simulation = this;
  309. (*SearchList) << NewEP;
  310. }
  311. else
  312. {
  313. NewEP->m_owner_simulation = NULL;
  314. for (int i = 0; i < SearchList->Count(); ++i)
  315. {
  316. if ((*SearchList)[i] == NewEP)
  317. {
  318. SearchList->Remove(i--);
  319. break;
  320. }
  321. }
  322. }
  323. }
  324. void ObjectRegistration(bool AddObject, EasyConstraint* NewEC)
  325. {
  326. Array<EasyConstraint*>* SearchList = NULL;
  327. SearchList = &m_constraint_list;
  328. if (AddObject)
  329. {
  330. NewEC->m_owner_simulation = this;
  331. (*SearchList) << NewEC;
  332. }
  333. else
  334. {
  335. NewEC->m_owner_simulation = NULL;
  336. for (int i = 0; i < SearchList->Count(); ++i)
  337. {
  338. if ((*SearchList)[i] == NewEC)
  339. {
  340. SearchList->Remove(i--);
  341. break;
  342. }
  343. }
  344. }
  345. }
  346. //Easy Physics body List
  347. Array<EasyPhysic*> m_dynamic_list;
  348. Array<EasyPhysic*> m_static_list;
  349. Array<EasyPhysic*> m_ghost_list;
  350. Array<EasyPhysic*> m_collision_object_list;
  351. Array<EasyPhysic*> m_character_controller_list;
  352. Array<EasyConstraint*> m_constraint_list;
  353. //Easy Physics data storage
  354. float m_timestep;
  355. bool m_using_CCD;
  356. vec3 m_gravity;
  357. vec3 m_world_min;
  358. vec3 m_world_max;
  359. };
  360. } /* namespace phys */
  361. } /* namespace lol */
  362. #endif // __LOLPHYSICS_H__