Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
CharacterVirtual.h
Go to the documentation of this file.
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
14
16
19
22{
23public:
25
27 float mMass = 70.0f;
28
30 float mMaxStrength = 100.0f;
31
33 Vec3 mShapeOffset = Vec3::sZero();
34
37 float mPredictiveContactDistance = 0.1f;
38 uint mMaxCollisionIterations = 5;
39 uint mMaxConstraintIterations = 15;
40 float mMinTimeRemaining = 1.0e-4f;
41 float mCollisionTolerance = 1.0e-3f;
42 float mCharacterPadding = 0.02f;
43 uint mMaxNumHits = 256;
44 float mHitReductionCosMaxAngle = 0.999f;
45 float mPenetrationRecoverySpeed = 1.0f;
46
52
54 ObjectLayer mInnerBodyLayer = 0;
55};
56
59{
60public:
62 bool mCanPushCharacter = true;
63
68};
69
72{
73public:
75 virtual ~CharacterContactListener() = default;
76
79 virtual void OnAdjustBodyVelocity(const CharacterVirtual *inCharacter, const Body &inBody2, Vec3 &ioLinearVelocity, Vec3 &ioAngularVelocity) { /* Do nothing, the linear and angular velocity are already filled in */ }
80
82 virtual bool OnContactValidate(const CharacterVirtual *inCharacter, const BodyID &inBodyID2, const SubShapeID &inSubShapeID2) { return true; }
83
85 virtual bool OnCharacterContactValidate(const CharacterVirtual *inCharacter, const CharacterVirtual *inOtherCharacter, const SubShapeID &inSubShapeID2) { return true; }
86
94 virtual void OnContactAdded(const CharacterVirtual *inCharacter, const BodyID &inBodyID2, const SubShapeID &inSubShapeID2, RVec3Arg inContactPosition, Vec3Arg inContactNormal, CharacterContactSettings &ioSettings) { /* Default do nothing */ }
95
97 virtual void OnCharacterContactAdded(const CharacterVirtual *inCharacter, const CharacterVirtual *inOtherCharacter, const SubShapeID &inSubShapeID2, RVec3Arg inContactPosition, Vec3Arg inContactNormal, CharacterContactSettings &ioSettings) { /* Default do nothing */ }
98
109 virtual void OnContactSolve(const CharacterVirtual *inCharacter, const BodyID &inBodyID2, const SubShapeID &inSubShapeID2, RVec3Arg inContactPosition, Vec3Arg inContactNormal, Vec3Arg inContactVelocity, const PhysicsMaterial *inContactMaterial, Vec3Arg inCharacterVelocity, Vec3 &ioNewCharacterVelocity) { /* Default do nothing */ }
110
112 virtual void OnCharacterContactSolve(const CharacterVirtual *inCharacter, const CharacterVirtual *inOtherCharacter, const SubShapeID &inSubShapeID2, RVec3Arg inContactPosition, Vec3Arg inContactNormal, Vec3Arg inContactVelocity, const PhysicsMaterial *inContactMaterial, Vec3Arg inCharacterVelocity, Vec3 &ioNewCharacterVelocity) { /* Default do nothing */ }
113};
114
119{
120public:
122
129 virtual void CollideCharacter(const CharacterVirtual *inCharacter, RMat44Arg inCenterOfMassTransform, const CollideShapeSettings &inCollideShapeSettings, RVec3Arg inBaseOffset, CollideShapeCollector &ioCollector) const = 0;
130
138 virtual void CastCharacter(const CharacterVirtual *inCharacter, RMat44Arg inCenterOfMassTransform, Vec3Arg inDirection, const ShapeCastSettings &inShapeCastSettings, RVec3Arg inBaseOffset, CastShapeCollector &ioCollector) const = 0;
139};
140
144{
145public:
147 void Add(CharacterVirtual *inCharacter) { mCharacters.push_back(inCharacter); }
148
150 void Remove(const CharacterVirtual *inCharacter);
151
152 // See: CharacterVsCharacterCollision
153 virtual void CollideCharacter(const CharacterVirtual *inCharacter, RMat44Arg inCenterOfMassTransform, const CollideShapeSettings &inCollideShapeSettings, RVec3Arg inBaseOffset, CollideShapeCollector &ioCollector) const override;
154 virtual void CastCharacter(const CharacterVirtual *inCharacter, RMat44Arg inCenterOfMassTransform, Vec3Arg inDirection, const ShapeCastSettings &inShapeCastSettings, RVec3Arg inBaseOffset, CastShapeCollector &ioCollector) const override;
155
157};
158
165{
166public:
168
175 CharacterVirtual(const CharacterVirtualSettings *inSettings, RVec3Arg inPosition, QuatArg inRotation, uint64 inUserData, PhysicsSystem *inSystem);
176
178 CharacterVirtual(const CharacterVirtualSettings *inSettings, RVec3Arg inPosition, QuatArg inRotation, PhysicsSystem *inSystem) : CharacterVirtual(inSettings, inPosition, inRotation, 0, inSystem) { }
179
181 virtual ~CharacterVirtual() override;
182
184 void SetListener(CharacterContactListener *inListener) { mListener = inListener; }
185
187 CharacterContactListener * GetListener() const { return mListener; }
188
190 void SetCharacterVsCharacterCollision(CharacterVsCharacterCollision *inCharacterVsCharacterCollision) { mCharacterVsCharacterCollision = inCharacterVsCharacterCollision; }
191
193 Vec3 GetLinearVelocity() const { return mLinearVelocity; }
194
196 void SetLinearVelocity(Vec3Arg inLinearVelocity) { mLinearVelocity = inLinearVelocity; }
197
199 RVec3 GetPosition() const { return mPosition; }
200
202 void SetPosition(RVec3Arg inPosition) { mPosition = inPosition; UpdateInnerBodyTransform(); }
203
205 Quat GetRotation() const { return mRotation; }
206
208 void SetRotation(QuatArg inRotation) { mRotation = inRotation; UpdateInnerBodyTransform(); }
209
210 // Get the center of mass position of the shape
211 inline RVec3 GetCenterOfMassPosition() const { return mPosition + (mRotation * (mShapeOffset + mShape->GetCenterOfMass()) + mCharacterPadding * mUp); }
212
214 RMat44 GetWorldTransform() const { return RMat44::sRotationTranslation(mRotation, mPosition); }
215
217 RMat44 GetCenterOfMassTransform() const { return GetCenterOfMassTransform(mPosition, mRotation, mShape); }
218
220 float GetMass() const { return mMass; }
221 void SetMass(float inMass) { mMass = inMass; }
222
224 float GetMaxStrength() const { return mMaxStrength; }
225 void SetMaxStrength(float inMaxStrength) { mMaxStrength = inMaxStrength; }
226
228 float GetPenetrationRecoverySpeed() const { return mPenetrationRecoverySpeed; }
229 void SetPenetrationRecoverySpeed(float inSpeed) { mPenetrationRecoverySpeed = inSpeed; }
230
233 void SetEnhancedInternalEdgeRemoval(bool inApply) { mEnhancedInternalEdgeRemoval = inApply; }
234
236 float GetCharacterPadding() const { return mCharacterPadding; }
237
239 uint GetMaxNumHits() const { return mMaxNumHits; }
240 void SetMaxNumHits(uint inMaxHits) { mMaxNumHits = inMaxHits; }
241
243 float GetHitReductionCosMaxAngle() const { return mHitReductionCosMaxAngle; }
244 void SetHitReductionCosMaxAngle(float inCosMaxAngle) { mHitReductionCosMaxAngle = inCosMaxAngle; }
245
250 bool GetMaxHitsExceeded() const { return mMaxHitsExceeded; }
251
253 Vec3 GetShapeOffset() const { return mShapeOffset; }
254 void SetShapeOffset(Vec3Arg inShapeOffset) { mShapeOffset = inShapeOffset; UpdateInnerBodyTransform(); }
255
257 uint64 GetUserData() const { return mUserData; }
258 void SetUserData(uint64 inUserData);
259
261 BodyID GetInnerBodyID() const { return mInnerBodyID; }
262
267 Vec3 CancelVelocityTowardsSteepSlopes(Vec3Arg inDesiredVelocity) const;
268
279 void Update(float inDeltaTime, Vec3Arg inGravity, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter, const ObjectLayerFilter &inObjectLayerFilter, const BodyFilter &inBodyFilter, const ShapeFilter &inShapeFilter, TempAllocator &inAllocator);
280
284 bool CanWalkStairs(Vec3Arg inLinearVelocity) const;
285
298 bool WalkStairs(float inDeltaTime, Vec3Arg inStepUp, Vec3Arg inStepForward, Vec3Arg inStepForwardTest, Vec3Arg inStepDownExtra, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter, const ObjectLayerFilter &inObjectLayerFilter, const BodyFilter &inBodyFilter, const ShapeFilter &inShapeFilter, TempAllocator &inAllocator);
299
310 bool StickToFloor(Vec3Arg inStepDown, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter, const ObjectLayerFilter &inObjectLayerFilter, const BodyFilter &inBodyFilter, const ShapeFilter &inShapeFilter, TempAllocator &inAllocator);
311
314 {
315 Vec3 mStickToFloorStepDown { 0, -0.5f, 0 };
316 Vec3 mWalkStairsStepUp { 0, 0.4f, 0 };
317 float mWalkStairsMinStepForward { 0.02f };
318 float mWalkStairsStepForwardTest { 0.15f };
319 float mWalkStairsCosAngleForwardContact { Cos(DegreesToRadians(75.0f)) };
320 Vec3 mWalkStairsStepDownExtra { Vec3::sZero() };
321 };
322
335 void ExtendedUpdate(float inDeltaTime, Vec3Arg inGravity, const ExtendedUpdateSettings &inSettings, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter, const ObjectLayerFilter &inObjectLayerFilter, const BodyFilter &inBodyFilter, const ShapeFilter &inShapeFilter, TempAllocator &inAllocator);
336
338 void RefreshContacts(const BroadPhaseLayerFilter &inBroadPhaseLayerFilter, const ObjectLayerFilter &inObjectLayerFilter, const BodyFilter &inBodyFilter, const ShapeFilter &inShapeFilter, TempAllocator &inAllocator);
339
342 void UpdateGroundVelocity();
343
353 bool SetShape(const Shape *inShape, float inMaxPenetrationDepth, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter, const ObjectLayerFilter &inObjectLayerFilter, const BodyFilter &inBodyFilter, const ShapeFilter &inShapeFilter, TempAllocator &inAllocator);
354
356 void SetInnerBodyShape(const Shape *inShape);
357
359 TransformedShape GetTransformedShape() const { return TransformedShape(GetCenterOfMassPosition(), mRotation, mShape, mInnerBodyID); }
360
374 void CheckCollision(RVec3Arg inPosition, QuatArg inRotation, Vec3Arg inMovementDirection, float inMaxSeparationDistance, const Shape *inShape, RVec3Arg inBaseOffset, CollideShapeCollector &ioCollector, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter, const ObjectLayerFilter &inObjectLayerFilter, const BodyFilter &inBodyFilter, const ShapeFilter &inShapeFilter) const;
375
376 // Saving / restoring state for replay
377 virtual void SaveState(StateRecorder &inStream) const override;
378 virtual void RestoreState(StateRecorder &inStream) override;
379
380#ifdef JPH_DEBUG_RENDERER
381 static inline bool sDrawConstraints = false;
382 static inline bool sDrawWalkStairs = false;
383 static inline bool sDrawStickToFloor = false;
384#endif
385
386 // Encapsulates a collision contact
387 struct Contact
388 {
389 // Saving / restoring state for replay
390 void SaveState(StateRecorder &inStream) const;
391 void RestoreState(StateRecorder &inStream);
392
393 // Checks if two contacts refer to the same body (or virtual character)
394 inline bool IsSameBody(const Contact &inOther) const { return mBodyB == inOther.mBodyB && mCharacterB == inOther.mCharacterB; }
395
400 float mDistance;
401 float mFraction;
403 CharacterVirtual * mCharacterB = nullptr;
409 bool mHadCollision = false;
410 bool mWasDiscarded = false;
411 bool mCanPushCharacter = true;
412 };
413
416
418 const ContactList & GetActiveContacts() const { return mActiveContacts; }
419
421 bool HasCollidedWith(const BodyID &inBody) const
422 {
423 for (const CharacterVirtual::Contact &c : mActiveContacts)
424 if (c.mHadCollision && c.mBodyB == inBody)
425 return true;
426 return false;
427 }
428
430 bool HasCollidedWith(const CharacterVirtual *inCharacter) const
431 {
432 for (const CharacterVirtual::Contact &c : mActiveContacts)
433 if (c.mHadCollision && c.mCharacterB == inCharacter)
434 return true;
435 return false;
436 }
437
438private:
439 // Sorting predicate for making contact order deterministic
440 struct ContactOrderingPredicate
441 {
442 inline bool operator () (const Contact &inLHS, const Contact &inRHS) const
443 {
444 if (inLHS.mBodyB != inRHS.mBodyB)
445 return inLHS.mBodyB < inRHS.mBodyB;
446
447 return inLHS.mSubShapeIDB.GetValue() < inRHS.mSubShapeIDB.GetValue();
448 }
449 };
450
451 // A contact that needs to be ignored
452 struct IgnoredContact
453 {
454 IgnoredContact() = default;
455 IgnoredContact(const BodyID &inBodyID, const SubShapeID &inSubShapeID) : mBodyID(inBodyID), mSubShapeID(inSubShapeID) { }
456
457 BodyID mBodyID;
458 SubShapeID mSubShapeID;
459 };
460
462
463 // A constraint that limits the movement of the character
464 struct Constraint
465 {
466 Contact * mContact;
467 float mTOI;
468 float mProjectedVelocity;
469 Vec3 mLinearVelocity;
470 Plane mPlane;
471 bool mIsSteepSlope = false;
472 };
473
475
476 // Collision collector that collects hits for CollideShape
477 class ContactCollector : public CollideShapeCollector
478 {
479 public:
480 ContactCollector(PhysicsSystem *inSystem, const CharacterVirtual *inCharacter, uint inMaxHits, float inHitReductionCosMaxAngle, Vec3Arg inUp, RVec3Arg inBaseOffset, TempContactList &outContacts) : mBaseOffset(inBaseOffset), mUp(inUp), mSystem(inSystem), mCharacter(inCharacter), mContacts(outContacts), mMaxHits(inMaxHits), mHitReductionCosMaxAngle(inHitReductionCosMaxAngle) { }
481
482 virtual void SetUserData(uint64 inUserData) override { mOtherCharacter = reinterpret_cast<CharacterVirtual *>(inUserData); }
483
484 virtual void AddHit(const CollideShapeResult &inResult) override;
485
486 RVec3 mBaseOffset;
487 Vec3 mUp;
488 PhysicsSystem * mSystem;
489 const CharacterVirtual * mCharacter;
490 CharacterVirtual * mOtherCharacter = nullptr;
491 TempContactList & mContacts;
492 uint mMaxHits;
493 float mHitReductionCosMaxAngle;
494 bool mMaxHitsExceeded = false;
495 };
496
497 // A collision collector that collects hits for CastShape
498 class ContactCastCollector : public CastShapeCollector
499 {
500 public:
501 ContactCastCollector(PhysicsSystem *inSystem, const CharacterVirtual *inCharacter, Vec3Arg inDisplacement, Vec3Arg inUp, const IgnoredContactList &inIgnoredContacts, RVec3Arg inBaseOffset, Contact &outContact) : mBaseOffset(inBaseOffset), mDisplacement(inDisplacement), mUp(inUp), mSystem(inSystem), mCharacter(inCharacter), mIgnoredContacts(inIgnoredContacts), mContact(outContact) { }
502
503 virtual void SetUserData(uint64 inUserData) override { mOtherCharacter = reinterpret_cast<CharacterVirtual *>(inUserData); }
504
505 virtual void AddHit(const ShapeCastResult &inResult) override;
506
507 RVec3 mBaseOffset;
508 Vec3 mDisplacement;
509 Vec3 mUp;
510 PhysicsSystem * mSystem;
511 const CharacterVirtual * mCharacter;
512 CharacterVirtual * mOtherCharacter = nullptr;
513 const IgnoredContactList & mIgnoredContacts;
514 Contact & mContact;
515 };
516
517 // Helper function to convert a Jolt collision result into a contact
518 template <class taCollector>
519 inline static void sFillContactProperties(const CharacterVirtual *inCharacter, Contact &outContact, const Body &inBody, Vec3Arg inUp, RVec3Arg inBaseOffset, const taCollector &inCollector, const CollideShapeResult &inResult);
520 inline static void sFillCharacterContactProperties(Contact &outContact, CharacterVirtual *inOtherCharacter, RVec3Arg inBaseOffset, const CollideShapeResult &inResult);
521
522 // Move the shape from ioPosition and try to displace it by inVelocity * inDeltaTime, this will try to slide the shape along the world geometry
523 void MoveShape(RVec3 &ioPosition, Vec3Arg inVelocity, float inDeltaTime, ContactList *outActiveContacts, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter, const ObjectLayerFilter &inObjectLayerFilter, const BodyFilter &inBodyFilter, const ShapeFilter &inShapeFilter, TempAllocator &inAllocator
524 #ifdef JPH_DEBUG_RENDERER
525 , bool inDrawConstraints = false
526 #endif // JPH_DEBUG_RENDERER
527 ) const;
528
529 // Ask the callback if inContact is a valid contact point
530 bool ValidateContact(const Contact &inContact) const;
531
532 // Trigger the contact callback for inContact and get the contact settings
533 void ContactAdded(const Contact &inContact, CharacterContactSettings &ioSettings) const;
534
535 // Tests the shape for collision around inPosition
536 void GetContactsAtPosition(RVec3Arg inPosition, Vec3Arg inMovementDirection, const Shape *inShape, TempContactList &outContacts, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter, const ObjectLayerFilter &inObjectLayerFilter, const BodyFilter &inBodyFilter, const ShapeFilter &inShapeFilter) const;
537
538 // Remove penetrating contacts with the same body that have conflicting normals, leaving these will make the character mover get stuck
539 void RemoveConflictingContacts(TempContactList &ioContacts, IgnoredContactList &outIgnoredContacts) const;
540
541 // Convert contacts into constraints. The character is assumed to start at the origin and the constraints are planes around the origin that confine the movement of the character.
542 void DetermineConstraints(TempContactList &inContacts, float inDeltaTime, ConstraintList &outConstraints) const;
543
544 // Use the constraints to solve the displacement of the character. This will slide the character on the planes around the origin for as far as possible.
545 void SolveConstraints(Vec3Arg inVelocity, float inDeltaTime, float inTimeRemaining, ConstraintList &ioConstraints, IgnoredContactList &ioIgnoredContacts, float &outTimeSimulated, Vec3 &outDisplacement, TempAllocator &inAllocator
546 #ifdef JPH_DEBUG_RENDERER
547 , bool inDrawConstraints = false
548 #endif // JPH_DEBUG_RENDERER
549 ) const;
550
551 // Get the velocity of a body adjusted by the contact listener
552 void GetAdjustedBodyVelocity(const Body& inBody, Vec3 &outLinearVelocity, Vec3 &outAngularVelocity) const;
553
554 // Calculate the ground velocity of the character assuming it's standing on an object with specified linear and angular velocity and with specified center of mass.
555 // Note that we don't just take the point velocity because a point on an object with angular velocity traces an arc,
556 // so if you just take point velocity * delta time you get an error that accumulates over time
557 Vec3 CalculateCharacterGroundVelocity(RVec3Arg inCenterOfMass, Vec3Arg inLinearVelocity, Vec3Arg inAngularVelocity, float inDeltaTime) const;
558
559 // Handle contact with physics object that we're colliding against
560 bool HandleContact(Vec3Arg inVelocity, Constraint &ioConstraint, float inDeltaTime) const;
561
562 // Does a swept test of the shape from inPosition with displacement inDisplacement, returns true if there was a collision
563 bool GetFirstContactForSweep(RVec3Arg inPosition, Vec3Arg inDisplacement, Contact &outContact, const IgnoredContactList &inIgnoredContacts, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter, const ObjectLayerFilter &inObjectLayerFilter, const BodyFilter &inBodyFilter, const ShapeFilter &inShapeFilter) const;
564
565 // Store contacts so that we have proper ground information
566 void StoreActiveContacts(const TempContactList &inContacts, TempAllocator &inAllocator);
567
568 // This function will determine which contacts are touching the character and will calculate the one that is supporting us
569 void UpdateSupportingContact(bool inSkipContactVelocityCheck, TempAllocator &inAllocator);
570
572 void MoveToContact(RVec3Arg inPosition, const Contact &inContact, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter, const ObjectLayerFilter &inObjectLayerFilter, const BodyFilter &inBodyFilter, const ShapeFilter &inShapeFilter, TempAllocator &inAllocator);
573
574 // This function returns the actual center of mass of the shape, not corrected for the character padding
575 inline RMat44 GetCenterOfMassTransform(RVec3Arg inPosition, QuatArg inRotation, const Shape *inShape) const
576 {
577 return RMat44::sRotationTranslation(inRotation, inPosition).PreTranslated(mShapeOffset + inShape->GetCenterOfMass()).PostTranslated(mCharacterPadding * mUp);
578 }
579
580 // This function returns the position of the inner rigid body
581 inline RVec3 GetInnerBodyPosition() const
582 {
583 return mPosition + (mRotation * mShapeOffset + mCharacterPadding * mUp);
584 }
585
586 // Move the inner rigid body to the current position
587 void UpdateInnerBodyTransform();
588
589 // Our main listener for contacts
590 CharacterContactListener * mListener = nullptr;
591
592 // Interface to detect collision between characters
593 CharacterVsCharacterCollision * mCharacterVsCharacterCollision = nullptr;
594
595 // Movement settings
596 EBackFaceMode mBackFaceMode; // When colliding with back faces, the character will not be able to move through back facing triangles. Use this if you have triangles that need to collide on both sides.
597 float mPredictiveContactDistance; // How far to scan outside of the shape for predictive contacts. A value of 0 will most likely cause the character to get stuck as it cannot properly calculate a sliding direction anymore. A value that's too high will cause ghost collisions.
598 uint mMaxCollisionIterations; // Max amount of collision loops
599 uint mMaxConstraintIterations; // How often to try stepping in the constraint solving
600 float mMinTimeRemaining; // Early out condition: If this much time is left to simulate we are done
601 float mCollisionTolerance; // How far we're willing to penetrate geometry
602 float mCharacterPadding; // How far we try to stay away from the geometry, this ensures that the sweep will hit as little as possible lowering the collision cost and reducing the risk of getting stuck
603 uint mMaxNumHits; // Max num hits to collect in order to avoid excess of contact points collection
604 float mHitReductionCosMaxAngle; // Cos(angle) where angle is the maximum angle between two hits contact normals that are allowed to be merged during hit reduction. Default is around 2.5 degrees. Set to -1 to turn off.
605 float mPenetrationRecoverySpeed; // This value governs how fast a penetration will be resolved, 0 = nothing is resolved, 1 = everything in one update
606 bool mEnhancedInternalEdgeRemoval; // Set to indicate that extra effort should be made to try to remove ghost contacts (collisions with internal edges of a mesh). This is more expensive but makes bodies move smoother over a mesh with convex edges.
607
608 // Character mass (kg)
609 float mMass;
610
611 // Maximum force with which the character can push other bodies (N)
612 float mMaxStrength;
613
614 // An extra offset applied to the shape in local space. This allows applying an extra offset to the shape in local space.
615 Vec3 mShapeOffset = Vec3::sZero();
616
617 // Current position (of the base, not the center of mass)
618 RVec3 mPosition = RVec3::sZero();
619
620 // Current rotation (of the base, not of the center of mass)
621 Quat mRotation = Quat::sIdentity();
622
623 // Current linear velocity
624 Vec3 mLinearVelocity = Vec3::sZero();
625
626 // List of contacts that were active in the last frame
627 ContactList mActiveContacts;
628
629 // Remembers the delta time of the last update
630 float mLastDeltaTime = 1.0f / 60.0f;
631
632 // Remember if we exceeded the maximum number of hits and had to remove similar contacts
633 mutable bool mMaxHitsExceeded = false;
634
635 // User data, can be used for anything by the application
636 uint64 mUserData = 0;
637
638 // The inner rigid body that proxies the character in the world
639 BodyID mInnerBodyID;
640};
641
EBackFaceMode
How collision detection functions will treat back facing triangles.
Definition BackFaceMode.h:11
@ CollideWithBackFaces
Collide with back facing surfaces/triangles.
#define JPH_EXPORT
Definition Core.h:236
std::uint64_t uint64
Definition Core.h:457
unsigned int uint
Definition Core.h:453
#define JPH_NAMESPACE_END
Definition Core.h:379
#define JPH_NAMESPACE_BEGIN
Definition Core.h:373
JPH_INLINE constexpr float DegreesToRadians(float inV)
Convert a value from degrees to radians.
Definition Math.h:13
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition Memory.h:31
EMotionType
Motion type of a physics body.
Definition MotionType.h:11
uint16 ObjectLayer
Definition ObjectLayer.h:16
JPH_INLINE float Cos(float inX)
Cosine of x (input in radians)
Definition Trigonometry.h:20
Definition Array.h:36
Class function to filter out bodies, returns true if test should collide with body.
Definition BodyFilter.h:16
Definition Body.h:35
ID of a body. This is a way of reasoning about bodies in a multithreaded simulation while avoiding ra...
Definition BodyID.h:13
Filter class for broadphase layers.
Definition BroadPhaseLayer.h:94
Base class for character class.
Definition CharacterBase.h:54
Base class for configuration of a character.
Definition CharacterBase.h:21
bool mEnhancedInternalEdgeRemoval
Set to indicate that extra effort should be made to try to remove ghost contacts (collisions with int...
Definition CharacterBase.h:45
Vec3 mUp
Vector indicating the up direction of the character.
Definition CharacterBase.h:34
This class receives callbacks when a virtual character hits something.
Definition CharacterVirtual.h:72
virtual void OnContactAdded(const CharacterVirtual *inCharacter, const BodyID &inBodyID2, const SubShapeID &inSubShapeID2, RVec3Arg inContactPosition, Vec3Arg inContactNormal, CharacterContactSettings &ioSettings)
Definition CharacterVirtual.h:94
virtual ~CharacterContactListener()=default
Destructor.
virtual bool OnContactValidate(const CharacterVirtual *inCharacter, const BodyID &inBodyID2, const SubShapeID &inSubShapeID2)
Checks if a character can collide with specified body. Return true if the contact is valid.
Definition CharacterVirtual.h:82
virtual void OnContactSolve(const CharacterVirtual *inCharacter, const BodyID &inBodyID2, const SubShapeID &inSubShapeID2, RVec3Arg inContactPosition, Vec3Arg inContactNormal, Vec3Arg inContactVelocity, const PhysicsMaterial *inContactMaterial, Vec3Arg inCharacterVelocity, Vec3 &ioNewCharacterVelocity)
Definition CharacterVirtual.h:109
virtual bool OnCharacterContactValidate(const CharacterVirtual *inCharacter, const CharacterVirtual *inOtherCharacter, const SubShapeID &inSubShapeID2)
Same as OnContactValidate but when colliding with a CharacterVirtual.
Definition CharacterVirtual.h:85
virtual void OnCharacterContactAdded(const CharacterVirtual *inCharacter, const CharacterVirtual *inOtherCharacter, const SubShapeID &inSubShapeID2, RVec3Arg inContactPosition, Vec3Arg inContactNormal, CharacterContactSettings &ioSettings)
Same as OnContactAdded but when colliding with a CharacterVirtual.
Definition CharacterVirtual.h:97
virtual void OnAdjustBodyVelocity(const CharacterVirtual *inCharacter, const Body &inBody2, Vec3 &ioLinearVelocity, Vec3 &ioAngularVelocity)
Definition CharacterVirtual.h:79
virtual void OnCharacterContactSolve(const CharacterVirtual *inCharacter, const CharacterVirtual *inOtherCharacter, const SubShapeID &inSubShapeID2, RVec3Arg inContactPosition, Vec3Arg inContactNormal, Vec3Arg inContactVelocity, const PhysicsMaterial *inContactMaterial, Vec3Arg inCharacterVelocity, Vec3 &ioNewCharacterVelocity)
Same as OnContactSolve but when colliding with a CharacterVirtual.
Definition CharacterVirtual.h:112
This class contains settings that allow you to override the behavior of a character's collision respo...
Definition CharacterVirtual.h:59
bool mCanPushCharacter
True when the object can push the virtual character.
Definition CharacterVirtual.h:62
bool mCanReceiveImpulses
Definition CharacterVirtual.h:67
Definition CharacterVirtual.h:165
void SetEnhancedInternalEdgeRemoval(bool inApply)
Definition CharacterVirtual.h:233
TransformedShape GetTransformedShape() const
Get the transformed shape that represents the volume of the character, can be used for collision chec...
Definition CharacterVirtual.h:359
float GetMass() const
Character mass (kg)
Definition CharacterVirtual.h:220
float GetPenetrationRecoverySpeed() const
This value governs how fast a penetration will be resolved, 0 = nothing is resolved,...
Definition CharacterVirtual.h:228
RVec3 GetPosition() const
Get the position of the character.
Definition CharacterVirtual.h:199
void SetLinearVelocity(Vec3Arg inLinearVelocity)
Set the linear velocity of the character (m / s)
Definition CharacterVirtual.h:196
const ContactList & GetActiveContacts() const
Access to the internal list of contacts that the character has found.
Definition CharacterVirtual.h:418
RMat44 GetWorldTransform() const
Calculate the world transform of the character.
Definition CharacterVirtual.h:214
void SetCharacterVsCharacterCollision(CharacterVsCharacterCollision *inCharacterVsCharacterCollision)
Set the character vs character collision interface.
Definition CharacterVirtual.h:190
RVec3 GetCenterOfMassPosition() const
Definition CharacterVirtual.h:211
bool GetMaxHitsExceeded() const
Definition CharacterVirtual.h:250
bool HasCollidedWith(const BodyID &inBody) const
Check if the character is currently in contact with or has collided with another body in the last tim...
Definition CharacterVirtual.h:421
void SetMaxStrength(float inMaxStrength)
Definition CharacterVirtual.h:225
float GetCharacterPadding() const
Character padding.
Definition CharacterVirtual.h:236
void SetMaxNumHits(uint inMaxHits)
Definition CharacterVirtual.h:240
uint GetMaxNumHits() const
Max num hits to collect in order to avoid excess of contact points collection.
Definition CharacterVirtual.h:239
float GetMaxStrength() const
Maximum force with which the character can push other bodies (N)
Definition CharacterVirtual.h:224
void SetShapeOffset(Vec3Arg inShapeOffset)
Definition CharacterVirtual.h:254
BodyID GetInnerBodyID() const
Optional inner rigid body that proxies the character in the world. Can be used to update body propert...
Definition CharacterVirtual.h:261
void SetMass(float inMass)
Definition CharacterVirtual.h:221
bool GetEnhancedInternalEdgeRemoval() const
Set to indicate that extra effort should be made to try to remove ghost contacts (collisions with int...
Definition CharacterVirtual.h:232
Vec3 GetLinearVelocity() const
Get the linear velocity of the character (m / s)
Definition CharacterVirtual.h:193
CharacterContactListener * GetListener() const
Get the current contact listener.
Definition CharacterVirtual.h:187
RMat44 GetCenterOfMassTransform() const
Calculates the transform for this character's center of mass.
Definition CharacterVirtual.h:217
CharacterVirtual(const CharacterVirtualSettings *inSettings, RVec3Arg inPosition, QuatArg inRotation, PhysicsSystem *inSystem)
Constructor without user data.
Definition CharacterVirtual.h:178
void SetRotation(QuatArg inRotation)
Set the rotation of the character.
Definition CharacterVirtual.h:208
void SetHitReductionCosMaxAngle(float inCosMaxAngle)
Definition CharacterVirtual.h:244
uint64 GetUserData() const
Access to the user data, can be used for anything by the application.
Definition CharacterVirtual.h:257
Vec3 GetShapeOffset() const
An extra offset applied to the shape in local space. This allows applying an extra offset to the shap...
Definition CharacterVirtual.h:253
void SetPenetrationRecoverySpeed(float inSpeed)
Definition CharacterVirtual.h:229
bool HasCollidedWith(const CharacterVirtual *inCharacter) const
Check if the character is currently in contact with or has collided with another character in the las...
Definition CharacterVirtual.h:430
float GetHitReductionCosMaxAngle() const
Cos(angle) where angle is the maximum angle between two hits contact normals that are allowed to be m...
Definition CharacterVirtual.h:243
void SetPosition(RVec3Arg inPosition)
Set the position of the character.
Definition CharacterVirtual.h:202
void SetListener(CharacterContactListener *inListener)
Set the contact listener.
Definition CharacterVirtual.h:184
Quat GetRotation() const
Get the rotation of the character.
Definition CharacterVirtual.h:205
Contains the configuration of a character.
Definition CharacterVirtual.h:22
RefConst< Shape > mInnerBodyShape
Definition CharacterVirtual.h:51
Definition CharacterVirtual.h:119
virtual void CollideCharacter(const CharacterVirtual *inCharacter, RMat44Arg inCenterOfMassTransform, const CollideShapeSettings &inCollideShapeSettings, RVec3Arg inBaseOffset, CollideShapeCollector &ioCollector) const =0
virtual ~CharacterVsCharacterCollision()=default
virtual void CastCharacter(const CharacterVirtual *inCharacter, RMat44Arg inCenterOfMassTransform, Vec3Arg inDirection, const ShapeCastSettings &inShapeCastSettings, RVec3Arg inBaseOffset, CastShapeCollector &ioCollector) const =0
Definition CharacterVirtual.h:144
void Add(CharacterVirtual *inCharacter)
Add a character to the list of characters to check collision against.
Definition CharacterVirtual.h:147
Array< CharacterVirtual * > mCharacters
The list of characters to check collision against.
Definition CharacterVirtual.h:156
Class that contains all information of two colliding shapes.
Definition CollideShape.h:19
Settings to be passed with a collision query.
Definition CollideShape.h:94
Virtual interface that allows collecting multiple collision results.
Definition CollisionCollector.h:45
Base class for all physics constraints. A constraint removes one or more degrees of freedom for a rig...
Definition Constraint.h:103
Holds a 4x4 matrix of floats, but supports also operations on the 3x3 upper left part of the matrix.
Definition Mat44.h:13
JPH_INLINE Mat44 PostTranslated(Vec3Arg inTranslation) const
Post multiply by translation matrix: result = Mat44::sTranslation(inTranslation) * this (i....
Definition Mat44.inl:903
JPH_INLINE Mat44 PreTranslated(Vec3Arg inTranslation) const
Pre multiply by translation matrix: result = this * Mat44::sTranslation(inTranslation)
Definition Mat44.inl:898
static JPH_INLINE Mat44 sRotationTranslation(QuatArg inR, Vec3Arg inT)
Get matrix that rotates and translates.
Definition Mat44.inl:149
Class that makes another class non-copyable. Usage: Inherit from NonCopyable.
Definition NonCopyable.h:11
Filter class for object layers.
Definition ObjectLayer.h:28
Definition PhysicsMaterial.h:23
Definition PhysicsSystem.h:29
An infinite plane described by the formula X . Normal + Constant = 0.
Definition Plane.h:11
Definition Quat.h:33
static JPH_INLINE Quat sIdentity()
Definition Quat.h:103
Definition Reference.h:163
Result of a shape cast test.
Definition ShapeCast.h:114
Settings to be passed with a shape cast.
Definition ShapeCast.h:92
Filter class.
Definition ShapeFilter.h:17
Base class for all shapes (collision volume of a body). Defines a virtual interface for collision det...
Definition Shape.h:186
virtual Vec3 GetCenterOfMass() const
All shapes are centered around their center of mass. This function returns the center of mass positio...
Definition Shape.h:211
Definition StateRecorder.h:105
A sub shape id contains a path to an element (usually a triangle or other primitive type) of a compou...
Definition SubShapeID.h:23
Definition TempAllocator.h:16
Definition TransformedShape.h:26
Definition Vec3.h:17
static JPH_INLINE Vec3 sZero()
Vector with all zeros.
Definition Vec3.inl:107
Definition CharacterVirtual.h:388
EMotionType mMotionTypeB
Motion type of B, used to determine the priority of the contact.
Definition CharacterVirtual.h:405
Vec3 mContactNormal
Contact normal, pointing towards the character.
Definition CharacterVirtual.h:398
const PhysicsMaterial * mMaterial
Material of B.
Definition CharacterVirtual.h:408
float mFraction
Fraction along the path where this contact takes place.
Definition CharacterVirtual.h:401
CharacterVirtual * mCharacterB
Character we're colliding with (if not null)
Definition CharacterVirtual.h:403
uint64 mUserData
User data of B.
Definition CharacterVirtual.h:407
BodyID mBodyB
ID of body we're colliding with (if not invalid)
Definition CharacterVirtual.h:402
SubShapeID mSubShapeIDB
Sub shape ID of body we're colliding with.
Definition CharacterVirtual.h:404
bool IsSameBody(const Contact &inOther) const
Definition CharacterVirtual.h:394
Vec3 mLinearVelocity
Velocity of the contact point.
Definition CharacterVirtual.h:397
float mDistance
Distance to the contact <= 0 means that it is an actual contact, > 0 means predictive.
Definition CharacterVirtual.h:400
Vec3 mSurfaceNormal
Surface normal of the contact.
Definition CharacterVirtual.h:399
RVec3 mPosition
Position where the character makes contact.
Definition CharacterVirtual.h:396
bool mIsSensorB
If B is a sensor.
Definition CharacterVirtual.h:406
Settings struct with settings for ExtendedUpdate.
Definition CharacterVirtual.h:314