You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

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