You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Jolt/Physics/Collision/ContactListener.h
+31-11Lines changed: 31 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -61,9 +61,15 @@ enum class ValidateResult
61
61
RejectAllContactsForThisBodyPair ///< Rejects this and any further contact points for this body pair
62
62
};
63
63
64
-
/// A listener class that receives collision contact events.
65
-
/// It can be registered with the ContactConstraintManager (or PhysicsSystem).
66
-
/// 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.
64
+
/// A listener class that receives collision contact events. It can be registered through PhysicsSystem::SetContactListener.
65
+
/// Only a single contact listener can be registered. A common pattern is to create a contact listener that casts Body::GetUserData
66
+
/// to a game object and then forwards the call to a handler specific for that game object.
67
+
/// Typically this is done on both objects involved in a collision event.
68
+
///
69
+
/// Note that contact listener callbacks are called from multiple threads at the same time when all bodies are locked, this means you cannot
70
+
/// use PhysicsSystem::GetBodyInterface / PhysicsSystem::GetBodyLockInterface but must use PhysicsSystem::GetBodyInterfaceNoLock / PhysicsSystem::GetBodyLockInterfaceNoLock instead.
71
+
/// If you use a locking interface, the simulation will deadlock. You're only allowed to read from the bodies and you can't change physics state.
72
+
///
67
73
/// During OnContactRemoved you cannot access the bodies at all, see the comments at that function.
68
74
classContactListener
69
75
{
@@ -72,29 +78,39 @@ class ContactListener
72
78
virtual~ContactListener() = default;
73
79
74
80
/// Called after detecting a collision between a body pair, but before calling OnContactAdded and before adding the contact constraint.
75
-
/// If the function rejects the contact, the contact will not be added and any other contacts between this body pair will not be processed.
76
-
/// This function will only be called once per PhysicsSystem::Update per body pair and may not be called again the next update
77
-
/// if a contact persists and no new contact pairs between sub shapes are found.
81
+
/// If the function rejects the contact, the contact will not be processed by the simulation.
78
82
/// This is a rather expensive time to reject a contact point since a lot of the collision detection has happened already, make sure you
79
83
/// filter out the majority of undesired body pairs through the ObjectLayerPairFilter that is registered on the PhysicsSystem.
80
-
/// Note that this callback is called when all bodies are locked, so don't use any locking functions!
84
+
///
85
+
/// This function may not be called again the next update if a contact persists and no new contact pairs between sub shapes are found.
86
+
///
87
+
/// Note that this callback is called when all bodies are locked, so don't use any locking functions! See detailed class description of ContactListener.
88
+
///
81
89
/// 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.
90
+
///
82
91
/// The collision result (inCollisionResult) is reported relative to inBaseOffset.
83
92
virtual ValidateResult OnContactValidate([[maybe_unused]]const Body &inBody1, [[maybe_unused]]const Body &inBody2, [[maybe_unused]] RVec3Arg inBaseOffset, [[maybe_unused]]const CollideShapeResult &inCollisionResult) { return ValidateResult::AcceptAllContactsForThisBodyPair; }
84
93
85
94
/// Called whenever a new contact point is detected.
86
-
/// Note that this callback is called when all bodies are locked, so don't use any locking functions!
95
+
///
96
+
/// Note that this callback is called when all bodies are locked, so don't use any locking functions! See detailed class description of ContactListener.
97
+
///
87
98
/// Body 1 and 2 will be sorted such that body 1 ID < body 2 ID, so body 1 may not be dynamic.
99
+
///
88
100
/// Note that only active bodies will report contacts, as soon as a body goes to sleep the contacts between that body and all other
89
101
/// bodies will receive an OnContactRemoved callback, if this is the case then Body::IsActive() will return false during the callback.
102
+
///
90
103
/// When contacts are added, the constraint solver has not run yet, so the collision impulse is unknown at that point.
91
104
/// The velocities of inBody1 and inBody2 are the velocities before the contact has been resolved, so you can use this to
92
105
/// estimate the collision impulse to e.g. determine the volume of the impact sound to play (see: EstimateCollisionResponse).
93
106
virtualvoidOnContactAdded([[maybe_unused]]const Body &inBody1, [[maybe_unused]]const Body &inBody2, [[maybe_unused]]const ContactManifold &inManifold, [[maybe_unused]] ContactSettings &ioSettings) { /* Do nothing */ }
94
107
95
108
/// Called whenever a contact is detected that was also detected last update.
96
-
/// Note that this callback is called when all bodies are locked, so don't use any locking functions!
109
+
///
110
+
/// Note that this callback is called when all bodies are locked, so don't use any locking functions! See detailed class description of ContactListener.
111
+
///
97
112
/// Body 1 and 2 will be sorted such that body 1 ID < body 2 ID, so body 1 may not be dynamic.
113
+
///
98
114
/// 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),
99
115
/// 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
100
116
/// system cannot detect this, so may send a 'contact persisted' callback even though the contact is now on a different child shape. You can
@@ -103,14 +119,18 @@ class ContactListener
103
119
virtualvoidOnContactPersisted([[maybe_unused]]const Body &inBody1, [[maybe_unused]]const Body &inBody2, [[maybe_unused]]const ContactManifold &inManifold, [[maybe_unused]] ContactSettings &ioSettings) { /* Do nothing */ }
104
120
105
121
/// Called whenever a contact was detected last update but is not detected anymore.
122
+
///
106
123
/// You cannot access the bodies at the time of this callback because:
107
124
/// - All bodies are locked at the time of this callback.
108
125
/// - Some properties of the bodies are being modified from another thread at the same time.
109
126
/// - The body may have been removed and destroyed (you'll receive an OnContactRemoved callback in the PhysicsSystem::Update after the body has been removed).
127
+
///
110
128
/// Cache what you need in the OnContactAdded and OnContactPersisted callbacks and store it in a separate structure to use during this callback.
111
-
/// Alternatively, you could just record that the contact was removed and process it after PhysicsSimulation::Update.
129
+
/// Alternatively, you could just record that the contact was removed and process it after PhysicsSystem::Update.
130
+
///
112
131
/// Body 1 and 2 will be sorted such that body 1 ID < body 2 ID, so body 1 may not be dynamic.
113
-
/// 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),
132
+
///
133
+
/// The sub shape IDs were created in the previous simulation step, so if the structure of a shape changes (e.g. by adding/removing a child shape of a compound shape),
114
134
/// the sub shape ID may not be valid / may not point to the same sub shape anymore.
115
135
/// If you want to know if this is the last contact between the two bodies, use PhysicsSystem::WereBodiesInContact.
116
136
virtualvoidOnContactRemoved([[maybe_unused]]const SubShapeIDPair &inSubShapePair) { /* Do nothing */ }
0 commit comments