BsPhysics.h 26 KB

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