BsPhysics.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsCorePrerequisites.h"
  5. #include "BsPhysicsCommon.h"
  6. #include "BsModule.h"
  7. #include "BsVector3.h"
  8. #include "BsVector2.h"
  9. #include "BsQuaternion.h"
  10. namespace BansheeEngine
  11. {
  12. struct PHYSICS_INIT_DESC;
  13. /** Flags for controlling physics behaviour globally. */
  14. enum class PhysicsFlag
  15. {
  16. /**
  17. * Automatically recovers character controllers that are interpenetrating geometry. This can happen if a controller
  18. * is spawned or teleported into geometry, its size/rotation is changed so it penetrates geometry, or simply
  19. * because of numerical imprecision.
  20. */
  21. CCT_OverlapRecovery = 1<<0,
  22. /**
  23. * Performs more accurate sweeps when moving the character controller, making it less likely to interpenetrate
  24. * geometry. When overlap recovery is turned on you can consider turning this off as it can compensate for the
  25. * less precise sweeps.
  26. */
  27. CCT_PreciseSweeps = 1<<1,
  28. /**
  29. * Large triangles can cause problems with character controller collision. If this option is enabled the triangles
  30. * larger than a certain size will be automatically tesselated into smaller triangles, in order to help with
  31. * precision.
  32. *
  33. * @see Physics::getMaxTesselationEdgeLength
  34. */
  35. CCT_Tesselation = 1<<2
  36. };
  37. /** @copydoc CharacterCollisionFlag */
  38. typedef Flags<PhysicsFlag> PhysicsFlags;
  39. BS_FLAGS_OPERATORS(PhysicsFlag)
  40. class BS_CORE_EXPORT Physics : public Module<Physics>
  41. {
  42. public:
  43. Physics(const PHYSICS_INIT_DESC& init);
  44. virtual ~Physics() { }
  45. virtual void update() = 0;
  46. /******************************************************************************************************************/
  47. /************************************************* CREATION *******************************************************/
  48. /******************************************************************************************************************/
  49. virtual SPtr<PhysicsMaterial> createMaterial(float staticFriction, float dynamicFriction, float restitution) = 0;
  50. virtual SPtr<PhysicsMesh> createMesh(const MeshDataPtr& meshData, PhysicsMeshType type) = 0;
  51. virtual SPtr<Rigidbody> createRigidbody(const HSceneObject& linkedSO) = 0;
  52. virtual SPtr<BoxCollider> createBoxCollider(const Vector3& extents, const Vector3& position,
  53. const Quaternion& rotation) = 0;
  54. virtual SPtr<SphereCollider> createSphereCollider(float radius,
  55. const Vector3& position, const Quaternion& rotation) = 0;
  56. virtual SPtr<PlaneCollider> createPlaneCollider(const Vector3& position, const Quaternion& rotation) = 0;
  57. virtual SPtr<CapsuleCollider> createCapsuleCollider(float radius, float halfHeight,
  58. const Vector3& position, const Quaternion& rotation) = 0;
  59. virtual SPtr<MeshCollider> createMeshCollider(const Vector3& position, const Quaternion& rotation) = 0;
  60. virtual SPtr<FixedJoint> createFixedJoint() = 0;
  61. virtual SPtr<DistanceJoint> createDistanceJoint() = 0;
  62. virtual SPtr<HingeJoint> createHingeJoint() = 0;
  63. virtual SPtr<SphericalJoint> createSphericalJoint() = 0;
  64. virtual SPtr<SliderJoint> createSliderJoint() = 0;
  65. virtual SPtr<D6Joint> createD6Joint() = 0;
  66. /** @copydoc CharacterController::create */
  67. virtual SPtr<CharacterController> createCharacterController(const CHAR_CONTROLLER_DESC& desc) = 0;
  68. /******************************************************************************************************************/
  69. /************************************************* QUERIES ********************************************************/
  70. /******************************************************************************************************************/
  71. /**
  72. * Casts a ray into the scene and returns the closest found hit, if any.
  73. *
  74. * @param[in] ray Ray to cast into the scene.
  75. * @param[out] hit Information recorded about a hit. Only valid if method returns true.
  76. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  77. * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be
  78. * detected.
  79. * @return True if something was hit, false otherwise.
  80. */
  81. virtual bool rayCast(const Ray& ray, PhysicsQueryHit& hit, UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const;
  82. /**
  83. * Casts a ray into the scene and returns the closest found hit, if any.
  84. *
  85. * @param[in] origin Origin of the ray to cast into the scene.
  86. * @param[in] unitDir Unit direction of the ray to cast into the scene.
  87. * @param[out] hit Information recorded about a hit. Only valid if method returns true.
  88. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  89. * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be
  90. * detected.
  91. * @return True if something was hit, false otherwise.
  92. */
  93. virtual bool rayCast(const Vector3& origin, const Vector3& unitDir, PhysicsQueryHit& hit,
  94. UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0;
  95. /**
  96. * Performs a sweep into the scene using a box and returns the closest found hit, if any.
  97. *
  98. * @param[in] box Box to sweep through the scene.
  99. * @param[in] rotation Orientation of the box.
  100. * @param[in] unitDir Unit direction towards which to perform the sweep.
  101. * @param[out] hit Information recorded about a hit. Only valid if method returns true.
  102. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  103. * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be
  104. * detected.
  105. * @return True if something was hit, false otherwise.
  106. */
  107. virtual bool boxCast(const AABox& box, const Quaternion& rotation, const Vector3& unitDir, PhysicsQueryHit& hit,
  108. UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0;
  109. /**
  110. * Performs a sweep into the scene using a sphere and returns the closest found hit, if any.
  111. *
  112. * @param[in] sphere Sphere to sweep through the scene.
  113. * @param[in] unitDir Unit direction towards which to perform the sweep.
  114. * @param[out] hit Information recorded about a hit. Only valid if method returns true.
  115. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  116. * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be
  117. * detected.
  118. * @return True if something was hit, false otherwise.
  119. */
  120. virtual bool sphereCast(const Sphere& sphere, const Vector3& unitDir, PhysicsQueryHit& hit,
  121. UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0;
  122. /**
  123. * Performs a sweep into the scene using a capsule and returns the closest found hit, if any.
  124. *
  125. * @param[in] capsule Capsule to sweep through the scene.
  126. * @param[in] rotation Orientation of the capsule.
  127. * @param[in] unitDir Unit direction towards which to perform the sweep.
  128. * @param[out] hit Information recorded about a hit. Only valid if method returns true.
  129. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  130. * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be
  131. * detected.
  132. * @return True if something was hit, false otherwise.
  133. */
  134. virtual bool capsuleCast(const Capsule& capsule, const Quaternion& rotation, const Vector3& unitDir,
  135. PhysicsQueryHit& hit, UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0;
  136. /**
  137. * Performs a sweep into the scene using a convex mesh and returns the closest found hit, if any.
  138. *
  139. * @param[in] mesh Mesh to sweep through the scene. Must be convex.
  140. * @param[in] position Starting position of the mesh.
  141. * @param[in] rotation Orientation of the mesh.
  142. * @param[in] unitDir Unit direction towards which to perform the sweep.
  143. * @param[out] hit Information recorded about a hit. Only valid if method returns true.
  144. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  145. * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be
  146. * detected.
  147. * @return True if something was hit, false otherwise.
  148. */
  149. virtual bool convexCast(const HPhysicsMesh& mesh, const Vector3& position, const Quaternion& rotation,
  150. const Vector3& unitDir, PhysicsQueryHit& hit, UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0;
  151. /**
  152. * Casts a ray into the scene and returns all found hits.
  153. *
  154. * @param[in] ray Ray to cast into the scene.
  155. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  156. * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be
  157. * detected.
  158. * @return List of all detected hits.
  159. */
  160. virtual Vector<PhysicsQueryHit> rayCastAll(const Ray& ray, UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const;
  161. /**
  162. * Casts a ray into the scene and returns all found hits.
  163. *
  164. * @param[in] origin Origin of the ray to cast into the scene.
  165. * @param[in] unitDir Unit direction of the ray to cast into the scene.
  166. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  167. * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be
  168. * detected.
  169. * @return List of all detected hits.
  170. */
  171. virtual Vector<PhysicsQueryHit> rayCastAll(const Vector3& origin, const Vector3& unitDir,
  172. UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0;
  173. /**
  174. * Performs a sweep into the scene using a box and returns all found hits.
  175. *
  176. * @param[in] box Box to sweep through the scene.
  177. * @param[in] rotation Orientation of the box.
  178. * @param[in] unitDir Unit direction towards which to perform the sweep.
  179. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  180. * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be
  181. * detected.
  182. * @return List of all detected hits.
  183. */
  184. virtual Vector<PhysicsQueryHit> boxCastAll(const AABox& box, const Quaternion& rotation,
  185. const Vector3& unitDir, UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0;
  186. /**
  187. * Performs a sweep into the scene using a sphere and returns all found hits.
  188. *
  189. * @param[in] sphere Sphere to sweep through the scene.
  190. * @param[in] unitDir Unit direction towards which to perform the sweep.
  191. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  192. * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be
  193. * detected.
  194. * @return List of all detected hits.
  195. */
  196. virtual Vector<PhysicsQueryHit> sphereCastAll(const Sphere& sphere, const Vector3& unitDir,
  197. UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0;
  198. /**
  199. * Performs a sweep into the scene using a capsule and returns all found hits.
  200. *
  201. * @param[in] capsule Capsule to sweep through the scene.
  202. * @param[in] rotation Orientation of the capsule.
  203. * @param[in] unitDir Unit direction towards which to perform the sweep.
  204. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  205. * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be
  206. * detected.
  207. * @return List of all detected hits.
  208. */
  209. virtual Vector<PhysicsQueryHit> capsuleCastAll(const Capsule& capsule, const Quaternion& rotation,
  210. const Vector3& unitDir, UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0;
  211. /**
  212. * Performs a sweep into the scene using a convex mesh and returns all found hits.
  213. *
  214. * @param[in] mesh Mesh to sweep through the scene. Must be convex.
  215. * @param[in] position Starting position of the mesh.
  216. * @param[in] rotation Orientation of the mesh.
  217. * @param[in] unitDir Unit direction towards which to perform the sweep.
  218. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  219. * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be
  220. * detected.
  221. * @return List of all detected hits.
  222. */
  223. virtual Vector<PhysicsQueryHit> convexCastAll(const HPhysicsMesh& mesh, const Vector3& position,
  224. const Quaternion& rotation, const Vector3& unitDir, UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0;
  225. /**
  226. * Casts a ray into the scene and checks if it has hit anything. This can be significantly more efficient than other
  227. * types of cast* calls.
  228. *
  229. * @param[in] ray Ray to cast into the scene.
  230. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  231. * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be
  232. * detected.
  233. * @return True if something was hit, false otherwise.
  234. */
  235. virtual bool rayCastAny(const Ray& ray, UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const;
  236. /**
  237. * Casts a ray into the scene and checks if it has hit anything. This can be significantly more efficient than other
  238. * types of cast* calls.
  239. *
  240. * @param[in] origin Origin of the ray to cast into the scene.
  241. * @param[in] unitDir Unit direction of the ray to cast into the scene.
  242. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  243. * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be
  244. * detected.
  245. * @return True if something was hit, false otherwise.
  246. */
  247. virtual bool rayCastAny(const Vector3& origin, const Vector3& unitDir,
  248. UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0;
  249. /**
  250. * Performs a sweep into the scene using a box and checks if it has hit anything. This can be significantly more
  251. * efficient than other types of cast* calls.
  252. *
  253. * @param[in] box Box to sweep through the scene.
  254. * @param[in] rotation Orientation of the box.
  255. * @param[in] unitDir Unit direction towards which to perform the sweep.
  256. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  257. * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be
  258. * detected.
  259. * @return True if something was hit, false otherwise.
  260. */
  261. virtual bool boxCastAny(const AABox& box, const Quaternion& rotation, const Vector3& unitDir,
  262. UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0;
  263. /**
  264. * Performs a sweep into the scene using a sphere and checks if it has hit anything. This can be significantly more
  265. * efficient than other types of cast* calls.
  266. *
  267. * @param[in] sphere Sphere to sweep through the scene.
  268. * @param[in] unitDir Unit direction towards which to perform the sweep.
  269. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  270. * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be
  271. * detected.
  272. * @return True if something was hit, false otherwise.
  273. */
  274. virtual bool sphereCastAny(const Sphere& sphere, const Vector3& unitDir,
  275. UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0;
  276. /**
  277. * Performs a sweep into the scene using a capsule and checks if it has hit anything. This can be significantly more
  278. * efficient than other types of cast* calls.
  279. *
  280. * @param[in] capsule Capsule to sweep through the scene.
  281. * @param[in] rotation Orientation of the capsule.
  282. * @param[in] unitDir Unit direction towards which to perform the sweep.
  283. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  284. * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be
  285. * detected.
  286. * @return True if something was hit, false otherwise.
  287. */
  288. virtual bool capsuleCastAny(const Capsule& capsule, const Quaternion& rotation, const Vector3& unitDir,
  289. UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0;
  290. /**
  291. * Performs a sweep into the scene using a convex mesh and checks if it has hit anything. This can be significantly
  292. * more efficient than other types of cast* calls.
  293. *
  294. * @param[in] mesh Mesh to sweep through the scene. Must be convex.
  295. * @param[in] position Starting position of the mesh.
  296. * @param[in] rotation Orientation of the mesh.
  297. * @param[in] unitDir Unit direction towards which to perform the sweep.
  298. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  299. * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be
  300. * detected.
  301. * @return True if something was hit, false otherwise.
  302. */
  303. virtual bool convexCastAny(const HPhysicsMesh& mesh, const Vector3& position, const Quaternion& rotation,
  304. const Vector3& unitDir, UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0;
  305. /**
  306. * Returns a list of all colliders in the scene that overlap the provided box.
  307. *
  308. * @param[in] box Box to check for overlap.
  309. * @param[in] rotation Orientation of the box.
  310. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  311. * @return List of all colliders that overlap the box.
  312. */
  313. virtual Vector<HCollider> boxOverlap(const AABox& box, const Quaternion& rotation,
  314. UINT64 layer = BS_ALL_LAYERS) const;
  315. /**
  316. * Returns a list of all colliders in the scene that overlap the provided sphere.
  317. *
  318. * @param[in] Sphere Sphere to check for overlap.
  319. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  320. * @return List of all colliders that overlap the sphere.
  321. */
  322. virtual Vector<HCollider> sphereOverlap(const Sphere& sphere, UINT64 layer = BS_ALL_LAYERS) const;
  323. /**
  324. * Returns a list of all colliders in the scene that overlap the provided capsule.
  325. *
  326. * @param[in] capsule Capsule to check for overlap.
  327. * @param[in] rotation Orientation of the capsule.
  328. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  329. * @return List of all colliders that overlap the capsule.
  330. */
  331. virtual Vector<HCollider> capsuleOverlap(const Capsule& capsule, const Quaternion& rotation,
  332. UINT64 layer = BS_ALL_LAYERS) const;
  333. /**
  334. * Returns a list of all colliders in the scene that overlap the provided convex mesh.
  335. *
  336. * @param[in] mesh Mesh to check for overlap. Must be convex.
  337. * @param[in] position Position of the mesh.
  338. * @param[in] rotation Orientation of the mesh.
  339. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  340. * @return List of all colliders that overlap the mesh.
  341. */
  342. virtual Vector<HCollider> convexOverlap(const HPhysicsMesh& mesh, const Vector3& position,
  343. const Quaternion& rotation, UINT64 layer = BS_ALL_LAYERS) const;
  344. /**
  345. * Checks if the provided box overlaps any other collider in the scene.
  346. *
  347. * @param[in] box Box to check for overlap.
  348. * @param[in] rotation Orientation of the box.
  349. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  350. * @return True if there is overlap with another object, false otherwise.
  351. */
  352. virtual bool boxOverlapAny(const AABox& box, const Quaternion& rotation, UINT64 layer = BS_ALL_LAYERS) const = 0;
  353. /**
  354. * Checks if the provided sphere overlaps any other collider in the scene.
  355. *
  356. * @param[in] sphere Sphere to check for overlap.
  357. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  358. * @return True if there is overlap with another object, false otherwise.
  359. */
  360. virtual bool sphereOverlapAny(const Sphere& sphere, UINT64 layer = BS_ALL_LAYERS) const = 0;
  361. /**
  362. * Checks if the provided capsule overlaps any other collider in the scene.
  363. *
  364. * @param[in] capsule Capsule to check for overlap.
  365. * @param[in] rotation Orientation of the capsule.
  366. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  367. * @return True if there is overlap with another object, false otherwise.
  368. */
  369. virtual bool capsuleOverlapAny(const Capsule& capsule, const Quaternion& rotation,
  370. UINT64 layer = BS_ALL_LAYERS) const = 0;
  371. /**
  372. * Checks if the provided convex mesh overlaps any other collider in the scene.
  373. *
  374. * @param[in] mesh Mesh to check for overlap. Must be convex.
  375. * @param[in] position Position of the mesh.
  376. * @param[in] rotation Orientation of the mesh.
  377. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  378. * @return True if there is overlap with another object, false otherwise.
  379. */
  380. virtual bool convexOverlapAny(const HPhysicsMesh& mesh, const Vector3& position, const Quaternion& rotation,
  381. UINT64 layer = BS_ALL_LAYERS) const = 0;
  382. /******************************************************************************************************************/
  383. /************************************************* OPTIONS ********************************************************/
  384. /******************************************************************************************************************/
  385. virtual bool hasFlag(PhysicsFlags flag) const { return mFlags & flag; }
  386. virtual void setFlag(PhysicsFlags flag, bool enabled) { if (enabled) mFlags |= flag; else mFlags &= ~flag; }
  387. virtual Vector3 getGravity() const = 0;
  388. virtual void setGravity(const Vector3& gravity) = 0;
  389. virtual UINT32 addBroadPhaseRegion(const AABox& region) = 0;
  390. virtual void removeBroadPhaseRegion(UINT32 handle) = 0;
  391. virtual void clearBroadPhaseRegions() = 0;
  392. /**
  393. * Returns a maximum edge length before a triangle is tesselated.
  394. *
  395. * @see PhysicsFlags::CCT_Tesselation
  396. */
  397. virtual float getMaxTesselationEdgeLength() const = 0;
  398. /**
  399. * Sets a maximum edge length before a triangle is tesselated.
  400. *
  401. * @see PhysicsFlags::CCT_Tesselation
  402. */
  403. virtual void setMaxTesselationEdgeLength(float length) = 0;
  404. void toggleCollision(UINT64 groupA, UINT64 groupB, bool enabled);
  405. bool isCollisionEnabled(UINT64 groupA, UINT64 groupB) const;
  406. /** @cond INTERNAL */
  407. /** @copydoc Physics::boxOverlap() */
  408. virtual Vector<Collider*> _boxOverlap(const AABox& box, const Quaternion& rotation,
  409. UINT64 layer = BS_ALL_LAYERS) const = 0;
  410. /** @copydoc Physics::sphereOverlap() */
  411. virtual Vector<Collider*> _sphereOverlap(const Sphere& sphere, UINT64 layer = BS_ALL_LAYERS) const = 0;
  412. /** @copydoc Physics::capsuleOverlap() */
  413. virtual Vector<Collider*> _capsuleOverlap(const Capsule& capsule, const Quaternion& rotation,
  414. UINT64 layer = BS_ALL_LAYERS) const = 0;
  415. /** @copydoc Physics::convexOverlap() */
  416. virtual Vector<Collider*> _convexOverlap(const HPhysicsMesh& mesh, const Vector3& position,
  417. const Quaternion& rotation, UINT64 layer = BS_ALL_LAYERS) const = 0;
  418. /**
  419. * Checks does the ray hit the provided collider.
  420. *
  421. * @param[in] origin Origin of the ray to check.
  422. * @param[in] unitDir Unit direction of the ray to check.
  423. * @param[in] collider Collider to check for hit.
  424. * @param[out] hit Information about the hit. Valid only if the method returns true.
  425. * @param[in] maxDist Maximum distance from the ray origin to search for hits.
  426. * @return True if the ray has hit the collider.
  427. */
  428. virtual bool _rayCast(const Vector3& origin, const Vector3& unitDir, const Collider& collider, PhysicsQueryHit& hit,
  429. float maxDist = FLT_MAX) const = 0;
  430. bool _isUpdateInProgress() const { return mUpdateInProgress; }
  431. /** @endcond */
  432. static const UINT64 CollisionMapSize = 64;
  433. protected:
  434. friend class Rigidbody;
  435. void registerRigidbody(Rigidbody* body, UINT32 priority);
  436. void unregisterRigidbody(UINT32 id, UINT32 priority);
  437. void updatePriority(UINT32 id, UINT32 oldPriority, UINT32 newPriority);
  438. mutable Mutex mMutex;
  439. bool mCollisionMap[CollisionMapSize][CollisionMapSize];
  440. bool mUpdateInProgress = false;
  441. PhysicsFlags mFlags;
  442. Vector<Vector<Rigidbody*>> mRigidbodies; // TODO: Unused for now, but keeping it here just in case I change the design. Remove later.
  443. const static UINT32 MAX_PRIORITY = 128;
  444. };
  445. /** Provides easier access to Physics. */
  446. BS_CORE_EXPORT Physics& gPhysics();
  447. struct PHYSICS_INIT_DESC
  448. {
  449. float typicalLength = 1.0f;
  450. float typicalSpeed = 9.81f;
  451. Vector3 gravity = Vector3(0.0f, -9.81f, 0.0f);
  452. bool initCooking = true; // TODO: Disable this for Game build
  453. float timeStep = 1.0f / 60.0f;
  454. PhysicsFlags flags = PhysicsFlag::CCT_OverlapRecovery | PhysicsFlag::CCT_PreciseSweeps;
  455. };
  456. }