| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#pragma once |
| 6 |
|
| 7 |
#include <Jolt/Physics/Body/BodyInterface.h> |
| 8 |
#include <Jolt/Physics/Collision/NarrowPhaseQuery.h> |
| 9 |
#include <Jolt/Physics/Collision/ContactListener.h> |
| 10 |
#include <Jolt/Physics/Constraints/ContactConstraintManager.h> |
| 11 |
#include <Jolt/Physics/Constraints/ConstraintManager.h> |
| 12 |
#include <Jolt/Physics/IslandBuilder.h> |
| 13 |
#include <Jolt/Physics/LargeIslandSplitter.h> |
| 14 |
#include <Jolt/Physics/PhysicsUpdateContext.h> |
| 15 |
#include <Jolt/Physics/PhysicsSettings.h> |
| 16 |
|
| 17 |
JPH_NAMESPACE_BEGIN |
| 18 |
|
| 19 |
class JobSystem; |
| 20 |
class StateRecorder; |
| 21 |
class TempAllocator; |
| 22 |
class PhysicsStepListener; |
| 23 |
class SoftBodyContactListener; |
| 24 |
class SimShapeFilter; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
class JPH_EXPORT PhysicsSystem : public NonCopyable |
| 30 |
{ |
| 31 |
public: |
| 32 |
JPH_OVERRIDE_NEW_DELETE |
| 33 |
|
| 34 |
|
| 35 |
PhysicsSystem() : mContactManager(mPhysicsSettings) JPH_IF_ENABLE_ASSERTS(, mConstraintManager(&mBodyManager)) { } |
| 36 |
~PhysicsSystem(); |
| 37 |
|
| 38 |
|
| 39 |
static constexpr uint cMaxBodiesLimit = BodyID::cMaxBodyIndex + 1; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
static constexpr uint cMaxBodyPairsLimit = ContactConstraintManager::cMaxBodyPairsLimit; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
static constexpr uint cMaxContactConstraintsLimit = ContactConstraintManager::cMaxContactConstraintsLimit; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
void Init(uint inMaxBodies, uint inNumBodyMutexes, uint inMaxBodyPairs, uint inMaxContactConstraints, const BroadPhaseLayerInterface &inBroadPhaseLayerInterface, const ObjectVsBroadPhaseLayerFilter &inObjectVsBroadPhaseLayerFilter, const ObjectLayerPairFilter &inObjectLayerPairFilter); |
| 60 |
|
| 61 |
|
| 62 |
void SetBodyActivationListener(BodyActivationListener *inListener) { mBodyManager.SetBodyActivationListener(inListener); } |
| 63 |
BodyActivationListener * GetBodyActivationListener() const { return mBodyManager.GetBodyActivationListener(); } |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
void SetContactListener(ContactListener *inListener) { mContactManager.SetContactListener(inListener); } |
| 68 |
ContactListener * GetContactListener() const { return mContactManager.GetContactListener(); } |
| 69 |
|
| 70 |
|
| 71 |
void SetSoftBodyContactListener(SoftBodyContactListener *inListener) { mSoftBodyContactListener = inListener; } |
| 72 |
SoftBodyContactListener * GetSoftBodyContactListener() const { return mSoftBodyContactListener; } |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
void SetCombineFriction(ContactConstraintManager::CombineFunction inCombineFriction) { mContactManager.SetCombineFriction(inCombineFriction); } |
| 77 |
ContactConstraintManager::CombineFunction GetCombineFriction() const { return mContactManager.GetCombineFriction(); } |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
void SetCombineRestitution(ContactConstraintManager::CombineFunction inCombineRestitution) { mContactManager.SetCombineRestitution(inCombineRestitution); } |
| 82 |
ContactConstraintManager::CombineFunction GetCombineRestitution() const { return mContactManager.GetCombineRestitution(); } |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
void SetSimShapeFilter(const SimShapeFilter *inShapeFilter) { mSimShapeFilter = inShapeFilter; } |
| 90 |
const SimShapeFilter * GetSimShapeFilter() const { return mSimShapeFilter; } |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
using SimCollideBodyVsBody = std::function<void(const Body &inBody1, const Body &inBody2, Mat44Arg inCenterOfMassTransform1, Mat44Arg inCenterOfMassTransform2, CollideShapeSettings &ioCollideShapeSettings, CollideShapeCollector &ioCollector, const ShapeFilter &inShapeFilter)>; |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
void SetSimCollideBodyVsBody(const SimCollideBodyVsBody &inBodyVsBody) { mSimCollideBodyVsBody = inBodyVsBody; } |
| 105 |
const SimCollideBodyVsBody &GetSimCollideBodyVsBody() const { return mSimCollideBodyVsBody; } |
| 106 |
|
| 107 |
|
| 108 |
static void sDefaultSimCollideBodyVsBody(const Body &inBody1, const Body &inBody2, Mat44Arg inCenterOfMassTransform1, Mat44Arg inCenterOfMassTransform2, CollideShapeSettings &ioCollideShapeSettings, CollideShapeCollector &ioCollector, const ShapeFilter &inShapeFilter); |
| 109 |
|
| 110 |
|
| 111 |
void SetPhysicsSettings(const PhysicsSettings &inSettings) { mPhysicsSettings = inSettings; } |
| 112 |
const PhysicsSettings & GetPhysicsSettings() const { return mPhysicsSettings; } |
| 113 |
|
| 114 |
|
| 115 |
const BodyInterface & GetBodyInterface() const { return mBodyInterfaceLocking; } |
| 116 |
BodyInterface & GetBodyInterface() { return mBodyInterfaceLocking; } |
| 117 |
const BodyInterface & GetBodyInterfaceNoLock() const { return mBodyInterfaceNoLock; } |
| 118 |
BodyInterface & GetBodyInterfaceNoLock() { return mBodyInterfaceNoLock; } |
| 119 |
|
| 120 |
|
| 121 |
const BroadPhaseQuery & GetBroadPhaseQuery() const { return *mBroadPhase; } |
| 122 |
|
| 123 |
|
| 124 |
const NarrowPhaseQuery & GetNarrowPhaseQuery() const { return mNarrowPhaseQueryLocking; } |
| 125 |
const NarrowPhaseQuery & GetNarrowPhaseQueryNoLock() const { return mNarrowPhaseQueryNoLock; } |
| 126 |
|
| 127 |
|
| 128 |
void AddConstraint(Constraint *inConstraint) { mConstraintManager.Add(&inConstraint, 1); } |
| 129 |
|
| 130 |
|
| 131 |
void RemoveConstraint(Constraint *inConstraint) { mConstraintManager.Remove(&inConstraint, 1); } |
| 132 |
|
| 133 |
|
| 134 |
void AddConstraints(Constraint **inConstraints, int inNumber) { mConstraintManager.Add(inConstraints, inNumber); } |
| 135 |
|
| 136 |
|
| 137 |
void RemoveConstraints(Constraint **inConstraints, int inNumber) { mConstraintManager.Remove(inConstraints, inNumber); } |
| 138 |
|
| 139 |
|
| 140 |
Constraints GetConstraints() const { return mConstraintManager.GetConstraints(); } |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
void OptimizeBroadPhase(); |
| 149 |
|
| 150 |
|
| 151 |
void AddStepListener(PhysicsStepListener *inListener); |
| 152 |
|
| 153 |
|
| 154 |
void RemoveStepListener(PhysicsStepListener *inListener); |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
EPhysicsUpdateError Update(float inDeltaTime, int inCollisionSteps, TempAllocator *inTempAllocator, JobSystem *inJobSystem); |
| 163 |
|
| 164 |
|
| 165 |
void SaveState(StateRecorder &inStream, EStateRecorderState inState = EStateRecorderState::All, const StateRecorderFilter *inFilter = nullptr) const; |
| 166 |
|
| 167 |
|
| 168 |
bool RestoreState(StateRecorder &inStream, const StateRecorderFilter *inFilter = nullptr); |
| 169 |
|
| 170 |
|
| 171 |
void SaveBodyState(const Body &inBody, StateRecorder &inStream) const; |
| 172 |
|
| 173 |
|
| 174 |
void RestoreBodyState(Body &ioBody, StateRecorder &inStream); |
| 175 |
|
| 176 |
#ifdef JPH_DEBUG_RENDERER |
| 177 |
|
| 178 |
static bool sDrawMotionQualityLinearCast; |
| 179 |
|
| 180 |
|
| 181 |
void DrawBodies(const BodyManager::DrawSettings &inSettings, DebugRenderer *inRenderer, const BodyDrawFilter *inBodyFilter = nullptr) { mBodyManager.Draw(inSettings, mPhysicsSettings, inRenderer, inBodyFilter); } |
| 182 |
|
| 183 |
|
| 184 |
void DrawConstraints(DebugRenderer *inRenderer) { mConstraintManager.DrawConstraints(inRenderer); } |
| 185 |
|
| 186 |
|
| 187 |
void DrawConstraintLimits(DebugRenderer *inRenderer) { mConstraintManager.DrawConstraintLimits(inRenderer); } |
| 188 |
|
| 189 |
|
| 190 |
void DrawConstraintReferenceFrame(DebugRenderer *inRenderer) { mConstraintManager.DrawConstraintReferenceFrame(inRenderer); } |
| 191 |
#endif |
| 192 |
|
| 193 |
|
| 194 |
void SetGravity(Vec3Arg inGravity) { mGravity = inGravity; } |
| 195 |
Vec3 GetGravity() const { return mGravity; } |
| 196 |
|
| 197 |
|
| 198 |
inline const BodyLockInterfaceNoLock & GetBodyLockInterfaceNoLock() const { return mBodyLockInterfaceNoLock; } |
| 199 |
|
| 200 |
|
| 201 |
inline const BodyLockInterfaceLocking & GetBodyLockInterface() const { return mBodyLockInterfaceLocking; } |
| 202 |
|
| 203 |
|
| 204 |
const ObjectVsBroadPhaseLayerFilter &GetObjectVsBroadPhaseLayerFilter() const { return *mObjectVsBroadPhaseLayerFilter; } |
| 205 |
|
| 206 |
|
| 207 |
const ObjectLayerPairFilter &GetObjectLayerPairFilter() const { return *mObjectLayerPairFilter; } |
| 208 |
|
| 209 |
|
| 210 |
DefaultBroadPhaseLayerFilter GetDefaultBroadPhaseLayerFilter(ObjectLayer inLayer) const { return DefaultBroadPhaseLayerFilter(*mObjectVsBroadPhaseLayerFilter, inLayer); } |
| 211 |
|
| 212 |
|
| 213 |
DefaultObjectLayerFilter GetDefaultLayerFilter(ObjectLayer inLayer) const { return DefaultObjectLayerFilter(*mObjectLayerPairFilter, inLayer); } |
| 214 |
|
| 215 |
|
| 216 |
uint GetNumBodies() const { return mBodyManager.GetNumBodies(); } |
| 217 |
|
| 218 |
|
| 219 |
uint32 GetNumActiveBodies(EBodyType inType) const { return mBodyManager.GetNumActiveBodies(inType); } |
| 220 |
|
| 221 |
|
| 222 |
uint GetMaxBodies() const { return mBodyManager.GetMaxBodies(); } |
| 223 |
|
| 224 |
|
| 225 |
using BodyStats = BodyManager::BodyStats; |
| 226 |
|
| 227 |
|
| 228 |
BodyStats GetBodyStats() const { return mBodyManager.GetBodyStats(); } |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
void GetBodies(BodyIDVector &outBodyIDs) const { return mBodyManager.GetBodyIDs(outBodyIDs); } |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
void GetActiveBodies(EBodyType inType, BodyIDVector &outBodyIDs) const { return mBodyManager.GetActiveBodies(inType, outBodyIDs); } |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
const BodyID * GetActiveBodiesUnsafe(EBodyType inType) const { return mBodyManager.GetActiveBodiesUnsafe(inType); } |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
bool WereBodiesInContact(const BodyID &inBody1ID, const BodyID &inBody2ID) const { return mContactManager.WereBodiesInContact(inBody1ID, inBody2ID); } |
| 249 |
|
| 250 |
|
| 251 |
AABox GetBounds() const { return mBroadPhase->GetBounds(); } |
| 252 |
|
| 253 |
#ifdef JPH_TRACK_BROADPHASE_STATS |
| 254 |
|
| 255 |
void ReportBroadphaseStats() { mBroadPhase->ReportStats(); } |
| 256 |
#endif |
| 257 |
|
| 258 |
private: |
| 259 |
using CCDBody = PhysicsUpdateContext::Step::CCDBody; |
| 260 |
|
| 261 |
|
| 262 |
void JobStepListeners(PhysicsUpdateContext::Step *ioStep); |
| 263 |
void JobDetermineActiveConstraints(PhysicsUpdateContext::Step *ioStep) const; |
| 264 |
void JobApplyGravity(const PhysicsUpdateContext *ioContext, PhysicsUpdateContext::Step *ioStep); |
| 265 |
void JobSetupVelocityConstraints(float inDeltaTime, PhysicsUpdateContext::Step *ioStep) const; |
| 266 |
void JobBuildIslandsFromConstraints(PhysicsUpdateContext *ioContext, PhysicsUpdateContext::Step *ioStep); |
| 267 |
void JobFindCollisions(PhysicsUpdateContext::Step *ioStep, int inJobIndex); |
| 268 |
void JobFinalizeIslands(PhysicsUpdateContext *ioContext); |
| 269 |
void JobBodySetIslandIndex(); |
| 270 |
void JobSolveVelocityConstraints(PhysicsUpdateContext *ioContext, PhysicsUpdateContext::Step *ioStep); |
| 271 |
void JobPreIntegrateVelocity(PhysicsUpdateContext *ioContext, PhysicsUpdateContext::Step *ioStep); |
| 272 |
void JobIntegrateVelocity(const PhysicsUpdateContext *ioContext, PhysicsUpdateContext::Step *ioStep); |
| 273 |
void JobPostIntegrateVelocity(PhysicsUpdateContext *ioContext, PhysicsUpdateContext::Step *ioStep) const; |
| 274 |
void JobFindCCDContacts(const PhysicsUpdateContext *ioContext, PhysicsUpdateContext::Step *ioStep); |
| 275 |
void JobResolveCCDContacts(PhysicsUpdateContext *ioContext, PhysicsUpdateContext::Step *ioStep); |
| 276 |
void JobContactRemovedCallbacks(const PhysicsUpdateContext::Step *ioStep); |
| 277 |
void JobSolvePositionConstraints(PhysicsUpdateContext *ioContext, PhysicsUpdateContext::Step *ioStep); |
| 278 |
void JobSoftBodyPrepare(PhysicsUpdateContext *ioContext, PhysicsUpdateContext::Step *ioStep); |
| 279 |
void JobSoftBodyCollide(PhysicsUpdateContext *ioContext) const; |
| 280 |
void JobSoftBodySimulate(PhysicsUpdateContext *ioContext, uint inThreadIndex) const; |
| 281 |
void JobSoftBodyFinalize(PhysicsUpdateContext *ioContext); |
| 282 |
|
| 283 |
|
| 284 |
void TrySpawnJobFindCollisions(PhysicsUpdateContext::Step *ioStep) const; |
| 285 |
|
| 286 |
using ContactAllocator = ContactConstraintManager::ContactAllocator; |
| 287 |
|
| 288 |
|
| 289 |
void ProcessBodyPair(ContactAllocator &ioContactAllocator, const BodyPair &inBodyPair); |
| 290 |
|
| 291 |
|
| 292 |
class BodiesToSleep; |
| 293 |
|
| 294 |
|
| 295 |
void CheckSleepAndUpdateBounds(uint32 inIslandIndex, const PhysicsUpdateContext *ioContext, const PhysicsUpdateContext::Step *ioStep, BodiesToSleep &ioBodiesToSleep); |
| 296 |
|
| 297 |
|
| 298 |
static constexpr int cDetermineActiveConstraintsBatchSize = 64; |
| 299 |
|
| 300 |
|
| 301 |
static constexpr int cSetupVelocityConstraintsBatchSize = 256; |
| 302 |
|
| 303 |
|
| 304 |
static constexpr int cApplyGravityBatchSize = 64; |
| 305 |
|
| 306 |
|
| 307 |
static constexpr int cActiveBodiesBatchSize = 16; |
| 308 |
|
| 309 |
|
| 310 |
static constexpr int cIntegrateVelocityBatchSize = 64; |
| 311 |
|
| 312 |
|
| 313 |
static constexpr int cNarrowPhaseBatchSize = 16; |
| 314 |
|
| 315 |
|
| 316 |
static constexpr int cNumCCDBodiesPerJob = 4; |
| 317 |
|
| 318 |
|
| 319 |
const ObjectVsBroadPhaseLayerFilter *mObjectVsBroadPhaseLayerFilter = nullptr; |
| 320 |
|
| 321 |
|
| 322 |
const ObjectLayerPairFilter *mObjectLayerPairFilter = nullptr; |
| 323 |
|
| 324 |
|
| 325 |
BodyManager mBodyManager; |
| 326 |
|
| 327 |
|
| 328 |
BodyLockInterfaceNoLock mBodyLockInterfaceNoLock { mBodyManager }; |
| 329 |
BodyLockInterfaceLocking mBodyLockInterfaceLocking { mBodyManager }; |
| 330 |
|
| 331 |
|
| 332 |
BodyInterface mBodyInterfaceNoLock; |
| 333 |
BodyInterface mBodyInterfaceLocking; |
| 334 |
|
| 335 |
|
| 336 |
NarrowPhaseQuery mNarrowPhaseQueryNoLock; |
| 337 |
NarrowPhaseQuery mNarrowPhaseQueryLocking; |
| 338 |
|
| 339 |
|
| 340 |
BroadPhase * mBroadPhase = nullptr; |
| 341 |
|
| 342 |
|
| 343 |
SoftBodyContactListener * mSoftBodyContactListener = nullptr; |
| 344 |
|
| 345 |
|
| 346 |
const SimShapeFilter * mSimShapeFilter = nullptr; |
| 347 |
|
| 348 |
|
| 349 |
SimCollideBodyVsBody mSimCollideBodyVsBody = &sDefaultSimCollideBodyVsBody; |
| 350 |
|
| 351 |
|
| 352 |
PhysicsSettings mPhysicsSettings; |
| 353 |
|
| 354 |
|
| 355 |
ContactConstraintManager mContactManager; |
| 356 |
|
| 357 |
|
| 358 |
ConstraintManager mConstraintManager; |
| 359 |
|
| 360 |
|
| 361 |
IslandBuilder mIslandBuilder; |
| 362 |
|
| 363 |
|
| 364 |
LargeIslandSplitter mLargeIslandSplitter; |
| 365 |
|
| 366 |
|
| 367 |
Mutex mStepListenersMutex; |
| 368 |
|
| 369 |
|
| 370 |
using StepListeners = Array<PhysicsStepListener *>; |
| 371 |
StepListeners mStepListeners; |
| 372 |
|
| 373 |
|
| 374 |
Vec3 mGravity = Vec3(0, -9.81f, 0); |
| 375 |
|
| 376 |
|
| 377 |
float mPreviousStepDeltaTime = 0.0f; |
| 378 |
}; |
| 379 |
|
| 380 |
JPH_NAMESPACE_END |