39 void Init(
uint inMaxBodyPairs,
uint inMaxContactConstraints);
66 if (mContactListener ==
nullptr)
69 return mContactListener->OnContactValidate(inBody1, inBody2, inBaseOffset, inCollisionResult);
76 static const int MaxContactPoints = 4;
95 void GetContactsFromCache(ContactAllocator &ioContactAllocator,
Body &inBody1,
Body &inBody2,
bool &outPairHandled,
bool &outConstraintCreated);
151 void FinalizeContactCacheAndCallContactPointRemovedCallbacks(
uint inExpectedNumBodyPairs,
uint inExpectedNumManifolds);
155 bool WereBodiesInContact(
const BodyID &inBody1ID,
const BodyID &inBody2ID)
const;
161 void SortContacts(
uint32 *inConstraintIdxBegin,
uint32 *inConstraintIdxEnd)
const;
166 const ContactConstraint &constraint = mConstraints[inConstraintIdx];
167 outBody1 = constraint.mBody1;
168 outBody2 = constraint.mBody2;
172 template <
class MotionPropertiesCallback>
173 void WarmStartVelocityConstraints(
const uint32 *inConstraintIdxBegin,
const uint32 *inConstraintIdxEnd,
float inWarmStartImpulseRatio, MotionPropertiesCallback &ioCallback);
205 bool SolveVelocityConstraints(
const uint32 *inConstraintIdxBegin,
const uint32 *inConstraintIdxEnd);
208 void StoreAppliedImpulses(
const uint32 *inConstraintIdxBegin,
const uint32 *inConstraintIdxEnd)
const;
226 bool SolvePositionConstraints(
const uint32 *inConstraintIdxBegin,
const uint32 *inConstraintIdxEnd);
229 void RecycleConstraintBuffer();
232 void FinishConstraintBuffer();
242#ifdef JPH_DEBUG_RENDERER
258 class CachedContactPoint
271 float mNonPenetrationLambda;
275 static_assert(
sizeof(CachedContactPoint) == 36,
"Unexpected size");
276 static_assert(
alignof(CachedContactPoint) == 4,
"Assuming 4 byte aligned");
283 static int sGetRequiredExtraSize(
int inNumContactPoints) {
return max(0, inNumContactPoints - 1) *
sizeof(CachedContactPoint); }
286 static int sGetRequiredTotalSize(
int inNumContactPoints) {
return sizeof(CachedManifold) + sGetRequiredExtraSize(inNumContactPoints); }
293 uint32 mNextWithSameBodyPair;
300 enum class EFlags :
uint16
302 ContactPersisted = 1,
307 mutable atomic<uint16> mFlags { 0 };
313 CachedContactPoint mContactPoints[1];
316 static_assert(
sizeof(CachedManifold) == 56,
"This structure is expect to not contain any waste due to alignment");
317 static_assert(
alignof(CachedManifold) == 4,
"Assuming 4 byte aligned");
321 using MKeyValue = ManifoldMap::KeyValue;
322 using MKVAndCreated = std::pair<MKeyValue *, bool>;
341 uint32 mFirstCachedManifold;
344 static_assert(
sizeof(CachedBodyPair) == 28,
"Unexpected size");
345 static_assert(
alignof(CachedBodyPair) == 4,
"Assuming 4 byte aligned");
349 using BPKeyValue = BodyPairMap::KeyValue;
356 void Init(
uint inMaxBodyPairs,
uint inMaxContactConstraints,
uint inCachedManifoldsSize);
363 void Prepare(
uint inExpectedNumBodyPairs,
uint inExpectedNumManifolds);
366 ContactAllocator GetContactAllocator() {
return ContactAllocator(mAllocator, cAllocatorBlockSize); }
370 MKeyValue * Create(ContactAllocator &ioContactAllocator,
const SubShapeIDPair &inKey,
uint64 inKeyHash,
int inNumContactPoints);
371 MKVAndCreated FindOrCreate(ContactAllocator &ioContactAllocator,
const SubShapeIDPair &inKey,
uint64 inKeyHash,
int inNumContactPoints);
372 uint32 ToHandle(
const MKeyValue *inKeyValue)
const;
373 const MKeyValue * FromHandle(
uint32 inHandle)
const;
376 const BPKeyValue * Find(
const BodyPair &inKey,
uint64 inKeyHash)
const;
377 BPKeyValue * Create(ContactAllocator &ioContactAllocator,
const BodyPair &inKey,
uint64 inKeyHash);
383#ifdef JPH_ENABLE_ASSERTS
385 uint GetNumManifolds()
const {
return mCachedManifolds.GetNumKeyValues(); }
388 uint GetNumBodyPairs()
const {
return mCachedBodyPairs.GetNumKeyValues(); }
400 static constexpr uint32 cAllocatorBlockSize = 4096;
406 ManifoldMap mCachedManifolds { mAllocator };
409 BodyPairMap mCachedBodyPairs { mAllocator };
411#ifdef JPH_ENABLE_ASSERTS
412 bool mIsFinalized =
false;
416 ManifoldCache mCache[2];
417 int mCacheWriteIdx = 0;
420 class WorldContactPoint
424 void CalculateNonPenetrationConstraintProperties(
const Body &inBody1,
float inInvMass1,
float inInvInertiaScale1,
const Body &inBody2,
float inInvMass2,
float inInvInertiaScale2,
RVec3Arg inWorldSpacePosition1,
RVec3Arg inWorldSpacePosition2,
Vec3Arg inWorldSpaceNormal);
426 template <EMotionType Type1, EMotionType Type2>
427 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);
435 CachedContactPoint * mContactPoint;
441 class ContactConstraint
444 #ifdef JPH_DEBUG_RENDERER
450 JPH_INLINE
Vec3 GetWorldSpaceNormal()
const
456 JPH_INLINE
void GetTangents(
Vec3 &outTangent1,
Vec3 &outTangent2)
const
458 Vec3 ws_normal = GetWorldSpaceNormal();
460 outTangent2 = ws_normal.
Cross(outTangent1);
467 float mCombinedFriction;
469 float mInvInertiaScale1;
471 float mInvInertiaScale2;
472 WorldContactPoints mContactPoints;
476 template <EMotionType Type1, EMotionType Type2>
477 JPH_INLINE
void TemplatedCalculateFrictionAndNonPenetrationConstraintProperties(ContactConstraint &ioConstraint,
const ContactSettings &inSettings,
float inDeltaTime,
Vec3Arg inGravityDeltaTime,
RMat44Arg inTransformBody1,
RMat44Arg inTransformBody2,
const Body &inBody1,
const Body &inBody2);
480 inline void CalculateFrictionAndNonPenetrationConstraintProperties(ContactConstraint &ioConstraint,
const ContactSettings &inSettings,
float inDeltaTime,
Vec3Arg inGravityDeltaTime,
RMat44Arg inTransformBody1,
RMat44Arg inTransformBody2,
const Body &inBody1,
const Body &inBody2);
483 template <EMotionType Type1, EMotionType Type2>
484 bool TemplatedAddContactConstraint(ContactAllocator &ioContactAllocator, BodyPairHandle inBodyPairHandle,
Body &inBody1,
Body &inBody2,
const ContactManifold &inManifold);
487 template <EMotionType Type1, EMotionType Type2>
488 JPH_INLINE
static void sWarmStartConstraint(ContactConstraint &ioConstraint,
MotionProperties *ioMotionProperties1,
MotionProperties *ioMotionProperties2,
float inWarmStartImpulseRatio);
491 template <EMotionType Type1, EMotionType Type2>
492 JPH_INLINE
static bool sSolveVelocityConstraint(ContactConstraint &ioConstraint,
MotionProperties *ioMotionProperties1,
MotionProperties *ioMotionProperties2);
505 ContactConstraint * mConstraints =
nullptr;
506 uint32 mMaxConstraints = 0;
507 atomic<uint32> mNumConstraints { 0 };
#define JPH_EXPORT
Definition Core.h:236
#define JPH_SUPPRESS_WARNINGS_STD_BEGIN
Definition Core.h:384
#define JPH_SUPPRESS_WARNINGS_STD_END
Definition Core.h:396
std::uint64_t uint64
Definition Core.h:450
unsigned int uint
Definition Core.h:446
#define JPH_NAMESPACE_END
Definition Core.h:379
std::uint32_t uint32
Definition Core.h:449
#define JPH_NAMESPACE_BEGIN
Definition Core.h:373
std::uint16_t uint16
Definition Core.h:448
EPhysicsUpdateError
Enum used by PhysicsSystem to report error conditions during the PhysicsSystem::Update call....
Definition EPhysicsUpdateError.h:11
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition Memory.h:31
Definition AxisConstraintPart.h:43
float GetRestitution() const
Restitution (dimensionless number, usually between 0 and 1, 0 = completely inelastic collision respon...
Definition Body.h:142
float GetFriction() const
Friction (dimensionless number, usually between 0 and 1, 0 = no friction, 1 = friction force equals f...
Definition Body.h:138
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 an RGBA color with 8-bits per component.
Definition Color.h:16
Definition DebugRenderer.h:47
Class that holds 3 floats. Used as a storage class. Convert to Vec3 for calculations.
Definition Float3.h:13
Definition LockFreeHashMap.h:49
LFHMAllocatorContext(LFHMAllocator &inAllocator, uint32 inBlockSize)
Construct a new allocator context.
Definition LockFreeHashMap.inl:80
Allocator for a lock free hash map.
Definition LockFreeHashMap.h:14
Definition LockFreeHashMap.h:72
Holds a 4x4 matrix of floats, but supports also operations on the 3x3 upper left part of the matrix.
Definition Mat44.h:13
The Body class only keeps track of state for static bodies, the MotionProperties class keeps the addi...
Definition MotionProperties.h:29
Class that makes another class non-copyable. Usage: Inherit from NonCopyable.
Definition NonCopyable.h:11
Information used during the Update call.
Definition PhysicsUpdateContext.h:24
User callbacks that allow determining which parts of the simulation should be saved by a StateRecorde...
Definition StateRecorder.h:79
Definition StateRecorder.h:110
Simple variable length array backed by a fixed size buffer.
Definition StaticArray.h:14
A sub shape id contains a path to an element (usually a triangle or other primitive type) of a compou...
Definition SubShapeID.h:23
A pair of bodies and their sub shape ID's. Can be used as a key in a map to find a contact point.
Definition SubShapeIDPair.h:15
JPH_INLINE Vec3 Cross(Vec3Arg inV2) const
Cross product.
Definition Vec3.inl:594
JPH_INLINE Vec3 GetNormalizedPerpendicular() const
Get normalized vector that is perpendicular to this vector.
Definition Vec3.inl:826
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:134
Templatized vector class.
Definition Vector.h:12
Structure that holds a body pair.
Definition BodyPair.h:14
Definition PhysicsSettings.h:28