BsPhysics.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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. /** @addtogroup Physics
  13. * @{
  14. */
  15. struct PHYSICS_INIT_DESC;
  16. /** Flags for controlling physics behaviour globally. */
  17. enum class PhysicsFlag
  18. {
  19. /**
  20. * Automatically recovers character controllers that are interpenetrating geometry. This can happen if a controller
  21. * is spawned or teleported into geometry, its size/rotation is changed so it penetrates geometry, or simply
  22. * because of numerical imprecision.
  23. */
  24. CCT_OverlapRecovery = 1<<0,
  25. /**
  26. * Performs more accurate sweeps when moving the character controller, making it less likely to interpenetrate
  27. * geometry. When overlap recovery is turned on you can consider turning this off as it can compensate for the
  28. * less precise sweeps.
  29. */
  30. CCT_PreciseSweeps = 1<<1,
  31. /**
  32. * Large triangles can cause problems with character controller collision. If this option is enabled the triangles
  33. * larger than a certain size will be automatically tesselated into smaller triangles, in order to help with
  34. * precision.
  35. *
  36. * @see Physics::getMaxTesselationEdgeLength
  37. */
  38. CCT_Tesselation = 1<<2,
  39. /**
  40. * Enables continous collision detection. This will prevent fast-moving objects from tunneling through each other.
  41. * You must also enable CCD for individual Rigidbodies. This option can have a significant performance impact.
  42. */
  43. CCD_Enable = 1<<3
  44. };
  45. /** @copydoc CharacterCollisionFlag */
  46. typedef Flags<PhysicsFlag> PhysicsFlags;
  47. BS_FLAGS_OPERATORS(PhysicsFlag)
  48. /** Provides global physics settings, factory methods for physics objects and scene queries. */
  49. class BS_CORE_EXPORT Physics : public Module<Physics>
  50. {
  51. public:
  52. Physics(const PHYSICS_INIT_DESC& init);
  53. virtual ~Physics() { }
  54. /******************************************************************************************************************/
  55. /************************************************* QUERIES ********************************************************/
  56. /******************************************************************************************************************/
  57. /**
  58. * Casts a ray into the scene and returns the closest found hit, if any.
  59. *
  60. * @param[in] ray Ray to cast into the scene.
  61. * @param[out] hit Information recorded about a hit. Only valid if method returns true.
  62. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  63. * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be
  64. * detected.
  65. * @return True if something was hit, false otherwise.
  66. */
  67. virtual bool rayCast(const Ray& ray, PhysicsQueryHit& hit, UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const;
  68. /**
  69. * Casts a ray into the scene and returns the closest found hit, if any.
  70. *
  71. * @param[in] origin Origin of the ray to cast into the scene.
  72. * @param[in] unitDir Unit direction of the ray to cast into the scene.
  73. * @param[out] hit Information recorded about a hit. Only valid if method returns true.
  74. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  75. * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be
  76. * detected.
  77. * @return True if something was hit, false otherwise.
  78. */
  79. virtual bool rayCast(const Vector3& origin, const Vector3& unitDir, PhysicsQueryHit& hit,
  80. UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0;
  81. /**
  82. * Performs a sweep into the scene using a box and returns the closest found hit, if any.
  83. *
  84. * @param[in] box Box to sweep through the scene.
  85. * @param[in] rotation Orientation of the box.
  86. * @param[in] unitDir Unit direction towards which to perform the sweep.
  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 boxCast(const AABox& box, const Quaternion& rotation, 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 sphere and returns the closest found hit, if any.
  97. *
  98. * @param[in] sphere Sphere to sweep through the scene.
  99. * @param[in] unitDir Unit direction towards which to perform the sweep.
  100. * @param[out] hit Information recorded about a hit. Only valid if method returns true.
  101. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  102. * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be
  103. * detected.
  104. * @return True if something was hit, false otherwise.
  105. */
  106. virtual bool sphereCast(const Sphere& sphere, const Vector3& unitDir, PhysicsQueryHit& hit,
  107. UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0;
  108. /**
  109. * Performs a sweep into the scene using a capsule and returns the closest found hit, if any.
  110. *
  111. * @param[in] capsule Capsule to sweep through the scene.
  112. * @param[in] rotation Orientation of the capsule.
  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 capsuleCast(const Capsule& capsule, const Quaternion& rotation, const Vector3& unitDir,
  121. PhysicsQueryHit& hit, UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0;
  122. /**
  123. * Performs a sweep into the scene using a convex mesh and returns the closest found hit, if any.
  124. *
  125. * @param[in] mesh Mesh to sweep through the scene. Must be convex.
  126. * @param[in] position Starting position of the mesh.
  127. * @param[in] rotation Orientation of the mesh.
  128. * @param[in] unitDir Unit direction towards which to perform the sweep.
  129. * @param[out] hit Information recorded about a hit. Only valid if method returns true.
  130. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  131. * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be
  132. * detected.
  133. * @return True if something was hit, false otherwise.
  134. */
  135. virtual bool convexCast(const HPhysicsMesh& mesh, const Vector3& position, const Quaternion& rotation,
  136. const Vector3& unitDir, PhysicsQueryHit& hit, UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0;
  137. /**
  138. * Casts a ray into the scene and returns all found hits.
  139. *
  140. * @param[in] ray Ray to cast into the scene.
  141. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  142. * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be
  143. * detected.
  144. * @return List of all detected hits.
  145. */
  146. virtual Vector<PhysicsQueryHit> rayCastAll(const Ray& ray, UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const;
  147. /**
  148. * Casts a ray into the scene and returns all found hits.
  149. *
  150. * @param[in] origin Origin of the ray to cast into the scene.
  151. * @param[in] unitDir Unit direction of the ray to cast into the scene.
  152. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  153. * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be
  154. * detected.
  155. * @return List of all detected hits.
  156. */
  157. virtual Vector<PhysicsQueryHit> rayCastAll(const Vector3& origin, const Vector3& unitDir,
  158. UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0;
  159. /**
  160. * Performs a sweep into the scene using a box and returns all found hits.
  161. *
  162. * @param[in] box Box to sweep through the scene.
  163. * @param[in] rotation Orientation of the box.
  164. * @param[in] unitDir Unit direction towards which to perform the sweep.
  165. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  166. * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be
  167. * detected.
  168. * @return List of all detected hits.
  169. */
  170. virtual Vector<PhysicsQueryHit> boxCastAll(const AABox& box, const Quaternion& rotation,
  171. const Vector3& unitDir, UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0;
  172. /**
  173. * Performs a sweep into the scene using a sphere and returns all found hits.
  174. *
  175. * @param[in] sphere Sphere to sweep through the scene.
  176. * @param[in] unitDir Unit direction towards which to perform the sweep.
  177. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  178. * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be
  179. * detected.
  180. * @return List of all detected hits.
  181. */
  182. virtual Vector<PhysicsQueryHit> sphereCastAll(const Sphere& sphere, const Vector3& unitDir,
  183. UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0;
  184. /**
  185. * Performs a sweep into the scene using a capsule and returns all found hits.
  186. *
  187. * @param[in] capsule Capsule to sweep through the scene.
  188. * @param[in] rotation Orientation of the capsule.
  189. * @param[in] unitDir Unit direction towards which to perform the sweep.
  190. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  191. * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be
  192. * detected.
  193. * @return List of all detected hits.
  194. */
  195. virtual Vector<PhysicsQueryHit> capsuleCastAll(const Capsule& capsule, const Quaternion& rotation,
  196. const Vector3& unitDir, UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0;
  197. /**
  198. * Performs a sweep into the scene using a convex mesh and returns all found hits.
  199. *
  200. * @param[in] mesh Mesh to sweep through the scene. Must be convex.
  201. * @param[in] position Starting position of the mesh.
  202. * @param[in] rotation Orientation of the mesh.
  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> convexCastAll(const HPhysicsMesh& mesh, const Vector3& position,
  210. const Quaternion& rotation, const Vector3& unitDir, UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0;
  211. /**
  212. * Casts a ray into the scene and checks if it has hit anything. This can be significantly more efficient than other
  213. * types of cast* calls.
  214. *
  215. * @param[in] ray Ray to cast into the scene.
  216. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  217. * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be
  218. * detected.
  219. * @return True if something was hit, false otherwise.
  220. */
  221. virtual bool rayCastAny(const Ray& ray, UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const;
  222. /**
  223. * Casts a ray into the scene and checks if it has hit anything. This can be significantly more efficient than other
  224. * types of cast* calls.
  225. *
  226. * @param[in] origin Origin of the ray to cast into the scene.
  227. * @param[in] unitDir Unit direction of the ray to cast into the scene.
  228. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  229. * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be
  230. * detected.
  231. * @return True if something was hit, false otherwise.
  232. */
  233. virtual bool rayCastAny(const Vector3& origin, const Vector3& unitDir,
  234. UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0;
  235. /**
  236. * Performs a sweep into the scene using a box and checks if it has hit anything. This can be significantly more
  237. * efficient than other types of cast* calls.
  238. *
  239. * @param[in] box Box to sweep through the scene.
  240. * @param[in] rotation Orientation of the box.
  241. * @param[in] unitDir Unit direction towards which to perform the sweep.
  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 boxCastAny(const AABox& box, const Quaternion& rotation, 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 sphere and checks if it has hit anything. This can be significantly more
  251. * efficient than other types of cast* calls.
  252. *
  253. * @param[in] sphere Sphere to sweep through the scene.
  254. * @param[in] unitDir Unit direction towards which to perform the sweep.
  255. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  256. * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be
  257. * detected.
  258. * @return True if something was hit, false otherwise.
  259. */
  260. virtual bool sphereCastAny(const Sphere& sphere, const Vector3& unitDir,
  261. UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0;
  262. /**
  263. * Performs a sweep into the scene using a capsule and checks if it has hit anything. This can be significantly more
  264. * efficient than other types of cast* calls.
  265. *
  266. * @param[in] capsule Capsule to sweep through the scene.
  267. * @param[in] rotation Orientation of the capsule.
  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 capsuleCastAny(const Capsule& capsule, const Quaternion& rotation, 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 convex mesh and checks if it has hit anything. This can be significantly
  278. * more efficient than other types of cast* calls.
  279. *
  280. * @param[in] mesh Mesh to sweep through the scene. Must be convex.
  281. * @param[in] position Starting position of the mesh.
  282. * @param[in] rotation Orientation of the mesh.
  283. * @param[in] unitDir Unit direction towards which to perform the sweep.
  284. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  285. * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be
  286. * detected.
  287. * @return True if something was hit, false otherwise.
  288. */
  289. virtual bool convexCastAny(const HPhysicsMesh& mesh, const Vector3& position, const Quaternion& rotation,
  290. const Vector3& unitDir, UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0;
  291. /**
  292. * Returns a list of all colliders in the scene that overlap the provided box.
  293. *
  294. * @param[in] box Box to check for overlap.
  295. * @param[in] rotation Orientation of the box.
  296. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  297. * @return List of all colliders that overlap the box.
  298. */
  299. virtual Vector<HCollider> boxOverlap(const AABox& box, const Quaternion& rotation,
  300. UINT64 layer = BS_ALL_LAYERS) const;
  301. /**
  302. * Returns a list of all colliders in the scene that overlap the provided sphere.
  303. *
  304. * @param[in] sphere Sphere to check for overlap.
  305. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  306. * @return List of all colliders that overlap the sphere.
  307. */
  308. virtual Vector<HCollider> sphereOverlap(const Sphere& sphere, UINT64 layer = BS_ALL_LAYERS) const;
  309. /**
  310. * Returns a list of all colliders in the scene that overlap the provided capsule.
  311. *
  312. * @param[in] capsule Capsule to check for overlap.
  313. * @param[in] rotation Orientation of the capsule.
  314. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  315. * @return List of all colliders that overlap the capsule.
  316. */
  317. virtual Vector<HCollider> capsuleOverlap(const Capsule& capsule, const Quaternion& rotation,
  318. UINT64 layer = BS_ALL_LAYERS) const;
  319. /**
  320. * Returns a list of all colliders in the scene that overlap the provided convex mesh.
  321. *
  322. * @param[in] mesh Mesh to check for overlap. Must be convex.
  323. * @param[in] position Position of the mesh.
  324. * @param[in] rotation Orientation of the mesh.
  325. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  326. * @return List of all colliders that overlap the mesh.
  327. */
  328. virtual Vector<HCollider> convexOverlap(const HPhysicsMesh& mesh, const Vector3& position,
  329. const Quaternion& rotation, UINT64 layer = BS_ALL_LAYERS) const;
  330. /**
  331. * Checks if the provided box overlaps any other collider in the scene.
  332. *
  333. * @param[in] box Box to check for overlap.
  334. * @param[in] rotation Orientation of the box.
  335. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  336. * @return True if there is overlap with another object, false otherwise.
  337. */
  338. virtual bool boxOverlapAny(const AABox& box, const Quaternion& rotation, UINT64 layer = BS_ALL_LAYERS) const = 0;
  339. /**
  340. * Checks if the provided sphere overlaps any other collider in the scene.
  341. *
  342. * @param[in] sphere Sphere to check for overlap.
  343. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  344. * @return True if there is overlap with another object, false otherwise.
  345. */
  346. virtual bool sphereOverlapAny(const Sphere& sphere, UINT64 layer = BS_ALL_LAYERS) const = 0;
  347. /**
  348. * Checks if the provided capsule overlaps any other collider in the scene.
  349. *
  350. * @param[in] capsule Capsule to check for overlap.
  351. * @param[in] rotation Orientation of the capsule.
  352. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  353. * @return True if there is overlap with another object, false otherwise.
  354. */
  355. virtual bool capsuleOverlapAny(const Capsule& capsule, const Quaternion& rotation,
  356. UINT64 layer = BS_ALL_LAYERS) const = 0;
  357. /**
  358. * Checks if the provided convex mesh overlaps any other collider in the scene.
  359. *
  360. * @param[in] mesh Mesh to check for overlap. Must be convex.
  361. * @param[in] position Position of the mesh.
  362. * @param[in] rotation Orientation of the mesh.
  363. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  364. * @return True if there is overlap with another object, false otherwise.
  365. */
  366. virtual bool convexOverlapAny(const HPhysicsMesh& mesh, const Vector3& position, const Quaternion& rotation,
  367. UINT64 layer = BS_ALL_LAYERS) const = 0;
  368. /******************************************************************************************************************/
  369. /************************************************* OPTIONS ********************************************************/
  370. /******************************************************************************************************************/
  371. /** Checks is a specific physics option enabled. */
  372. virtual bool hasFlag(PhysicsFlags flag) const { return mFlags & flag; }
  373. /** Enables or disabled a specific physics option. */
  374. virtual void setFlag(PhysicsFlags flag, bool enabled) { if (enabled) mFlags |= flag; else mFlags &= ~flag; }
  375. /** Pauses or resumes the physics simulation. */
  376. virtual void setPaused(bool paused) = 0;
  377. /** Gets the global gravity value for all objects in the scene. */
  378. virtual Vector3 getGravity() const = 0;
  379. /** Sets the global gravity value for all objects in the scene. */
  380. virtual void setGravity(const Vector3& gravity) = 0;
  381. /**
  382. * Adds a new physics region. Certain physics options require you to set up regions in which physics objects are
  383. * allowed to be in, and objects outside of these regions will not be handled by physics. You do not need to set
  384. * up these regions by default.
  385. */
  386. virtual UINT32 addBroadPhaseRegion(const AABox& region) = 0;
  387. /** Removes a physics region. */
  388. virtual void removeBroadPhaseRegion(UINT32 handle) = 0;
  389. /** Removes all physics regions. */
  390. virtual void clearBroadPhaseRegions() = 0;
  391. /**
  392. * Returns a maximum edge length before a triangle is tesselated.
  393. *
  394. * @see PhysicsFlags::CCT_Tesselation
  395. */
  396. virtual float getMaxTesselationEdgeLength() const = 0;
  397. /**
  398. * Sets a maximum edge length before a triangle is tesselated.
  399. *
  400. * @see PhysicsFlags::CCT_Tesselation
  401. */
  402. virtual void setMaxTesselationEdgeLength(float length) = 0;
  403. /**
  404. * Enables or disables collision between two layers. Each physics object can be assigned a specific layer, and here
  405. * you can determine which layers can interact with each other.
  406. */
  407. void toggleCollision(UINT64 groupA, UINT64 groupB, bool enabled);
  408. /** Checks if two collision layers are allowed to interact. */
  409. bool isCollisionEnabled(UINT64 groupA, UINT64 groupB) const;
  410. /** @name Internal
  411. * @{
  412. */
  413. /******************************************************************************************************************/
  414. /************************************************* CREATION *******************************************************/
  415. /******************************************************************************************************************/
  416. /** @copydoc PhysicsMaterial::create */
  417. virtual SPtr<PhysicsMaterial> createMaterial(float staticFriction, float dynamicFriction, float restitution) = 0;
  418. /** @copydoc PhysicsMesh::create */
  419. virtual SPtr<PhysicsMesh> createMesh(const SPtr<MeshData>& meshData, PhysicsMeshType type) = 0;
  420. /** @copydoc Rigidbody::create */
  421. virtual SPtr<Rigidbody> createRigidbody(const HSceneObject& linkedSO) = 0;
  422. /** @copydoc BoxCollider::create */
  423. virtual SPtr<BoxCollider> createBoxCollider(const Vector3& extents, const Vector3& position,
  424. const Quaternion& rotation) = 0;
  425. /** @copydoc SphereCollider::create */
  426. virtual SPtr<SphereCollider> createSphereCollider(float radius,
  427. const Vector3& position, const Quaternion& rotation) = 0;
  428. /** @copydoc PlaneCollider::create */
  429. virtual SPtr<PlaneCollider> createPlaneCollider(const Vector3& position, const Quaternion& rotation) = 0;
  430. /** @copydoc CapsuleCollider::create */
  431. virtual SPtr<CapsuleCollider> createCapsuleCollider(float radius, float halfHeight,
  432. const Vector3& position, const Quaternion& rotation) = 0;
  433. /** @copydoc MeshCollider::create */
  434. virtual SPtr<MeshCollider> createMeshCollider(const Vector3& position, const Quaternion& rotation) = 0;
  435. /** @copydoc FixedJoint::create */
  436. virtual SPtr<FixedJoint> createFixedJoint(const FIXED_JOINT_DESC& desc) = 0;
  437. /** @copydoc DistanceJoint::create */
  438. virtual SPtr<DistanceJoint> createDistanceJoint(const DISTANCE_JOINT_DESC& desc) = 0;
  439. /** @copydoc HingeJoint::create */
  440. virtual SPtr<HingeJoint> createHingeJoint(const HINGE_JOINT_DESC& desc) = 0;
  441. /** @copydoc SphericalJoint::create */
  442. virtual SPtr<SphericalJoint> createSphericalJoint(const SPHERICAL_JOINT_DESC& desc) = 0;
  443. /** @copydoc SliderJoint::create */
  444. virtual SPtr<SliderJoint> createSliderJoint(const SLIDER_JOINT_DESC& desc) = 0;
  445. /** @copydoc D6Joint::create */
  446. virtual SPtr<D6Joint> createD6Joint(const D6_JOINT_DESC& desc) = 0;
  447. /** @copydoc CharacterController::create */
  448. virtual SPtr<CharacterController> createCharacterController(const CHAR_CONTROLLER_DESC& desc) = 0;
  449. /** Triggers physics simulation update as needed. Should be called once per frame. */
  450. virtual void update() = 0;
  451. /** @copydoc Physics::boxOverlap() */
  452. virtual Vector<Collider*> _boxOverlap(const AABox& box, const Quaternion& rotation,
  453. UINT64 layer = BS_ALL_LAYERS) const = 0;
  454. /** @copydoc Physics::sphereOverlap() */
  455. virtual Vector<Collider*> _sphereOverlap(const Sphere& sphere, UINT64 layer = BS_ALL_LAYERS) const = 0;
  456. /** @copydoc Physics::capsuleOverlap() */
  457. virtual Vector<Collider*> _capsuleOverlap(const Capsule& capsule, const Quaternion& rotation,
  458. UINT64 layer = BS_ALL_LAYERS) const = 0;
  459. /** @copydoc Physics::convexOverlap() */
  460. virtual Vector<Collider*> _convexOverlap(const HPhysicsMesh& mesh, const Vector3& position,
  461. const Quaternion& rotation, UINT64 layer = BS_ALL_LAYERS) const = 0;
  462. /**
  463. * Checks does the ray hit the provided collider.
  464. *
  465. * @param[in] origin Origin of the ray to check.
  466. * @param[in] unitDir Unit direction of the ray to check.
  467. * @param[in] collider Collider to check for hit.
  468. * @param[out] hit Information about the hit. Valid only if the method returns true.
  469. * @param[in] maxDist Maximum distance from the ray origin to search for hits.
  470. * @return True if the ray has hit the collider.
  471. */
  472. virtual bool _rayCast(const Vector3& origin, const Vector3& unitDir, const Collider& collider, PhysicsQueryHit& hit,
  473. float maxDist = FLT_MAX) const = 0;
  474. /** Checks is the physics simulation update currently in progress. */
  475. bool _isUpdateInProgress() const { return mUpdateInProgress; }
  476. /** @} */
  477. static const UINT64 CollisionMapSize = 64;
  478. protected:
  479. friend class Rigidbody;
  480. mutable Mutex mMutex;
  481. bool mCollisionMap[CollisionMapSize][CollisionMapSize];
  482. bool mUpdateInProgress = false;
  483. PhysicsFlags mFlags;
  484. const static UINT32 MAX_PRIORITY = 128;
  485. };
  486. /** Provides easier access to Physics. */
  487. BS_CORE_EXPORT Physics& gPhysics();
  488. /** Contains parameters used for initializing the physics system. */
  489. struct PHYSICS_INIT_DESC
  490. {
  491. float typicalLength = 1.0f; /**< Typical length of an object in the scene. */
  492. float typicalSpeed = 9.81f; /**< Typical speed of an object in the scene. */
  493. Vector3 gravity = Vector3(0.0f, -9.81f, 0.0f); /**< Initial gravity. */
  494. bool initCooking = true; /**< Determines should the cooking library be initialized. */
  495. float timeStep = 1.0f / 60.0f; /**< Determines using what interval should the physics update happen. */
  496. /** Flags that control global physics option. */
  497. PhysicsFlags flags = PhysicsFlag::CCT_OverlapRecovery | PhysicsFlag::CCT_PreciseSweeps | PhysicsFlag::CCD_Enable;
  498. };
  499. /** @} */
  500. }