25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

427 satır
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. default:
  161. {
  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. default:
  191. {
  192. break;
  193. }
  194. }
  195. }
  196. delete BtRayResult;
  197. return bResult;
  198. }
  199. void Exit()
  200. {
  201. delete m_dynamics_world;
  202. delete m_solver;
  203. delete m_dispatcher;
  204. delete m_collision_configuration;
  205. delete m_broadphase;
  206. }
  207. btDiscreteDynamicsWorld* GetWorld()
  208. {
  209. return m_dynamics_world;
  210. }
  211. private:
  212. void CustomSetContinuousDetection(bool ShouldUseCCD)
  213. {
  214. if (m_dynamics_world)
  215. m_dynamics_world->getDispatchInfo().m_useContinuous = ShouldUseCCD;
  216. }
  217. void CustomSetGravity(vec3 &NewGravity)
  218. {
  219. if (m_dynamics_world)
  220. m_dynamics_world->setGravity(LOL2BT_VEC3(NewGravity * LOL2BT_UNIT));
  221. }
  222. void CustomSetWorldLimit(vec3 const &NewWorldMin, vec3 const &NewWorldMax)
  223. {
  224. }
  225. void CustomSetTimestep(float NewTimestep) { }
  226. //broadphase
  227. btBroadphaseInterface* m_broadphase;
  228. btAxisSweep3* m_Sweep_broadphase;
  229. // Set up the collision configuration and dispatc
  230. btDefaultCollisionConfiguration* m_collision_configuration;
  231. btCollisionDispatcher* m_dispatcher;
  232. // The actual physics solver
  233. btSequentialImpulseConstraintSolver* m_solver;
  234. // The world.
  235. btDiscreteDynamicsWorld* m_dynamics_world;
  236. public:
  237. //Main logic :
  238. //The Set*() functions do the all-lib-independent data storage.
  239. //And then it calls the CustomSet*() which are the specialized versions.
  240. //Sets the continuous collision detection flag.
  241. void SetContinuousDetection(bool ShouldUseCCD)
  242. {
  243. m_using_CCD = ShouldUseCCD;
  244. CustomSetContinuousDetection(ShouldUseCCD);
  245. }
  246. //Sets the simulation gravity.
  247. void SetGravity(vec3 &NewGravity)
  248. {
  249. m_gravity = NewGravity;
  250. CustomSetGravity(NewGravity);
  251. }
  252. //Sets the simulation gravity.
  253. void SetWorldLimit(vec3 const &NewWorldMin, vec3 const &NewWorldMax)
  254. {
  255. m_world_min = NewWorldMin;
  256. m_world_max = NewWorldMax;
  257. CustomSetWorldLimit(NewWorldMin, NewWorldMax);
  258. }
  259. //Sets the simulation fixed timestep.
  260. void SetTimestep(float NewTimestep)
  261. {
  262. if (NewTimestep > .0f)
  263. {
  264. m_timestep = NewTimestep;
  265. CustomSetTimestep(NewTimestep);
  266. }
  267. }
  268. private:
  269. friend class EasyPhysic;
  270. friend class EasyCharacterController;
  271. friend class EasyConstraint;
  272. enum eEasyPhysicType
  273. {
  274. EEPT_Dynamic,
  275. EEPT_Static,
  276. EEPT_Ghost,
  277. EEPT_CollisionObject,
  278. EEPT_CharacterController,
  279. EEPT_MAX
  280. };
  281. //m_owner_simulation
  282. //Adds the given EasyPhysic to the correct list.
  283. void ObjectRegistration(bool AddObject, EasyPhysic* NewEP, eEasyPhysicType CurType)
  284. {
  285. Array<EasyPhysic*>* SearchList = NULL;
  286. switch(CurType)
  287. {
  288. case EEPT_Dynamic:
  289. {
  290. SearchList = &m_dynamic_list;
  291. break;
  292. }
  293. case EEPT_Static:
  294. {
  295. SearchList = &m_static_list;
  296. break;
  297. }
  298. case EEPT_Ghost:
  299. {
  300. SearchList = &m_ghost_list;
  301. break;
  302. }
  303. case EEPT_CollisionObject:
  304. {
  305. SearchList = &m_collision_object_list;
  306. break;
  307. }
  308. case EEPT_CharacterController:
  309. {
  310. SearchList = &m_character_controller_list;
  311. break;
  312. }
  313. }
  314. if (AddObject)
  315. {
  316. NewEP->m_owner_simulation = this;
  317. (*SearchList) << NewEP;
  318. }
  319. else
  320. {
  321. NewEP->m_owner_simulation = NULL;
  322. for (int i = 0; i < SearchList->Count(); ++i)
  323. {
  324. if ((*SearchList)[i] == NewEP)
  325. {
  326. SearchList->Remove(i--);
  327. break;
  328. }
  329. }
  330. }
  331. }
  332. void ObjectRegistration(bool AddObject, EasyConstraint* NewEC)
  333. {
  334. Array<EasyConstraint*>* SearchList = NULL;
  335. SearchList = &m_constraint_list;
  336. if (AddObject)
  337. {
  338. NewEC->m_owner_simulation = this;
  339. (*SearchList) << NewEC;
  340. }
  341. else
  342. {
  343. NewEC->m_owner_simulation = NULL;
  344. for (int i = 0; i < SearchList->Count(); ++i)
  345. {
  346. if ((*SearchList)[i] == NewEC)
  347. {
  348. SearchList->Remove(i--);
  349. break;
  350. }
  351. }
  352. }
  353. }
  354. //Easy Physics body List
  355. Array<EasyPhysic*> m_dynamic_list;
  356. Array<EasyPhysic*> m_static_list;
  357. Array<EasyPhysic*> m_ghost_list;
  358. Array<EasyPhysic*> m_collision_object_list;
  359. Array<EasyPhysic*> m_character_controller_list;
  360. Array<EasyConstraint*> m_constraint_list;
  361. //Easy Physics data storage
  362. float m_timestep;
  363. bool m_using_CCD;
  364. vec3 m_gravity;
  365. vec3 m_world_min;
  366. vec3 m_world_max;
  367. };
  368. } /* namespace phys */
  369. } /* namespace lol */
  370. #endif // __LOLPHYSICS_H__