Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
ContactListener Class Reference

#include <ContactListener.h>

Public Member Functions

virtual ~ContactListener ()=default
 Ensure virtual destructor.
 
virtual ValidateResult OnContactValidate (const Body &inBody1, const Body &inBody2, RVec3Arg inBaseOffset, const CollideShapeResult &inCollisionResult)
 
virtual void OnContactAdded (const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &ioSettings)
 
virtual void OnContactPersisted (const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &ioSettings)
 
virtual void OnContactRemoved (const SubShapeIDPair &inSubShapePair)
 

Detailed Description

A listener class that receives collision contact events. It can be registered with the ContactConstraintManager (or PhysicsSystem). Note that contact listener callbacks are called from multiple threads at the same time when all bodies are locked, you're only allowed to read from the bodies and you can't change physics state.

Constructor & Destructor Documentation

◆ ~ContactListener()

virtual ContactListener::~ContactListener ( )
virtualdefault

Ensure virtual destructor.

Member Function Documentation

◆ OnContactAdded()

virtual void ContactListener::OnContactAdded ( const Body inBody1,
const Body inBody2,
const ContactManifold inManifold,
ContactSettings ioSettings 
)
inlinevirtual

Called whenever a new contact point is detected. Note that this callback is called when all bodies are locked, so don't use any locking functions! Body 1 and 2 will be sorted such that body 1 ID < body 2 ID, so body 1 may not be dynamic. Note that only active bodies will report contacts, as soon as a body goes to sleep the contacts between that body and all other bodies will receive an OnContactRemoved callback, if this is the case then Body::IsActive() will return false during the callback. When contacts are added, the constraint solver has not run yet, so the collision impulse is unknown at that point. The velocities of inBody1 and inBody2 are the velocities before the contact has been resolved, so you can use this to estimate the collision impulse to e.g. determine the volume of the impact sound to play (see: EstimateCollisionResponse).

◆ OnContactPersisted()

virtual void ContactListener::OnContactPersisted ( const Body inBody1,
const Body inBody2,
const ContactManifold inManifold,
ContactSettings ioSettings 
)
inlinevirtual

Called whenever a contact is detected that was also detected last update. Note that this callback is called when all bodies are locked, so don't use any locking functions! Body 1 and 2 will be sorted such that body 1 ID < body 2 ID, so body 1 may not be dynamic. If the structure of the shape of a body changes between simulation steps (e.g. by adding/removing a child shape of a compound shape), it is possible that the same sub shape ID used to identify the removed child shape is now reused for a different child shape. The physics system cannot detect this, so may send a 'contact persisted' callback even though the contact is now on a different child shape. You can detect this by keeping the old shape (before adding/removing a part) around until the next PhysicsSystem::Update (when the OnContactPersisted callbacks are triggered) and resolving the sub shape ID against both the old and new shape to see if they still refer to the same child shape.

◆ OnContactRemoved()

virtual void ContactListener::OnContactRemoved ( const SubShapeIDPair inSubShapePair)
inlinevirtual

Called whenever a contact was detected last update but is not detected anymore. Note that this callback is called when all bodies are locked, so don't use any locking functions! Note that we're using BodyID's since the bodies may have been removed at the time of callback. Body 1 and 2 will be sorted such that body 1 ID < body 2 ID, so body 1 may not be dynamic. The sub shape ID were created in the previous simulation step too, so if the structure of a shape changes (e.g. by adding/removing a child shape of a compound shape), the sub shape ID may not be valid / may not point to the same sub shape anymore. If you want to know if this is the last contact between the two bodies, use PhysicsSystem::WereBodiesInContact.

◆ OnContactValidate()

virtual ValidateResult ContactListener::OnContactValidate ( const Body inBody1,
const Body inBody2,
RVec3Arg  inBaseOffset,
const CollideShapeResult inCollisionResult 
)
inlinevirtual

Called after detecting a collision between a body pair, but before calling OnContactAdded and before adding the contact constraint. If the function rejects the contact, the contact will not be added and any other contacts between this body pair will not be processed. This function will only be called once per PhysicsSystem::Update per body pair and may not be called again the next update if a contact persists and no new contact pairs between sub shapes are found. This is a rather expensive time to reject a contact point since a lot of the collision detection has happened already, make sure you filter out the majority of undesired body pairs through the ObjectLayerPairFilter that is registered on the PhysicsSystem. Note that this callback is called when all bodies are locked, so don't use any locking functions! Body 1 will have a motion type that is larger or equal than body 2's motion type (order from large to small: dynamic -> kinematic -> static). When motion types are equal, they are ordered by BodyID. The collision result (inCollisionResult) is reported relative to inBaseOffset.


The documentation for this class was generated from the following file: