1

I've been using Jonas Wagner's JS Box2D port and am running into a strange problem with shape userdata. I've setup my entity to have a collision shape as well as a secondary 'foot' sensor shape to determine when my object is on solid ground. The definition looks a little like this:

var bodyDef = new b2.BodyDef();
bodyDef.position.Set(
    (this.pos.x + this.size.x / 2) * b2.SCALE,
    (this.pos.y + this.size.y / 2) * b2.SCALE
);
this.body = ig.world.CreateBody(bodyDef);

var shapeDef = new b2.PolygonDef();
shapeDef.SetAsBox(
    this.size.x / 2 * b2.SCALE,
    this.size.y / 2 * b2.SCALE
);
shapeDef.density = 0.85;
shapeDef.filter.groupIndex = -1; 
this.body.CreateShape(shapeDef);

var footShapeDef = new b2.PolygonDef();
footShapeDef.SetAsOrientedBox(
    3 * b2.SCALE,
    3 * b2.SCALE,
    new b2.Vec2(0,7*b2.SCALE),
    0
);
footShapeDef.isSensor = true;
footShapeDef.density = 0.85;
footShapeDef.filter.groupIndex = -1; 

var footShape = this.body.CreateShape(footShapeDef);
footShape.SetUserData("feet");

this.body.SetMassFromShapes();

The idea here being that I can detect when my foot sensor stops colliding with entities. Everything is working as expected and my b2.ContactListener is correctly reporting when objects stop colliding with my foot sensor. The problem is that the userData I'm assigning to my foot shape isn't being correctly reported.

As you can see below, the point object returned in my b2.ContactListener's Remove callback clearly contains a shape (shape2) with it's m_userData attribute set to 'feet'. However, querying the shape2 object directly reports it's m_userData as null.

I've included a screenshot of Safari's debug console performing the console.log shown below. What's going on here?!

var listener = new b2.ContactListener();
listener.Remove = function(point)
{
    var p = point;
    var s1 = p.shape1;
    var s2 = p.shape2;

    console.log(p, s1, s2);
}

enter image description here

4

0 回答 0