Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Constraints/ConstraintManager.h
9913 views
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)1// SPDX-FileCopyrightText: 2021 Jorrit Rouwe2// SPDX-License-Identifier: MIT34#pragma once56#include <Jolt/Physics/Constraints/Constraint.h>7#include <Jolt/Physics/PhysicsLock.h>8#include <Jolt/Core/Mutex.h>910JPH_NAMESPACE_BEGIN1112class IslandBuilder;13class BodyManager;14class StateRecorderFilter;15#ifdef JPH_DEBUG_RENDERER16class DebugRenderer;17#endif // JPH_DEBUG_RENDERER1819/// A list of constraints20using Constraints = Array<Ref<Constraint>>;2122/// A constraint manager manages all constraints of the same type23class JPH_EXPORT ConstraintManager : public NonCopyable24{25public:26JPH_OVERRIDE_NEW_DELETE2728#ifdef JPH_ENABLE_ASSERTS29/// Constructor30ConstraintManager(PhysicsLockContext inContext) : mLockContext(inContext) { }31#endif // JPH_ENABLE_ASSERTS3233/// Add a new constraint. This is thread safe.34void Add(Constraint **inConstraints, int inNumber);3536/// Remove a constraint. This is thread safe.37void Remove(Constraint **inConstraint, int inNumber);3839/// Get a list of all constraints40Constraints GetConstraints() const;4142/// Get total number of constraints43inline uint32 GetNumConstraints() const { return uint32(mConstraints.size()); }4445/// Determine the active constraints of a subset of the constraints46void GetActiveConstraints(uint32 inStartConstraintIdx, uint32 inEndConstraintIdx, Constraint **outActiveConstraints, uint32 &outNumActiveConstraints) const;4748/// Link bodies to form islands49static void sBuildIslands(Constraint **inActiveConstraints, uint32 inNumActiveConstraints, IslandBuilder &ioBuilder, BodyManager &inBodyManager);5051/// In order to have a deterministic simulation, we need to sort the constraints of an island before solving them52static void sSortConstraints(Constraint **inActiveConstraints, uint32 *inConstraintIdxBegin, uint32 *inConstraintIdxEnd);5354/// Prior to solving the velocity constraints, you must call SetupVelocityConstraints once to precalculate values that are independent of velocity55static void sSetupVelocityConstraints(Constraint **inActiveConstraints, uint32 inNumActiveConstraints, float inDeltaTime);5657/// Apply last frame's impulses, must be called prior to SolveVelocityConstraints58template <class ConstraintCallback>59static void sWarmStartVelocityConstraints(Constraint **inActiveConstraints, const uint32 *inConstraintIdxBegin, const uint32 *inConstraintIdxEnd, float inWarmStartImpulseRatio, ConstraintCallback &ioCallback);6061/// This function is called multiple times to iteratively come to a solution that meets all velocity constraints62static bool sSolveVelocityConstraints(Constraint **inActiveConstraints, const uint32 *inConstraintIdxBegin, const uint32 *inConstraintIdxEnd, float inDeltaTime);6364/// This function is called multiple times to iteratively come to a solution that meets all position constraints65static bool sSolvePositionConstraints(Constraint **inActiveConstraints, const uint32 *inConstraintIdxBegin, const uint32 *inConstraintIdxEnd, float inDeltaTime, float inBaumgarte);6667#ifdef JPH_DEBUG_RENDERER68/// Draw all constraints69void DrawConstraints(DebugRenderer *inRenderer) const;7071/// Draw all constraint limits72void DrawConstraintLimits(DebugRenderer *inRenderer) const;7374/// Draw all constraint reference frames75void DrawConstraintReferenceFrame(DebugRenderer *inRenderer) const;76#endif // JPH_DEBUG_RENDERER7778/// Save state of constraints79void SaveState(StateRecorder &inStream, const StateRecorderFilter *inFilter) const;8081/// Restore the state of constraints. Returns false if failed.82bool RestoreState(StateRecorder &inStream);8384/// Lock all constraints. This should only be done during PhysicsSystem::Update().85void LockAllConstraints() { PhysicsLock::sLock(mConstraintsMutex JPH_IF_ENABLE_ASSERTS(, mLockContext, EPhysicsLockTypes::ConstraintsList)); }86void UnlockAllConstraints() { PhysicsLock::sUnlock(mConstraintsMutex JPH_IF_ENABLE_ASSERTS(, mLockContext, EPhysicsLockTypes::ConstraintsList)); }8788private:89#ifdef JPH_ENABLE_ASSERTS90PhysicsLockContext mLockContext;91#endif // JPH_ENABLE_ASSERTS92Constraints mConstraints;93mutable Mutex mConstraintsMutex;94};9596JPH_NAMESPACE_END979899