| 1 | //************************************ bs::framework - Copyright 2018 Marko Pintera **************************************// |
| 2 | //*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********// |
| 3 | #pragma once |
| 4 | |
| 5 | #include <cfloat> |
| 6 | |
| 7 | #include "BsCorePrerequisites.h" |
| 8 | #include "Physics/BsPhysicsCommon.h" |
| 9 | #include "Utility/BsModule.h" |
| 10 | #include "Math/BsVector3.h" |
| 11 | #include "Math/BsVector2.h" |
| 12 | #include "Math/BsQuaternion.h" |
| 13 | |
| 14 | namespace bs |
| 15 | { |
| 16 | /** @addtogroup Physics |
| 17 | * @{ |
| 18 | */ |
| 19 | |
| 20 | struct PHYSICS_INIT_DESC; |
| 21 | class PhysicsScene; |
| 22 | |
| 23 | /** Flags for controlling physics behaviour globally. */ |
| 24 | enum class PhysicsFlag |
| 25 | { |
| 26 | /** |
| 27 | * Automatically recovers character controllers that are interpenetrating geometry. This can happen if a controller |
| 28 | * is spawned or teleported into geometry, its size/rotation is changed so it penetrates geometry, or simply |
| 29 | * because of numerical imprecision. |
| 30 | */ |
| 31 | CCT_OverlapRecovery = 1<<0, |
| 32 | /** |
| 33 | * Performs more accurate sweeps when moving the character controller, making it less likely to interpenetrate |
| 34 | * geometry. When overlap recovery is turned on you can consider turning this off as it can compensate for the |
| 35 | * less precise sweeps. |
| 36 | */ |
| 37 | CCT_PreciseSweeps = 1<<1, |
| 38 | /** |
| 39 | * Large triangles can cause problems with character controller collision. If this option is enabled the triangles |
| 40 | * larger than a certain size will be automatically tesselated into smaller triangles, in order to help with |
| 41 | * precision. |
| 42 | * |
| 43 | * @see Physics::getMaxTesselationEdgeLength |
| 44 | */ |
| 45 | CCT_Tesselation = 1<<2, |
| 46 | /** |
| 47 | * Enables continous collision detection. This will prevent fast-moving objects from tunneling through each other. |
| 48 | * You must also enable CCD for individual Rigidbodies. This option can have a significant performance impact. |
| 49 | */ |
| 50 | CCD_Enable = 1<<3 |
| 51 | }; |
| 52 | |
| 53 | /** @copydoc CharacterCollisionFlag */ |
| 54 | typedef Flags<PhysicsFlag> PhysicsFlags; |
| 55 | BS_FLAGS_OPERATORS(PhysicsFlag) |
| 56 | |
| 57 | /** Provides global physics settings, factory methods for physics objects and scene queries. */ |
| 58 | class BS_CORE_EXPORT BS_SCRIPT_EXPORT(m:Physics) Physics : public Module<Physics> |
| 59 | { |
| 60 | public: |
| 61 | Physics(const PHYSICS_INIT_DESC& init); |
| 62 | virtual ~Physics() = default; |
| 63 | |
| 64 | /******************************************************************************************************************/ |
| 65 | /************************************************* OPTIONS ********************************************************/ |
| 66 | /******************************************************************************************************************/ |
| 67 | |
| 68 | /** Pauses or resumes the physics simulation. */ |
| 69 | virtual void setPaused(bool paused) = 0; |
| 70 | |
| 71 | /** |
| 72 | * Enables or disables collision between two layers. Each physics object can be assigned a specific layer, and here |
| 73 | * you can determine which layers can interact with each other. |
| 74 | */ |
| 75 | BS_SCRIPT_EXPORT(n:ToggleCollision) |
| 76 | void toggleCollision(UINT64 groupA, UINT64 groupB, bool enabled); |
| 77 | |
| 78 | /** Checks if two collision layers are allowed to interact. */ |
| 79 | BS_SCRIPT_EXPORT(n:IsCollisionEnabled) |
| 80 | bool isCollisionEnabled(UINT64 groupA, UINT64 groupB) const; |
| 81 | |
| 82 | /** @name Internal |
| 83 | * @{ |
| 84 | */ |
| 85 | |
| 86 | /******************************************************************************************************************/ |
| 87 | /************************************************* CREATION *******************************************************/ |
| 88 | /******************************************************************************************************************/ |
| 89 | |
| 90 | /** @copydoc PhysicsMaterial::create */ |
| 91 | virtual SPtr<PhysicsMaterial> createMaterial(float staticFriction, float dynamicFriction, float restitution) = 0; |
| 92 | |
| 93 | /** @copydoc PhysicsMesh::create */ |
| 94 | virtual SPtr<PhysicsMesh> createMesh(const SPtr<MeshData>& meshData, PhysicsMeshType type) = 0; |
| 95 | |
| 96 | /** Creates an object representing the physics scene. Must be manually released via destroyPhysicsScene(). */ |
| 97 | virtual SPtr<PhysicsScene> createPhysicsScene() = 0; |
| 98 | |
| 99 | /** |
| 100 | * Updates the physics simulation. In order to maintain stability of the physics calculations this method should |
| 101 | * be called at fixed intervals (e.g. 60 times a second). |
| 102 | * |
| 103 | * @param[in] step Time delta to advance the physics simulation by, in seconds. |
| 104 | */ |
| 105 | virtual void fixedUpdate(float step) = 0; |
| 106 | |
| 107 | /** |
| 108 | * Performs any physics operations that arent tied to the fixed update interval. Should be called once per frame. |
| 109 | */ |
| 110 | virtual void update() { } |
| 111 | |
| 112 | /** Checks is the physics simulation update currently in progress. */ |
| 113 | BS_SCRIPT_EXPORT(n:IsUpdateInProgress,pr:getter) |
| 114 | bool _isUpdateInProgress() const { return mUpdateInProgress; } |
| 115 | |
| 116 | /** |
| 117 | * Checks does the ray hit the provided collider. |
| 118 | * |
| 119 | * @param[in] origin Origin of the ray to check. |
| 120 | * @param[in] unitDir Unit direction of the ray to check. |
| 121 | * @param[in] collider Collider to check for hit. |
| 122 | * @param[out] hit Information about the hit. Valid only if the method returns true. |
| 123 | * @param[in] maxDist Maximum distance from the ray origin to search for hits. |
| 124 | * @return True if the ray has hit the collider. |
| 125 | */ |
| 126 | virtual bool _rayCast(const Vector3& origin, const Vector3& unitDir, const Collider& collider, PhysicsQueryHit& hit, |
| 127 | float maxDist = FLT_MAX) const = 0; |
| 128 | |
| 129 | /** @} */ |
| 130 | |
| 131 | static const UINT64 CollisionMapSize = 64; |
| 132 | protected: |
| 133 | friend class Rigidbody; |
| 134 | |
| 135 | mutable Mutex mMutex; |
| 136 | bool mCollisionMap[CollisionMapSize][CollisionMapSize]; |
| 137 | |
| 138 | bool mUpdateInProgress = false; |
| 139 | }; |
| 140 | |
| 141 | /** Provides easier access to Physics. */ |
| 142 | BS_CORE_EXPORT Physics& gPhysics(); |
| 143 | |
| 144 | /** Contains parameters used for initializing the physics system. */ |
| 145 | struct PHYSICS_INIT_DESC |
| 146 | { |
| 147 | float typicalLength = 1.0f; /**< Typical length of an object in the scene. */ |
| 148 | float typicalSpeed = 9.81f; /**< Typical speed of an object in the scene. */ |
| 149 | Vector3 gravity = Vector3(0.0f, -9.81f, 0.0f); /**< Initial gravity. */ |
| 150 | bool initCooking = true; /**< Determines should the cooking library be initialized. */ |
| 151 | /** Flags that control global physics option. */ |
| 152 | PhysicsFlags flags = PhysicsFlag::CCT_OverlapRecovery | PhysicsFlag::CCT_PreciseSweeps | PhysicsFlag::CCD_Enable; |
| 153 | }; |
| 154 | |
| 155 | /** |
| 156 | * Physical representation of a scene, allowing creation of new physical objects in the scene and queries against |
| 157 | * those objects. Objects created in different scenes cannot physically interact with eachother. |
| 158 | */ |
| 159 | class BS_CORE_EXPORT BS_SCRIPT_EXPORT(m:Physics) PhysicsScene |
| 160 | { |
| 161 | public: |
| 162 | /******************************************************************************************************************/ |
| 163 | /************************************************* QUERIES ********************************************************/ |
| 164 | /******************************************************************************************************************/ |
| 165 | |
| 166 | /** |
| 167 | * Casts a ray into the scene and returns the closest found hit, if any. |
| 168 | * |
| 169 | * @param[in] ray Ray to cast into the scene. |
| 170 | * @param[out] hit Information recorded about a hit. Only valid if method returns true. |
| 171 | * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects. |
| 172 | * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be |
| 173 | * detected. |
| 174 | * @return True if something was hit, false otherwise. |
| 175 | */ |
| 176 | BS_SCRIPT_EXPORT(n:RayCast) |
| 177 | virtual bool rayCast(const Ray& ray, PhysicsQueryHit& hit, UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const; |
| 178 | |
| 179 | /** |
| 180 | * Casts a ray into the scene and returns the closest found hit, if any. |
| 181 | * |
| 182 | * @param[in] origin Origin of the ray to cast into the scene. |
| 183 | * @param[in] unitDir Unit direction of the ray to cast into the scene. |
| 184 | * @param[out] hit Information recorded about a hit. Only valid if method returns true. |
| 185 | * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects. |
| 186 | * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be |
| 187 | * detected. |
| 188 | * @return True if something was hit, false otherwise. |
| 189 | */ |
| 190 | BS_SCRIPT_EXPORT(n:RayCast) |
| 191 | virtual bool rayCast(const Vector3& origin, const Vector3& unitDir, PhysicsQueryHit& hit, |
| 192 | UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0; |
| 193 | |
| 194 | /** |
| 195 | * Performs a sweep into the scene using a box and returns the closest found hit, if any. |
| 196 | * |
| 197 | * @param[in] box Box to sweep through the scene. |
| 198 | * @param[in] rotation Orientation of the box. |
| 199 | * @param[in] unitDir Unit direction towards which to perform the sweep. |
| 200 | * @param[out] hit Information recorded about a hit. Only valid if method returns true. |
| 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 True if something was hit, false otherwise. |
| 205 | */ |
| 206 | BS_SCRIPT_EXPORT(n:BoxCast) |
| 207 | virtual bool boxCast(const AABox& box, const Quaternion& rotation, const Vector3& unitDir, PhysicsQueryHit& hit, |
| 208 | UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0; |
| 209 | |
| 210 | /** |
| 211 | * Performs a sweep into the scene using a sphere and returns the closest found hit, if any. |
| 212 | * |
| 213 | * @param[in] sphere Sphere to sweep through the scene. |
| 214 | * @param[in] unitDir Unit direction towards which to perform the sweep. |
| 215 | * @param[out] hit Information recorded about a hit. Only valid if method returns true. |
| 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 | BS_SCRIPT_EXPORT(n:SphereCast) |
| 222 | virtual bool sphereCast(const Sphere& sphere, const Vector3& unitDir, PhysicsQueryHit& hit, |
| 223 | UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0; |
| 224 | |
| 225 | /** |
| 226 | * Performs a sweep into the scene using a capsule and returns the closest found hit, if any. |
| 227 | * |
| 228 | * @param[in] capsule Capsule to sweep through the scene. |
| 229 | * @param[in] rotation Orientation of the capsule. |
| 230 | * @param[in] unitDir Unit direction towards which to perform the sweep. |
| 231 | * @param[out] hit Information recorded about a hit. Only valid if method returns true. |
| 232 | * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects. |
| 233 | * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be |
| 234 | * detected. |
| 235 | * @return True if something was hit, false otherwise. |
| 236 | */ |
| 237 | BS_SCRIPT_EXPORT(n:CapsuleCast) |
| 238 | virtual bool capsuleCast(const Capsule& capsule, const Quaternion& rotation, const Vector3& unitDir, |
| 239 | PhysicsQueryHit& hit, UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0; |
| 240 | |
| 241 | /** |
| 242 | * Performs a sweep into the scene using a convex mesh and returns the closest found hit, if any. |
| 243 | * |
| 244 | * @param[in] mesh Mesh to sweep through the scene. Must be convex. |
| 245 | * @param[in] position Starting position of the mesh. |
| 246 | * @param[in] rotation Orientation of the mesh. |
| 247 | * @param[in] unitDir Unit direction towards which to perform the sweep. |
| 248 | * @param[out] hit Information recorded about a hit. Only valid if method returns true. |
| 249 | * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects. |
| 250 | * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be |
| 251 | * detected. |
| 252 | * @return True if something was hit, false otherwise. |
| 253 | */ |
| 254 | BS_SCRIPT_EXPORT(n:ConvexCast) |
| 255 | virtual bool convexCast(const HPhysicsMesh& mesh, const Vector3& position, const Quaternion& rotation, |
| 256 | const Vector3& unitDir, PhysicsQueryHit& hit, UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0; |
| 257 | |
| 258 | /** |
| 259 | * Casts a ray into the scene and returns all found hits. |
| 260 | * |
| 261 | * @param[in] ray Ray to cast into the scene. |
| 262 | * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects. |
| 263 | * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be |
| 264 | * detected. |
| 265 | * @return List of all detected hits. |
| 266 | */ |
| 267 | BS_SCRIPT_EXPORT(n:RayCastAll) |
| 268 | virtual Vector<PhysicsQueryHit> rayCastAll(const Ray& ray, UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const; |
| 269 | |
| 270 | /** |
| 271 | * Casts a ray into the scene and returns all found hits. |
| 272 | * |
| 273 | * @param[in] origin Origin of the ray to cast into the scene. |
| 274 | * @param[in] unitDir Unit direction of the ray to cast into the scene. |
| 275 | * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects. |
| 276 | * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be |
| 277 | * detected. |
| 278 | * @return List of all detected hits. |
| 279 | */ |
| 280 | BS_SCRIPT_EXPORT(n:RayCastAll) |
| 281 | virtual Vector<PhysicsQueryHit> rayCastAll(const Vector3& origin, const Vector3& unitDir, |
| 282 | UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0; |
| 283 | |
| 284 | /** |
| 285 | * Performs a sweep into the scene using a box and returns all found hits. |
| 286 | * |
| 287 | * @param[in] box Box to sweep through the scene. |
| 288 | * @param[in] rotation Orientation of the box. |
| 289 | * @param[in] unitDir Unit direction towards which to perform the sweep. |
| 290 | * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects. |
| 291 | * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be |
| 292 | * detected. |
| 293 | * @return List of all detected hits. |
| 294 | */ |
| 295 | BS_SCRIPT_EXPORT(n:BoxCastAll) |
| 296 | virtual Vector<PhysicsQueryHit> boxCastAll(const AABox& box, const Quaternion& rotation, |
| 297 | const Vector3& unitDir, UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0; |
| 298 | |
| 299 | /** |
| 300 | * Performs a sweep into the scene using a sphere and returns all found hits. |
| 301 | * |
| 302 | * @param[in] sphere Sphere to sweep through the scene. |
| 303 | * @param[in] unitDir Unit direction towards which to perform the sweep. |
| 304 | * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects. |
| 305 | * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be |
| 306 | * detected. |
| 307 | * @return List of all detected hits. |
| 308 | */ |
| 309 | BS_SCRIPT_EXPORT(n:SphereCastAll) |
| 310 | virtual Vector<PhysicsQueryHit> sphereCastAll(const Sphere& sphere, const Vector3& unitDir, |
| 311 | UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0; |
| 312 | |
| 313 | /** |
| 314 | * Performs a sweep into the scene using a capsule and returns all found hits. |
| 315 | * |
| 316 | * @param[in] capsule Capsule to sweep through the scene. |
| 317 | * @param[in] rotation Orientation of the capsule. |
| 318 | * @param[in] unitDir Unit direction towards which to perform the sweep. |
| 319 | * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects. |
| 320 | * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be |
| 321 | * detected. |
| 322 | * @return List of all detected hits. |
| 323 | */ |
| 324 | BS_SCRIPT_EXPORT(n:CapsuleCastAll) |
| 325 | virtual Vector<PhysicsQueryHit> capsuleCastAll(const Capsule& capsule, const Quaternion& rotation, |
| 326 | const Vector3& unitDir, UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0; |
| 327 | |
| 328 | /** |
| 329 | * Performs a sweep into the scene using a convex mesh and returns all found hits. |
| 330 | * |
| 331 | * @param[in] mesh Mesh to sweep through the scene. Must be convex. |
| 332 | * @param[in] position Starting position of the mesh. |
| 333 | * @param[in] rotation Orientation of the mesh. |
| 334 | * @param[in] unitDir Unit direction towards which to perform the sweep. |
| 335 | * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects. |
| 336 | * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be |
| 337 | * detected. |
| 338 | * @return List of all detected hits. |
| 339 | */ |
| 340 | BS_SCRIPT_EXPORT(n:ConvexCastAll) |
| 341 | virtual Vector<PhysicsQueryHit> convexCastAll(const HPhysicsMesh& mesh, const Vector3& position, |
| 342 | const Quaternion& rotation, const Vector3& unitDir, UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0; |
| 343 | |
| 344 | /** |
| 345 | * Casts a ray into the scene and checks if it has hit anything. This can be significantly more efficient than other |
| 346 | * types of cast* calls. |
| 347 | * |
| 348 | * @param[in] ray Ray to cast into the scene. |
| 349 | * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects. |
| 350 | * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be |
| 351 | * detected. |
| 352 | * @return True if something was hit, false otherwise. |
| 353 | */ |
| 354 | BS_SCRIPT_EXPORT(n:RayCastAny) |
| 355 | virtual bool rayCastAny(const Ray& ray, UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const; |
| 356 | |
| 357 | /** |
| 358 | * Casts a ray into the scene and checks if it has hit anything. This can be significantly more efficient than other |
| 359 | * types of cast* calls. |
| 360 | * |
| 361 | * @param[in] origin Origin of the ray to cast into the scene. |
| 362 | * @param[in] unitDir Unit direction of the ray to cast into the scene. |
| 363 | * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects. |
| 364 | * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be |
| 365 | * detected. |
| 366 | * @return True if something was hit, false otherwise. |
| 367 | */ |
| 368 | BS_SCRIPT_EXPORT(n:RayCastAny) |
| 369 | virtual bool rayCastAny(const Vector3& origin, const Vector3& unitDir, |
| 370 | UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0; |
| 371 | |
| 372 | /** |
| 373 | * Performs a sweep into the scene using a box and checks if it has hit anything. This can be significantly more |
| 374 | * efficient than other types of cast* calls. |
| 375 | * |
| 376 | * @param[in] box Box to sweep through the scene. |
| 377 | * @param[in] rotation Orientation of the box. |
| 378 | * @param[in] unitDir Unit direction towards which to perform the sweep. |
| 379 | * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects. |
| 380 | * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be |
| 381 | * detected. |
| 382 | * @return True if something was hit, false otherwise. |
| 383 | */ |
| 384 | BS_SCRIPT_EXPORT(n:BoxCastAny) |
| 385 | virtual bool boxCastAny(const AABox& box, const Quaternion& rotation, const Vector3& unitDir, |
| 386 | UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0; |
| 387 | |
| 388 | /** |
| 389 | * Performs a sweep into the scene using a sphere and checks if it has hit anything. This can be significantly more |
| 390 | * efficient than other types of cast* calls. |
| 391 | * |
| 392 | * @param[in] sphere Sphere to sweep through the scene. |
| 393 | * @param[in] unitDir Unit direction towards which to perform the sweep. |
| 394 | * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects. |
| 395 | * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be |
| 396 | * detected. |
| 397 | * @return True if something was hit, false otherwise. |
| 398 | */ |
| 399 | BS_SCRIPT_EXPORT(n:SphereCastAny) |
| 400 | virtual bool sphereCastAny(const Sphere& sphere, const Vector3& unitDir, |
| 401 | UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0; |
| 402 | |
| 403 | /** |
| 404 | * Performs a sweep into the scene using a capsule and checks if it has hit anything. This can be significantly more |
| 405 | * efficient than other types of cast* calls. |
| 406 | * |
| 407 | * @param[in] capsule Capsule to sweep through the scene. |
| 408 | * @param[in] rotation Orientation of the capsule. |
| 409 | * @param[in] unitDir Unit direction towards which to perform the sweep. |
| 410 | * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects. |
| 411 | * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be |
| 412 | * detected. |
| 413 | * @return True if something was hit, false otherwise. |
| 414 | */ |
| 415 | BS_SCRIPT_EXPORT(n:CapsuleCastAny) |
| 416 | virtual bool capsuleCastAny(const Capsule& capsule, const Quaternion& rotation, const Vector3& unitDir, |
| 417 | UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0; |
| 418 | |
| 419 | /** |
| 420 | * Performs a sweep into the scene using a convex mesh and checks if it has hit anything. This can be significantly |
| 421 | * more efficient than other types of cast* calls. |
| 422 | * |
| 423 | * @param[in] mesh Mesh to sweep through the scene. Must be convex. |
| 424 | * @param[in] position Starting position of the mesh. |
| 425 | * @param[in] rotation Orientation of the mesh. |
| 426 | * @param[in] unitDir Unit direction towards which to perform the sweep. |
| 427 | * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects. |
| 428 | * @param[in] max Maximum distance at which to perform the query. Hits past this distance will not be |
| 429 | * detected. |
| 430 | * @return True if something was hit, false otherwise. |
| 431 | */ |
| 432 | BS_SCRIPT_EXPORT(n:ConvexCastAny) |
| 433 | virtual bool convexCastAny(const HPhysicsMesh& mesh, const Vector3& position, const Quaternion& rotation, |
| 434 | const Vector3& unitDir, UINT64 layer = BS_ALL_LAYERS, float max = FLT_MAX) const = 0; |
| 435 | |
| 436 | /** |
| 437 | * Returns a list of all colliders in the scene that overlap the provided box. |
| 438 | * |
| 439 | * @param[in] box Box to check for overlap. |
| 440 | * @param[in] rotation Orientation of the box. |
| 441 | * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects. |
| 442 | * @return List of all colliders that overlap the box. |
| 443 | */ |
| 444 | BS_SCRIPT_EXPORT(n:BoxOverlap) |
| 445 | virtual Vector<HCollider> boxOverlap(const AABox& box, const Quaternion& rotation, |
| 446 | UINT64 layer = BS_ALL_LAYERS) const; |
| 447 | |
| 448 | /** |
| 449 | * Returns a list of all colliders in the scene that overlap the provided sphere. |
| 450 | * |
| 451 | * @param[in] sphere Sphere to check for overlap. |
| 452 | * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects. |
| 453 | * @return List of all colliders that overlap the sphere. |
| 454 | */ |
| 455 | BS_SCRIPT_EXPORT(n:SphereOverlap) |
| 456 | virtual Vector<HCollider> sphereOverlap(const Sphere& sphere, UINT64 layer = BS_ALL_LAYERS) const; |
| 457 | |
| 458 | /** |
| 459 | * Returns a list of all colliders in the scene that overlap the provided capsule. |
| 460 | * |
| 461 | * @param[in] capsule Capsule to check for overlap. |
| 462 | * @param[in] rotation Orientation of the capsule. |
| 463 | * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects. |
| 464 | * @return List of all colliders that overlap the capsule. |
| 465 | */ |
| 466 | BS_SCRIPT_EXPORT(n:CapsuleOverlap) |
| 467 | virtual Vector<HCollider> capsuleOverlap(const Capsule& capsule, const Quaternion& rotation, |
| 468 | UINT64 layer = BS_ALL_LAYERS) const; |
| 469 | |
| 470 | /** |
| 471 | * Returns a list of all colliders in the scene that overlap the provided convex mesh. |
| 472 | * |
| 473 | * @param[in] mesh Mesh to check for overlap. Must be convex. |
| 474 | * @param[in] position Position of the mesh. |
| 475 | * @param[in] rotation Orientation of the mesh. |
| 476 | * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects. |
| 477 | * @return List of all colliders that overlap the mesh. |
| 478 | */ |
| 479 | BS_SCRIPT_EXPORT(n:ConvexOverlap) |
| 480 | virtual Vector<HCollider> convexOverlap(const HPhysicsMesh& mesh, const Vector3& position, |
| 481 | const Quaternion& rotation, UINT64 layer = BS_ALL_LAYERS) const; |
| 482 | |
| 483 | /** |
| 484 | * Checks if the provided box overlaps any other collider in the scene. |
| 485 | * |
| 486 | * @param[in] box Box to check for overlap. |
| 487 | * @param[in] rotation Orientation of the box. |
| 488 | * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects. |
| 489 | * @return True if there is overlap with another object, false otherwise. |
| 490 | */ |
| 491 | BS_SCRIPT_EXPORT(n:BoxOverlapAny) |
| 492 | virtual bool boxOverlapAny(const AABox& box, const Quaternion& rotation, UINT64 layer = BS_ALL_LAYERS) const = 0; |
| 493 | |
| 494 | /** |
| 495 | * Checks if the provided sphere overlaps any other collider in the scene. |
| 496 | * |
| 497 | * @param[in] sphere Sphere to check for overlap. |
| 498 | * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects. |
| 499 | * @return True if there is overlap with another object, false otherwise. |
| 500 | */ |
| 501 | BS_SCRIPT_EXPORT(n:SphereOverlapAny) |
| 502 | virtual bool sphereOverlapAny(const Sphere& sphere, UINT64 layer = BS_ALL_LAYERS) const = 0; |
| 503 | |
| 504 | /** |
| 505 | * Checks if the provided capsule overlaps any other collider in the scene. |
| 506 | * |
| 507 | * @param[in] capsule Capsule to check for overlap. |
| 508 | * @param[in] rotation Orientation of the capsule. |
| 509 | * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects. |
| 510 | * @return True if there is overlap with another object, false otherwise. |
| 511 | */ |
| 512 | BS_SCRIPT_EXPORT(n:CapsuleOverlapAny) |
| 513 | virtual bool capsuleOverlapAny(const Capsule& capsule, const Quaternion& rotation, |
| 514 | UINT64 layer = BS_ALL_LAYERS) const = 0; |
| 515 | |
| 516 | /** |
| 517 | * Checks if the provided convex mesh overlaps any other collider in the scene. |
| 518 | * |
| 519 | * @param[in] mesh Mesh to check for overlap. Must be convex. |
| 520 | * @param[in] position Position of the mesh. |
| 521 | * @param[in] rotation Orientation of the mesh. |
| 522 | * @param[in] layer Layers to consider for the query. This allows you to ignore certain groups of objects. |
| 523 | * @return True if there is overlap with another object, false otherwise. |
| 524 | */ |
| 525 | BS_SCRIPT_EXPORT(n:ConvexOverlapAny) |
| 526 | virtual bool convexOverlapAny(const HPhysicsMesh& mesh, const Vector3& position, const Quaternion& rotation, |
| 527 | UINT64 layer = BS_ALL_LAYERS) const = 0; |
| 528 | |
| 529 | /******************************************************************************************************************/ |
| 530 | /************************************************* OPTIONS ********************************************************/ |
| 531 | /******************************************************************************************************************/ |
| 532 | |
| 533 | /** Checks is a specific physics option enabled. */ |
| 534 | virtual bool hasFlag(PhysicsFlags flag) const { return mFlags & flag; } |
| 535 | |
| 536 | /** Enables or disabled a specific physics option. */ |
| 537 | virtual void setFlag(PhysicsFlags flag, bool enabled) { if (enabled) mFlags |= flag; else mFlags &= ~flag; } |
| 538 | |
| 539 | /** |
| 540 | * Returns a maximum edge length before a triangle is tesselated. |
| 541 | * |
| 542 | * @see PhysicsFlags::CCT_Tesselation |
| 543 | */ |
| 544 | virtual float getMaxTesselationEdgeLength() const = 0; |
| 545 | |
| 546 | /** |
| 547 | * Sets a maximum edge length before a triangle is tesselated. |
| 548 | * |
| 549 | * @see PhysicsFlags::CCT_Tesselation |
| 550 | */ |
| 551 | virtual void setMaxTesselationEdgeLength(float length) = 0; |
| 552 | |
| 553 | /** @copydoc setGravity() */ |
| 554 | BS_SCRIPT_EXPORT(n:Gravity,pr:getter) |
| 555 | virtual Vector3 getGravity() const = 0; |
| 556 | |
| 557 | /** Determines the global gravity value for all objects in the scene. */ |
| 558 | BS_SCRIPT_EXPORT(n:Gravity,pr:setter) |
| 559 | virtual void setGravity(const Vector3& gravity) = 0; |
| 560 | |
| 561 | /** |
| 562 | * Adds a new physics region. Certain physics options require you to set up regions in which physics objects are |
| 563 | * allowed to be in, and objects outside of these regions will not be handled by physics. You do not need to set |
| 564 | * up these regions by default. |
| 565 | */ |
| 566 | BS_SCRIPT_EXPORT(n:AddPhysicsRegion) |
| 567 | virtual UINT32 addBroadPhaseRegion(const AABox& region) = 0; |
| 568 | |
| 569 | /** Removes a physics region. */ |
| 570 | BS_SCRIPT_EXPORT(n:RemovePhysicsRegion) |
| 571 | virtual void removeBroadPhaseRegion(UINT32 handle) = 0; |
| 572 | |
| 573 | /** Removes all physics regions. */ |
| 574 | BS_SCRIPT_EXPORT(n:ClearPhysicsRegions) |
| 575 | virtual void clearBroadPhaseRegions() = 0; |
| 576 | |
| 577 | /** @name Internal |
| 578 | * @{ |
| 579 | */ |
| 580 | |
| 581 | /******************************************************************************************************************/ |
| 582 | /************************************************* CREATION *******************************************************/ |
| 583 | /******************************************************************************************************************/ |
| 584 | |
| 585 | /** @copydoc Rigidbody::create */ |
| 586 | virtual SPtr<Rigidbody> createRigidbody(const HSceneObject& linkedSO) = 0; |
| 587 | |
| 588 | /** |
| 589 | * Creates a new box collider. |
| 590 | * |
| 591 | * @param[in] extents Extents (half size) of the box. |
| 592 | * @param[in] position Center of the box. |
| 593 | * @param[in] rotation Rotation of the box. |
| 594 | */ |
| 595 | virtual SPtr<BoxCollider> createBoxCollider(const Vector3& extents, const Vector3& position, |
| 596 | const Quaternion& rotation) = 0; |
| 597 | |
| 598 | /** |
| 599 | * Creates a new sphere collider. |
| 600 | * |
| 601 | * @param[in] radius Radius of the sphere geometry. |
| 602 | * @param[in] position Position of the collider. |
| 603 | * @param[in] rotation Rotation of the collider. |
| 604 | */ |
| 605 | virtual SPtr<SphereCollider> createSphereCollider(float radius, |
| 606 | const Vector3& position, const Quaternion& rotation) = 0; |
| 607 | |
| 608 | /** |
| 609 | * Creates a new plane collider. |
| 610 | * |
| 611 | * @param[in] position Position of the collider. |
| 612 | * @param[in] rotation Rotation of the collider. |
| 613 | */ |
| 614 | virtual SPtr<PlaneCollider> createPlaneCollider(const Vector3& position, const Quaternion& rotation) = 0; |
| 615 | |
| 616 | /** |
| 617 | * Creates a new capsule collider. |
| 618 | * |
| 619 | * @param[in] radius Radius of the capsule. |
| 620 | * @param[in] halfHeight Half height of the capsule, from the origin to one of the hemispherical centers, along |
| 621 | * the normal vector. |
| 622 | * @param[in] position Center of the box. |
| 623 | * @param[in] rotation Rotation of the box. |
| 624 | */ |
| 625 | virtual SPtr<CapsuleCollider> createCapsuleCollider(float radius, float halfHeight, |
| 626 | const Vector3& position, const Quaternion& rotation) = 0; |
| 627 | |
| 628 | /** |
| 629 | * Creates a new mesh collider. |
| 630 | * |
| 631 | * @param[in] position Position of the collider. |
| 632 | * @param[in] rotation Rotation of the collider. |
| 633 | */ |
| 634 | virtual SPtr<MeshCollider> createMeshCollider(const Vector3& position, const Quaternion& rotation) = 0; |
| 635 | |
| 636 | /** |
| 637 | * Creates a new fixed joint. |
| 638 | * |
| 639 | * @param[in] desc Settings describing the joint. |
| 640 | */ |
| 641 | virtual SPtr<FixedJoint> createFixedJoint(const FIXED_JOINT_DESC& desc) = 0; |
| 642 | |
| 643 | /** |
| 644 | * Creates a new distance joint. |
| 645 | * |
| 646 | * @param[in] desc Settings describing the joint. |
| 647 | */ |
| 648 | virtual SPtr<DistanceJoint> createDistanceJoint(const DISTANCE_JOINT_DESC& desc) = 0; |
| 649 | |
| 650 | /** |
| 651 | * Creates a new hinge joint. |
| 652 | * |
| 653 | * @param[in] desc Settings describing the joint. |
| 654 | */ |
| 655 | virtual SPtr<HingeJoint> createHingeJoint(const HINGE_JOINT_DESC& desc) = 0; |
| 656 | |
| 657 | /** |
| 658 | * Creates a new spherical joint. |
| 659 | * |
| 660 | * @param[in] desc Settings describing the joint. |
| 661 | */ |
| 662 | virtual SPtr<SphericalJoint> createSphericalJoint(const SPHERICAL_JOINT_DESC& desc) = 0; |
| 663 | |
| 664 | /** |
| 665 | * Creates a new spherical joint. |
| 666 | * |
| 667 | * @param[in] desc Settings describing the joint. |
| 668 | */ |
| 669 | virtual SPtr<SliderJoint> createSliderJoint(const SLIDER_JOINT_DESC& desc) = 0; |
| 670 | |
| 671 | /** |
| 672 | * Creates a new D6 joint. |
| 673 | * |
| 674 | * @param[in] desc Settings describing the joint. |
| 675 | */ |
| 676 | virtual SPtr<D6Joint> createD6Joint(const D6_JOINT_DESC& desc) = 0; |
| 677 | |
| 678 | /** |
| 679 | * Creates a new character controller. |
| 680 | * |
| 681 | * @param[in] desc Describes controller geometry and movement. |
| 682 | */ |
| 683 | virtual SPtr<CharacterController> createCharacterController(const CHAR_CONTROLLER_DESC& desc) = 0; |
| 684 | |
| 685 | /** @copydoc PhysicsScene::boxOverlap() */ |
| 686 | virtual Vector<Collider*> _boxOverlap(const AABox& box, const Quaternion& rotation, |
| 687 | UINT64 layer = BS_ALL_LAYERS) const = 0; |
| 688 | |
| 689 | /** @copydoc PhysicsScene::sphereOverlap() */ |
| 690 | virtual Vector<Collider*> _sphereOverlap(const Sphere& sphere, UINT64 layer = BS_ALL_LAYERS) const = 0; |
| 691 | |
| 692 | /** @copydoc PhysicsScene::capsuleOverlap() */ |
| 693 | virtual Vector<Collider*> _capsuleOverlap(const Capsule& capsule, const Quaternion& rotation, |
| 694 | UINT64 layer = BS_ALL_LAYERS) const = 0; |
| 695 | |
| 696 | /** @copydoc PhysicsScene::convexOverlap() */ |
| 697 | virtual Vector<Collider*> _convexOverlap(const HPhysicsMesh& mesh, const Vector3& position, |
| 698 | const Quaternion& rotation, UINT64 layer = BS_ALL_LAYERS) const = 0; |
| 699 | |
| 700 | /** @} */ |
| 701 | protected: |
| 702 | PhysicsScene() = default; |
| 703 | virtual ~PhysicsScene() = default; |
| 704 | |
| 705 | PhysicsFlags mFlags; |
| 706 | }; |
| 707 | |
| 708 | /** @} */ |
| 709 | } |
| 710 | |