brad
brad

Reputation: 2249

on collision event handlers in box2djs

I'm playing around with box2djs. I'm trying to find a way to write an 'onCollision()' callback function, but the documentation is sparse and I can't find an obvious way to do it.

Thanks!

Upvotes: 1

Views: 808

Answers (1)

John Carter
John Carter

Reputation: 2056

Call back for box2djs is actually referenced as a collision "filter." Here is how you can implement it. I'll also show what I'm doing instead which maybe is a little slower but since my other approach is outside the step() I can destroy objects and stuff:

// Called whenever a collision occurs in the world
//

var JellyCollisionCallback = function()
    {
    // Required function - this is the function the gets called when b2ContactManager registers a collision between two bodies
    //
    this.ShouldCollide = function( shape1, shape2 )
        {
        // These are the two bodies…
        //
        var cBody1 = shape1.m_body;
        var cBody2 = shape2.m_body;

        // I'm setting userData when I create the body object
        //
        var jellyObject1 = cBody1.GetUserData();
        var jellyObject2 = cBody2.GetUserData();


        // This is the code from the default collision filter
        //
        if (shape1.m_groupIndex == shape2.m_groupIndex && shape1.m_groupIndex != 0)
            {
            return shape1.m_groupIndex > 0;
            }

        var collide = (shape1.m_maskBits & shape2.m_categoryBits) != 0 && (shape1.m_categoryBits & shape2.m_maskBits) != 0;

        return collide;
        }

    return this;
    }


function createWorld()
    {
    var world = new b2World(worldAABB, gravity, doSleep);

    myCollisionCallback = new JellyCollisionCallback();

    world.SetFilter(myCollisionCallback );
    }

probably not as nice as a true callback, but I had originally had trouble trying to get a callback to work so I wrote this approach and ended up keeping it instead. I do this in my main loop that calls the world.step()

// Find collisions between selected objects
//
// (world is the main b2World object)
//
var aContact;
for ( aContact = world.m_contactList; aContact != null; aContact = aContact.m_next )
    {
    var cBody1 = aContact.m_shape1.m_body;
    var cBody2 = aContact.m_shape2.m_body;

    // I'm setting userData when I create the body object
    //
    var jellyObject1 = cBody1.GetUserData();
    var jellyObject2 = cBody2.GetUserData();

    // Not one of my controlled Objects
    //
    if ( typeof(jellyObject1) != "object" || jellyObject1 == null )
        continue;
    if ( typeof(jellyObject2) != "object" || jellyObject2 == null )
        continue;

    // Call my collision event for the colliding objects
    //
    jellyObject1.dink();
    jellyObject2.dink();
    }

documentation is pretty much unavailable from what I found, but I really like box2djs and have finally figured out everything I need to accomplish some simple hobby projects. Here are some examples that extend the original box2djs demo jellyrobotics box2djs project

Upvotes: 1

Related Questions