BsPhysics.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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 "Physics/BsPhysicsCommon.h"
  7. #include "Utility/BsModule.h"
  8. #include "Math/BsVector3.h"
  9. #include "Math/BsVector2.h"
  10. #include "Math/BsQuaternion.h"
  11. namespace bs
  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 BS_SCRIPT_EXPORT(m:Physics) 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. BS_SCRIPT_EXPORT(n:RayCast)
  69. virtual bool rayCast(const Ray& ray, PhysicsQueryHit& hit, UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const;
  70. /**
  71. * Casts a ray into the scene and returns the closest found hit, if any.
  72. *
  73. * @param[in] origin Origin of the ray to cast into the scene.
  74. * @param[in] unitDir Unit direction of the 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. BS_SCRIPT_EXPORT(n:RayCast)
  82. virtual bool rayCast(const Vector3& origin, const Vector3& unitDir, PhysicsQueryHit& hit,
  83. UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0;
  84. /**
  85. * Performs a sweep into the scene using a box and returns the closest found hit, if any.
  86. *
  87. * @param[in] box Box to sweep through the scene.
  88. * @param[in] rotation Orientation of the box.
  89. * @param[in] unitDir Unit direction towards which to perform the sweep.
  90. * @param[out] hit Information recorded about a hit. Only valid if method returns true.
  91. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  92. * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be
  93. * detected.
  94. * @return True if something was hit, false otherwise.
  95. */
  96. BS_SCRIPT_EXPORT(n:BoxCast)
  97. virtual bool boxCast(const AABox& box, const Quaternion& rotation, const Vector3& unitDir, PhysicsQueryHit& hit,
  98. UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0;
  99. /**
  100. * Performs a sweep into the scene using a sphere and returns the closest found hit, if any.
  101. *
  102. * @param[in] sphere Sphere to sweep through the scene.
  103. * @param[in] unitDir Unit direction towards which to perform the sweep.
  104. * @param[out] hit Information recorded about a hit. Only valid if method returns true.
  105. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  106. * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be
  107. * detected.
  108. * @return True if something was hit, false otherwise.
  109. */
  110. BS_SCRIPT_EXPORT(n:SphereCast)
  111. virtual bool sphereCast(const Sphere& sphere, const Vector3& unitDir, PhysicsQueryHit& hit,
  112. UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0;
  113. /**
  114. * Performs a sweep into the scene using a capsule and returns the closest found hit, if any.
  115. *
  116. * @param[in] capsule Capsule to sweep through the scene.
  117. * @param[in] rotation Orientation of the capsule.
  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. BS_SCRIPT_EXPORT(n:CapsuleCast)
  126. virtual bool capsuleCast(const Capsule& capsule, const Quaternion& rotation, const Vector3& unitDir,
  127. PhysicsQueryHit& hit, UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0;
  128. /**
  129. * Performs a sweep into the scene using a convex mesh and returns the closest found hit, if any.
  130. *
  131. * @param[in] mesh Mesh to sweep through the scene. Must be convex.
  132. * @param[in] position Starting position of the mesh.
  133. * @param[in] rotation Orientation of the mesh.
  134. * @param[in] unitDir Unit direction towards which to perform the sweep.
  135. * @param[out] hit Information recorded about a hit. Only valid if method returns true.
  136. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  137. * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be
  138. * detected.
  139. * @return True if something was hit, false otherwise.
  140. */
  141. BS_SCRIPT_EXPORT(n:ConvexCast)
  142. virtual bool convexCast(const HPhysicsMesh& mesh, const Vector3& position, const Quaternion& rotation,
  143. const Vector3& unitDir, PhysicsQueryHit& hit, UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0;
  144. /**
  145. * Casts a ray into the scene and returns all found hits.
  146. *
  147. * @param[in] ray Ray to cast into the scene.
  148. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  149. * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be
  150. * detected.
  151. * @return List of all detected hits.
  152. */
  153. BS_SCRIPT_EXPORT(n:RayCastAll)
  154. virtual Vector<PhysicsQueryHit> rayCastAll(const Ray& ray, UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const;
  155. /**
  156. * Casts a ray into the scene and returns all found hits.
  157. *
  158. * @param[in] origin Origin of the ray to cast into the scene.
  159. * @param[in] unitDir Unit direction of the 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. BS_SCRIPT_EXPORT(n:RayCastAll)
  166. virtual Vector<PhysicsQueryHit> rayCastAll(const Vector3& origin, const Vector3& unitDir,
  167. UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0;
  168. /**
  169. * Performs a sweep into the scene using a box and returns all found hits.
  170. *
  171. * @param[in] box Box to sweep through the scene.
  172. * @param[in] rotation Orientation of the box.
  173. * @param[in] unitDir Unit direction towards which to perform the sweep.
  174. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  175. * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be
  176. * detected.
  177. * @return List of all detected hits.
  178. */
  179. BS_SCRIPT_EXPORT(n:BoxCastAll)
  180. virtual Vector<PhysicsQueryHit> boxCastAll(const AABox& box, const Quaternion& rotation,
  181. const Vector3& unitDir, UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0;
  182. /**
  183. * Performs a sweep into the scene using a sphere and returns all found hits.
  184. *
  185. * @param[in] sphere Sphere to sweep through the scene.
  186. * @param[in] unitDir Unit direction towards which to perform the sweep.
  187. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  188. * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be
  189. * detected.
  190. * @return List of all detected hits.
  191. */
  192. BS_SCRIPT_EXPORT(n:SphereCastAll)
  193. virtual Vector<PhysicsQueryHit> sphereCastAll(const Sphere& sphere, const Vector3& unitDir,
  194. UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0;
  195. /**
  196. * Performs a sweep into the scene using a capsule and returns all found hits.
  197. *
  198. * @param[in] capsule Capsule to sweep through the scene.
  199. * @param[in] rotation Orientation of the capsule.
  200. * @param[in] unitDir Unit direction towards which to perform the sweep.
  201. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  202. * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be
  203. * detected.
  204. * @return List of all detected hits.
  205. */
  206. BS_SCRIPT_EXPORT(n:CapsuleCastAll)
  207. virtual Vector<PhysicsQueryHit> capsuleCastAll(const Capsule& capsule, const Quaternion& rotation,
  208. const Vector3& unitDir, UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0;
  209. /**
  210. * Performs a sweep into the scene using a convex mesh and returns all found hits.
  211. *
  212. * @param[in] mesh Mesh to sweep through the scene. Must be convex.
  213. * @param[in] position Starting position of the mesh.
  214. * @param[in] rotation Orientation of the mesh.
  215. * @param[in] unitDir Unit direction towards which to perform the sweep.
  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 List of all detected hits.
  220. */
  221. BS_SCRIPT_EXPORT(n:ConvexCastAll)
  222. virtual Vector<PhysicsQueryHit> convexCastAll(const HPhysicsMesh& mesh, const Vector3& position,
  223. const Quaternion& rotation, const Vector3& unitDir, UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0;
  224. /**
  225. * Casts a ray into the scene and checks if it has hit anything. This can be significantly more efficient than other
  226. * types of cast* calls.
  227. *
  228. * @param[in] ray 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. BS_SCRIPT_EXPORT(n:RayCastAny)
  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. BS_SCRIPT_EXPORT(n:RayCastAny)
  248. virtual bool rayCastAny(const Vector3& origin, 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 box and checks if it has hit anything. This can be significantly more
  252. * efficient than other types of cast* calls.
  253. *
  254. * @param[in] box Box to sweep through the scene.
  255. * @param[in] rotation Orientation of the box.
  256. * @param[in] unitDir Unit direction towards which to perform the sweep.
  257. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  258. * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be
  259. * detected.
  260. * @return True if something was hit, false otherwise.
  261. */
  262. BS_SCRIPT_EXPORT(n:BoxCastAny)
  263. virtual bool boxCastAny(const AABox& box, const Quaternion& rotation, const Vector3& unitDir,
  264. UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0;
  265. /**
  266. * Performs a sweep into the scene using a sphere and checks if it has hit anything. This can be significantly more
  267. * efficient than other types of cast* calls.
  268. *
  269. * @param[in] sphere Sphere to sweep through the scene.
  270. * @param[in] unitDir Unit direction towards which to perform the sweep.
  271. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  272. * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be
  273. * detected.
  274. * @return True if something was hit, false otherwise.
  275. */
  276. BS_SCRIPT_EXPORT(n:SphereCastAny)
  277. virtual bool sphereCastAny(const Sphere& sphere, const Vector3& unitDir,
  278. UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0;
  279. /**
  280. * Performs a sweep into the scene using a capsule and checks if it has hit anything. This can be significantly more
  281. * efficient than other types of cast* calls.
  282. *
  283. * @param[in] capsule Capsule to sweep through the scene.
  284. * @param[in] rotation Orientation of the capsule.
  285. * @param[in] unitDir Unit direction towards which to perform the sweep.
  286. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  287. * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be
  288. * detected.
  289. * @return True if something was hit, false otherwise.
  290. */
  291. BS_SCRIPT_EXPORT(n:CapsuleCastAny)
  292. virtual bool capsuleCastAny(const Capsule& capsule, const Quaternion& rotation, const Vector3& unitDir,
  293. UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0;
  294. /**
  295. * Performs a sweep into the scene using a convex mesh and checks if it has hit anything. This can be significantly
  296. * more efficient than other types of cast* calls.
  297. *
  298. * @param[in] mesh Mesh to sweep through the scene. Must be convex.
  299. * @param[in] position Starting position of the mesh.
  300. * @param[in] rotation Orientation of the mesh.
  301. * @param[in] unitDir Unit direction towards which to perform the sweep.
  302. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  303. * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be
  304. * detected.
  305. * @return True if something was hit, false otherwise.
  306. */
  307. BS_SCRIPT_EXPORT(n:ConvexCastAny)
  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. BS_SCRIPT_EXPORT(n:BoxOverlap)
  319. virtual Vector<HCollider> boxOverlap(const AABox& box, const Quaternion& rotation,
  320. UINT64 layer = BS_ALL_LAYERS) const;
  321. /**
  322. * Returns a list of all colliders in the scene that overlap the provided sphere.
  323. *
  324. * @param[in] sphere Sphere to check for overlap.
  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 sphere.
  327. */
  328. BS_SCRIPT_EXPORT(n:SphereOverlap)
  329. virtual Vector<HCollider> sphereOverlap(const Sphere& sphere, UINT64 layer = BS_ALL_LAYERS) const;
  330. /**
  331. * Returns a list of all colliders in the scene that overlap the provided capsule.
  332. *
  333. * @param[in] capsule Capsule to check for overlap.
  334. * @param[in] rotation Orientation of the capsule.
  335. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  336. * @return List of all colliders that overlap the capsule.
  337. */
  338. BS_SCRIPT_EXPORT(n:CapsuleOverlap)
  339. virtual Vector<HCollider> capsuleOverlap(const Capsule& capsule, const Quaternion& rotation,
  340. UINT64 layer = BS_ALL_LAYERS) const;
  341. /**
  342. * Returns a list of all colliders in the scene that overlap the provided convex mesh.
  343. *
  344. * @param[in] mesh Mesh to check for overlap. Must be convex.
  345. * @param[in] position Position of the mesh.
  346. * @param[in] rotation Orientation of the mesh.
  347. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  348. * @return List of all colliders that overlap the mesh.
  349. */
  350. BS_SCRIPT_EXPORT(n:ConvexOverlap)
  351. virtual Vector<HCollider> convexOverlap(const HPhysicsMesh& mesh, const Vector3& position,
  352. const Quaternion& rotation, UINT64 layer = BS_ALL_LAYERS) const;
  353. /**
  354. * Checks if the provided box overlaps any other collider in the scene.
  355. *
  356. * @param[in] box Box to check for overlap.
  357. * @param[in] rotation Orientation of the box.
  358. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  359. * @return True if there is overlap with another object, false otherwise.
  360. */
  361. BS_SCRIPT_EXPORT(n:BoxOverlapAny)
  362. virtual bool boxOverlapAny(const AABox& box, const Quaternion& rotation, UINT64 layer = BS_ALL_LAYERS) const = 0;
  363. /**
  364. * Checks if the provided sphere overlaps any other collider in the scene.
  365. *
  366. * @param[in] sphere Sphere to check for overlap.
  367. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  368. * @return True if there is overlap with another object, false otherwise.
  369. */
  370. BS_SCRIPT_EXPORT(n:SphereOverlapAny)
  371. virtual bool sphereOverlapAny(const Sphere& sphere, UINT64 layer = BS_ALL_LAYERS) const = 0;
  372. /**
  373. * Checks if the provided capsule overlaps any other collider in the scene.
  374. *
  375. * @param[in] capsule Capsule to check for overlap.
  376. * @param[in] rotation Orientation of the capsule.
  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. BS_SCRIPT_EXPORT(n:CapsuleOverlapAny)
  381. virtual bool capsuleOverlapAny(const Capsule& capsule, const Quaternion& rotation,
  382. UINT64 layer = BS_ALL_LAYERS) const = 0;
  383. /**
  384. * Checks if the provided convex mesh overlaps any other collider in the scene.
  385. *
  386. * @param[in] mesh Mesh to check for overlap. Must be convex.
  387. * @param[in] position Position of the mesh.
  388. * @param[in] rotation Orientation of the mesh.
  389. * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects.
  390. * @return True if there is overlap with another object, false otherwise.
  391. */
  392. BS_SCRIPT_EXPORT(n:ConvexOverlapAny)
  393. virtual bool convexOverlapAny(const HPhysicsMesh& mesh, const Vector3& position, const Quaternion& rotation,
  394. UINT64 layer = BS_ALL_LAYERS) const = 0;
  395. /******************************************************************************************************************/
  396. /************************************************* OPTIONS ********************************************************/
  397. /******************************************************************************************************************/
  398. /** Checks is a specific physics option enabled. */
  399. virtual bool hasFlag(PhysicsFlags flag) const { return mFlags & flag; }
  400. /** Enables or disabled a specific physics option. */
  401. virtual void setFlag(PhysicsFlags flag, bool enabled) { if (enabled) mFlags |= flag; else mFlags &= ~flag; }
  402. /** Pauses or resumes the physics simulation. */
  403. virtual void setPaused(bool paused) = 0;
  404. /** @copydoc setGravity() */
  405. BS_SCRIPT_EXPORT(n:Gravity,pr:getter)
  406. virtual Vector3 getGravity() const = 0;
  407. /** Determines the global gravity value for all objects in the scene. */
  408. BS_SCRIPT_EXPORT(n:Gravity,pr:setter)
  409. virtual void setGravity(const Vector3& gravity) = 0;
  410. /**
  411. * Adds a new physics region. Certain physics options require you to set up regions in which physics objects are
  412. * allowed to be in, and objects outside of these regions will not be handled by physics. You do not need to set
  413. * up these regions by default.
  414. */
  415. BS_SCRIPT_EXPORT(n:AddPhysicsRegion)
  416. virtual UINT32 addBroadPhaseRegion(const AABox& region) = 0;
  417. /** Removes a physics region. */
  418. BS_SCRIPT_EXPORT(n:RemovePhysicsRegion)
  419. virtual void removeBroadPhaseRegion(UINT32 handle) = 0;
  420. /** Removes all physics regions. */
  421. BS_SCRIPT_EXPORT(n:ClearPhysicsRegions)
  422. virtual void clearBroadPhaseRegions() = 0;
  423. /**
  424. * Returns a maximum edge length before a triangle is tesselated.
  425. *
  426. * @see PhysicsFlags::CCT_Tesselation
  427. */
  428. virtual float getMaxTesselationEdgeLength() const = 0;
  429. /**
  430. * Sets a maximum edge length before a triangle is tesselated.
  431. *
  432. * @see PhysicsFlags::CCT_Tesselation
  433. */
  434. virtual void setMaxTesselationEdgeLength(float length) = 0;
  435. /**
  436. * Enables or disables collision between two layers. Each physics object can be assigned a specific layer, and here
  437. * you can determine which layers can interact with each other.
  438. */
  439. BS_SCRIPT_EXPORT(n:ToggleCollision)
  440. void toggleCollision(UINT64 groupA, UINT64 groupB, bool enabled);
  441. /** Checks if two collision layers are allowed to interact. */
  442. BS_SCRIPT_EXPORT(n:IsCollisionEnabled)
  443. bool isCollisionEnabled(UINT64 groupA, UINT64 groupB) const;
  444. /** @name Internal
  445. * @{
  446. */
  447. /******************************************************************************************************************/
  448. /************************************************* CREATION *******************************************************/
  449. /******************************************************************************************************************/
  450. /** @copydoc PhysicsMaterial::create */
  451. virtual SPtr<PhysicsMaterial> createMaterial(float staticFriction, float dynamicFriction, float restitution) = 0;
  452. /** @copydoc PhysicsMesh::create */
  453. virtual SPtr<PhysicsMesh> createMesh(const SPtr<MeshData>& meshData, PhysicsMeshType type) = 0;
  454. /** @copydoc Rigidbody::create */
  455. virtual SPtr<Rigidbody> createRigidbody(const HSceneObject& linkedSO) = 0;
  456. /** @copydoc BoxCollider::create */
  457. virtual SPtr<BoxCollider> createBoxCollider(const Vector3& extents, const Vector3& position,
  458. const Quaternion& rotation) = 0;
  459. /** @copydoc SphereCollider::create */
  460. virtual SPtr<SphereCollider> createSphereCollider(float radius,
  461. const Vector3& position, const Quaternion& rotation) = 0;
  462. /** @copydoc PlaneCollider::create */
  463. virtual SPtr<PlaneCollider> createPlaneCollider(const Vector3& position, const Quaternion& rotation) = 0;
  464. /** @copydoc CapsuleCollider::create */
  465. virtual SPtr<CapsuleCollider> createCapsuleCollider(float radius, float halfHeight,
  466. const Vector3& position, const Quaternion& rotation) = 0;
  467. /** @copydoc MeshCollider::create */
  468. virtual SPtr<MeshCollider> createMeshCollider(const Vector3& position, const Quaternion& rotation) = 0;
  469. /** @copydoc FixedJoint::create */
  470. virtual SPtr<FixedJoint> createFixedJoint(const FIXED_JOINT_DESC& desc) = 0;
  471. /** @copydoc DistanceJoint::create */
  472. virtual SPtr<DistanceJoint> createDistanceJoint(const DISTANCE_JOINT_DESC& desc) = 0;
  473. /** @copydoc HingeJoint::create */
  474. virtual SPtr<HingeJoint> createHingeJoint(const HINGE_JOINT_DESC& desc) = 0;
  475. /** @copydoc SphericalJoint::create */
  476. virtual SPtr<SphericalJoint> createSphericalJoint(const SPHERICAL_JOINT_DESC& desc) = 0;
  477. /** @copydoc SliderJoint::create */
  478. virtual SPtr<SliderJoint> createSliderJoint(const SLIDER_JOINT_DESC& desc) = 0;
  479. /** @copydoc D6Joint::create */
  480. virtual SPtr<D6Joint> createD6Joint(const D6_JOINT_DESC& desc) = 0;
  481. /** @copydoc CharacterController::create */
  482. virtual SPtr<CharacterController> createCharacterController(const CHAR_CONTROLLER_DESC& desc) = 0;
  483. /** Triggers physics simulation update as needed. Should be called once per frame. */
  484. virtual void update() = 0;
  485. /** @copydoc Physics::boxOverlap() */
  486. virtual Vector<Collider*> _boxOverlap(const AABox& box, const Quaternion& rotation,
  487. UINT64 layer = BS_ALL_LAYERS) const = 0;
  488. /** @copydoc Physics::sphereOverlap() */
  489. virtual Vector<Collider*> _sphereOverlap(const Sphere& sphere, UINT64 layer = BS_ALL_LAYERS) const = 0;
  490. /** @copydoc Physics::capsuleOverlap() */
  491. virtual Vector<Collider*> _capsuleOverlap(const Capsule& capsule, const Quaternion& rotation,
  492. UINT64 layer = BS_ALL_LAYERS) const = 0;
  493. /** @copydoc Physics::convexOverlap() */
  494. virtual Vector<Collider*> _convexOverlap(const HPhysicsMesh& mesh, const Vector3& position,
  495. const Quaternion& rotation, UINT64 layer = BS_ALL_LAYERS) const = 0;
  496. /**
  497. * Checks does the ray hit the provided collider.
  498. *
  499. * @param[in] origin Origin of the ray to check.
  500. * @param[in] unitDir Unit direction of the ray to check.
  501. * @param[in] collider Collider to check for hit.
  502. * @param[out] hit Information about the hit. Valid only if the method returns true.
  503. * @param[in] maxDist Maximum distance from the ray origin to search for hits.
  504. * @return True if the ray has hit the collider.
  505. */
  506. virtual bool _rayCast(const Vector3& origin, const Vector3& unitDir, const Collider& collider, PhysicsQueryHit& hit,
  507. float maxDist = FLT_MAX) const = 0;
  508. /** Checks is the physics simulation update currently in progress. */
  509. BS_SCRIPT_EXPORT(n:IsUpdateInProgress,pr:getter)
  510. bool _isUpdateInProgress() const { return mUpdateInProgress; }
  511. /** @} */
  512. static const UINT64 CollisionMapSize = 64;
  513. protected:
  514. friend class Rigidbody;
  515. mutable Mutex mMutex;
  516. bool mCollisionMap[CollisionMapSize][CollisionMapSize];
  517. bool mUpdateInProgress = false;
  518. PhysicsFlags mFlags;
  519. };
  520. /** Provides easier access to Physics. */
  521. BS_CORE_EXPORT Physics& gPhysics();
  522. /** Contains parameters used for initializing the physics system. */
  523. struct PHYSICS_INIT_DESC
  524. {
  525. float typicalLength = 1.0f; /**< Typical length of an object in the scene. */
  526. float typicalSpeed = 9.81f; /**< Typical speed of an object in the scene. */
  527. Vector3 gravity = Vector3(0.0f, -9.81f, 0.0f); /**< Initial gravity. */
  528. bool initCooking = true; /**< Determines should the cooking library be initialized. */
  529. float timeStep = 1.0f / 60.0f; /**< Determines using what interval should the physics update happen. */
  530. /** Flags that control global physics option. */
  531. PhysicsFlags flags = PhysicsFlag::CCT_OverlapRecovery | PhysicsFlag::CCT_PreciseSweeps | PhysicsFlag::CCD_Enable;
  532. };
  533. /** @} */
  534. }