BsPhysics.h 28 KB

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