Skip to content
Chandler edited this page Aug 3, 2016 · 3 revisions

When bodies in the world collide they emit various events that can be listened for. This allows you the ability to modify parameters of the collision or react to it in some way. The types of events are speculativeContact, contact, and endContact.

speculativeContact

A speculativeContact event is fired when two bodies are determined to be in contact. The event comes with two parameters, other_body and contact, and allows you to prevent the collision from being handled by returning false. For example, you could allow a body to intersect other bodies by a certain amount with the following.

body = new Goblin.RigidBody(someShape, someMass);
body.addListener(
    'speculativeContact',
    function( other_body, contact ) {
        if ( contact.penetration_depth < 0.5 ) {
            // prevent the contact when the intersection is less than 0.5 units
            return false;
        }
    }
);

contact

A contact event is emitted when two bodies come in contact and nothing has cancelled its speculativeContact. This event receives the same parameters as speculativeContact, other_body and contact.

body = new Goblin.RigidBody(someShape, someMass);
body.addListener(
    'contact',
    function( other_body, contact ) {
        // this body has come in `contact with` other_body and the details are provided by `contact`
    }
);

endContact

The endContact event is fired when a particular contact point between two objects is no longer valid. This does not mean that the bodies are no longer in contact (see the next event for that information). The endContact event has one parameter which tells you what the other body is.

body = new Goblin.RigidBody(someShape, someMass);
body.addListener(
    'endContact',
    function( other_body ) {
        // this body has lost a contact point with `other_body`
    }
);

endAllContact

The endAllContact event is fired when no more contacts exist between two bodies. Just like endContact, the endAllContact event has one parameter which tells you what the other body is.

body = new Goblin.RigidBody(someShape, someMass);
body.addListener(
    'endContact',
    function( other_body ) {
        // this body has lost a contact point with `other_body`
    }
);
Clone this wiki locally