What Info is in the planck.js EndContact Event?
In planck.js—a 2D physics engine for JavaScript games and
simulations—the EndContact event is triggered immediately
after two fixtures stop touching. This article provides a quick
breakdown of the specific information you can extract from this event,
including access to the underlying Contact object, the
colliding fixtures, the parent bodies, and why contact state data like
manifolds are no longer available at this stage of the collision
lifecycle.
Accessing the Contact Object
When the EndContact event fires, the listener receives a
Contact object as its primary argument. This object is the
gateway to understanding which elements in your physics world have just
separated.
world.on('end-contact', function(contact) {
// Your extraction code here
});Key Data Extracted from the Event
- The Fixtures (
getFixtureA()andgetFixtureB()): You can determine exactly which parts of your shapes broke contact. This is crucial if a single rigid body has multiple fixtures (e.g., a character body fixture and a separate sensor fixture for detecting ground contact). - The Bodies (
getBody()): By callingcontact.getFixtureA().getBody()andcontact.getFixtureB().getBody(), you can access the parentBodyobjects. This allows you to update game logic, such as switching a character’s state from “grounded” to “jumping/falling” the moment they leave a platform. - User Data (
getUserData()): If you attached custom game properties, IDs, or labels to your fixtures or bodies during creation, you can retrieve them here to identify what game entities just separated.
Limitations: What You Cannot Extract
It is equally important to know what information is not
reliable during an EndContact event. Because the shapes are
no longer intersecting, the engine clears the geometric collision
data.
- No Contact Manifolds: Methods like
contact.getManifold()will not yield useful geometric data because the contact points no longer exist. - No Impulse or Normal Vectors: If you need to know
the final direction or force of the separation, you cannot get it from
EndContact. That data must be captured during the precedingPostSolveorPreSolvesteps while the objects are still touching.