25class PhysicsUpdateContext;
42 void Init(
uint inMaxBodyPairs,
uint inMaxContactConstraints);
69 if (mContactListener ==
nullptr)
72 return mContactListener->OnContactValidate(inBody1, inBody2, inBaseOffset, inCollisionResult);
76 void PrepareConstraintBuffer(PhysicsUpdateContext *inContext);
98 void GetContactsFromCache(ContactAllocator &ioContactAllocator,
Body &inBody1,
Body &inBody2,
bool &outPairHandled,
bool &outConstraintCreated);
164 void SortContacts(
uint32 *inConstraintIdxBegin,
uint32 *inConstraintIdxEnd)
const;
169 const ContactConstraint &constraint = mConstraints[inConstraintIdx];
170 outBody1 = constraint.mBody1;
171 outBody2 = constraint.mBody2;
175 template <
class MotionPropertiesCallback>
176 void WarmStartVelocityConstraints(
const uint32 *inConstraintIdxBegin,
const uint32 *inConstraintIdxEnd,
float inWarmStartImpulseRatio, MotionPropertiesCallback &ioCallback);
208 bool SolveVelocityConstraints(
const uint32 *inConstraintIdxBegin,
const uint32 *inConstraintIdxEnd);
211 void StoreAppliedImpulses(
const uint32 *inConstraintIdxBegin,
const uint32 *inConstraintIdxEnd)
const;
229 bool SolvePositionConstraints(
const uint32 *inConstraintIdxBegin,
const uint32 *inConstraintIdxEnd);
232 void RecycleConstraintBuffer();
235 void FinishConstraintBuffer();
245#ifdef JPH_DEBUG_RENDERER
261 class CachedContactPoint
274 float mNonPenetrationLambda;
278 static_assert(
sizeof(CachedContactPoint) == 36,
"Unexpected size");
279 static_assert(
alignof(CachedContactPoint) == 4,
"Assuming 4 byte aligned");
286 static int sGetRequiredExtraSize(
int inNumContactPoints) {
return max(0, inNumContactPoints - 1) *
sizeof(CachedContactPoint); }
289 static int sGetRequiredTotalSize(
int inNumContactPoints) {
return sizeof(CachedManifold) + sGetRequiredExtraSize(inNumContactPoints); }
292 void SaveState(StateRecorder &inStream)
const;
293 void RestoreState(StateRecorder &inStream);
296 uint32 mNextWithSameBodyPair;
300 Float3 mContactNormal;
303 enum class EFlags :
uint16
305 ContactPersisted = 1,
310 mutable atomic<uint16> mFlags { 0 };
316 CachedContactPoint mContactPoints[1];
319 static_assert(
sizeof(CachedManifold) == 56,
"This structure is expect to not contain any waste due to alignment");
320 static_assert(
alignof(CachedManifold) == 4,
"Assuming 4 byte aligned");
323 using ManifoldMap = LockFreeHashMap<SubShapeIDPair, CachedManifold>;
324 using MKeyValue = ManifoldMap::KeyValue;
325 using MKVAndCreated = std::pair<MKeyValue *, bool>;
332 void SaveState(StateRecorder &inStream)
const;
333 void RestoreState(StateRecorder &inStream);
337 Float3 mDeltaPosition;
341 Float3 mDeltaRotation;
344 uint32 mFirstCachedManifold;
347 static_assert(
sizeof(CachedBodyPair) == 28,
"Unexpected size");
348 static_assert(
alignof(CachedBodyPair) == 4,
"Assuming 4 byte aligned");
351 using BodyPairMap = LockFreeHashMap<BodyPair, CachedBodyPair>;
352 using BPKeyValue = BodyPairMap::KeyValue;
359 void Init(
uint inMaxBodyPairs,
uint inMaxContactConstraints,
uint inCachedManifoldsSize);
366 void Prepare(
uint inExpectedNumBodyPairs,
uint inExpectedNumManifolds);
369 ContactAllocator GetContactAllocator() {
return ContactAllocator(mAllocator, cAllocatorBlockSize); }
372 const MKeyValue * Find(
const SubShapeIDPair &inKey,
uint64 inKeyHash)
const;
373 MKeyValue * Create(ContactAllocator &ioContactAllocator,
const SubShapeIDPair &inKey,
uint64 inKeyHash,
int inNumContactPoints);
374 MKVAndCreated FindOrCreate(ContactAllocator &ioContactAllocator,
const SubShapeIDPair &inKey,
uint64 inKeyHash,
int inNumContactPoints);
375 uint32 ToHandle(
const MKeyValue *inKeyValue)
const;
376 const MKeyValue * FromHandle(
uint32 inHandle)
const;
379 const BPKeyValue * Find(
const BodyPair &inKey,
uint64 inKeyHash)
const;
380 BPKeyValue * Create(ContactAllocator &ioContactAllocator,
const BodyPair &inKey,
uint64 inKeyHash);
384 void ContactPointRemovedCallbacks(ContactListener *inListener);
386#ifdef JPH_ENABLE_ASSERTS
388 uint GetNumManifolds()
const {
return mCachedManifolds.GetNumKeyValues(); }
391 uint GetNumBodyPairs()
const {
return mCachedBodyPairs.GetNumKeyValues(); }
398 void SaveState(StateRecorder &inStream,
const StateRecorderFilter *inFilter)
const;
399 bool RestoreState(
const ManifoldCache &inReadCache, StateRecorder &inStream,
const StateRecorderFilter *inFilter);
403 static constexpr uint32 cAllocatorBlockSize = 4096;
406 LFHMAllocator mAllocator;
409 ManifoldMap mCachedManifolds { mAllocator };
412 BodyPairMap mCachedBodyPairs { mAllocator };
414#ifdef JPH_ENABLE_ASSERTS
415 bool mIsFinalized =
false;
419 ManifoldCache mCache[2];
420 int mCacheWriteIdx = 0;
423 class WorldContactPoint
427 void CalculateNonPenetrationConstraintProperties(
const Body &inBody1,
float inInvMass1,
float inInvInertiaScale1,
const Body &inBody2,
float inInvMass2,
float inInvInertiaScale2,
RVec3Arg inWorldSpacePosition1,
RVec3Arg inWorldSpacePosition2,
Vec3Arg inWorldSpaceNormal);
429 template <EMotionType Type1, EMotionType Type2>
430 JPH_INLINE
void TemplatedCalculateFrictionAndNonPenetrationConstraintProperties(
float inDeltaTime,
float inGravityDeltaTimeDotNormal,
const Body &inBody1,
const Body &inBody2,
float inInvM1,
float inInvM2,
Mat44Arg inInvI1,
Mat44Arg inInvI2,
RVec3Arg inWorldSpacePosition1,
RVec3Arg inWorldSpacePosition2,
Vec3Arg inWorldSpaceNormal,
Vec3Arg inWorldSpaceTangent1,
Vec3Arg inWorldSpaceTangent2,
const ContactSettings &inSettings,
float inMinVelocityForRestitution);
433 AxisConstraintPart mNonPenetrationConstraint;
434 AxisConstraintPart mFrictionConstraint1;
435 AxisConstraintPart mFrictionConstraint2;
438 CachedContactPoint * mContactPoint;
441 using WorldContactPoints = StaticArray<WorldContactPoint, MaxContactPoints>;
444 class ContactConstraint
447 #ifdef JPH_DEBUG_RENDERER
449 void Draw(DebugRenderer *inRenderer,
ColorArg inManifoldColor)
const;
453 JPH_INLINE Vec3 GetWorldSpaceNormal()
const
459 JPH_INLINE
void GetTangents(Vec3 &outTangent1, Vec3 &outTangent2)
const
461 Vec3 ws_normal = GetWorldSpaceNormal();
463 outTangent2 = ws_normal.
Cross(outTangent1);
469 Float3 mWorldSpaceNormal;
470 float mCombinedFriction;
472 float mInvInertiaScale1;
474 float mInvInertiaScale2;
475 WorldContactPoints mContactPoints;
487 template <EMotionType Type1, EMotionType Type2>
488 JPH_INLINE
void TemplatedCalculateFrictionAndNonPenetrationConstraintProperties(ContactConstraint &ioConstraint,
const ContactSettings &inSettings,
float inDeltaTime,
Vec3Arg inGravityDeltaTime,
RMat44Arg inTransformBody1,
RMat44Arg inTransformBody2,
const Body &inBody1,
const Body &inBody2);
491 inline void CalculateFrictionAndNonPenetrationConstraintProperties(ContactConstraint &ioConstraint,
const ContactSettings &inSettings,
float inDeltaTime,
Vec3Arg inGravityDeltaTime,
RMat44Arg inTransformBody1,
RMat44Arg inTransformBody2,
const Body &inBody1,
const Body &inBody2);
494 template <EMotionType Type1, EMotionType Type2>
498 template <EMotionType Type1, EMotionType Type2>
499 JPH_INLINE
static void sWarmStartConstraint(ContactConstraint &ioConstraint,
MotionProperties *ioMotionProperties1,
MotionProperties *ioMotionProperties2,
float inWarmStartImpulseRatio);
502 template <EMotionType Type1, EMotionType Type2>
503 JPH_INLINE
static bool sSolveVelocityConstraint(ContactConstraint &ioConstraint,
MotionProperties *ioMotionProperties1,
MotionProperties *ioMotionProperties2);
516 ContactConstraint * mConstraints =
nullptr;
517 uint32 mMaxConstraints = 0;
518 atomic<uint32> mNumConstraints { 0 };
521 PhysicsUpdateContext * mUpdateContext;
Color ColorArg
Type to use for passing arguments to a function.
Definition Color.h:12
#define JPH_EXPORT
Definition Core.h:275
#define JPH_SUPPRESS_WARNINGS_STD_BEGIN
Definition Core.h:430
#define JPH_SUPPRESS_WARNINGS_STD_END
Definition Core.h:443
std::uint64_t uint64
Definition Core.h:504
unsigned int uint
Definition Core.h:500
#define JPH_NAMESPACE_END
Definition Core.h:425
std::uint32_t uint32
Definition Core.h:503
#define JPH_NAMESPACE_BEGIN
Definition Core.h:419
std::uint16_t uint16
Definition Core.h:502
EPhysicsUpdateError
Enum used by PhysicsSystem to report error conditions during the PhysicsSystem::Update call....
Definition EPhysicsUpdateError.h:11
@ None
No errors.
Definition EPhysicsUpdateError.h:12
const Vec3 Vec3Arg
Definition MathTypes.h:19
const Mat44 & Mat44Arg
Definition MathTypes.h:29
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition Memory.h:50
Vec3Arg RVec3Arg
Definition Real.h:30
Mat44Arg RMat44Arg
Definition Real.h:32
@ Array
Used in attribute declaration, indicates that this is an array of objects.
Definition SerializableAttribute.h:23
float GetRestitution() const
Restitution (dimensionless number, usually between 0 and 1, 0 = completely inelastic collision respon...
Definition Body.h:146
float GetFriction() const
Friction (dimensionless number, usually between 0 and 1, 0 = no friction, 1 = friction force equals f...
Definition Body.h:142
ID of a body. This is a way of reasoning about bodies in a multithreaded simulation while avoiding ra...
Definition BodyID.h:13
Class that contains all information of two colliding shapes.
Definition CollideShape.h:19
Class that holds 3 floats. Used as a storage class. Convert to Vec3 for calculations.
Definition Float3.h:13
LFHMAllocatorContext(LFHMAllocator &inAllocator, uint32 inBlockSize)
Construct a new allocator context.
Definition LockFreeHashMap.inl:80
The Body class only keeps track of state for static bodies, the MotionProperties class keeps the addi...
Definition MotionProperties.h:29
User callbacks that allow determining which parts of the simulation should be saved by a StateRecorde...
Definition StateRecorder.h:79
Definition StateRecorder.h:110
A sub shape id contains a path to an element (usually a triangle or other primitive type) of a compou...
Definition SubShapeID.h:23
JPH_INLINE Vec3 Cross(Vec3Arg inV2) const
Cross product.
Definition Vec3.inl:595
JPH_INLINE Vec3 GetNormalizedPerpendicular() const
Get normalized vector that is perpendicular to this vector.
Definition Vec3.inl:827
static JPH_INLINE Vec3 sLoadFloat3Unsafe(const Float3 &inV)
Load 3 floats from memory (reads 32 bits extra which it doesn't use).
Definition Vec3.inl:135
Templatized vector class.
Definition Vector.h:12
Definition PhysicsSettings.h:28