我一直在使用Jonas Wagner的JS Box2D port,并且遇到了形状用户数据的奇怪问题。我将我的实体设置为具有碰撞形状以及辅助“脚”传感器形状,以确定我的物体何时在坚实的地面上。定义看起来有点像这样:
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();
这里的想法是我可以检测到我的脚传感器何时停止与实体碰撞。一切都按预期工作,我的b2.ContactListener正确报告对象何时停止与我的脚传感器碰撞。问题是我没有正确报告我分配给我的脚形的userData。
如下所示,我的b2.ContactListener的Remove回调中返回的point对象显然包含一个形状(shape2),其m_userData属性设置为'feet'。但是,查询shape2对象直接将其m_userData报告为null。
我已经包含了Safari的调试控制台的屏幕截图,其执行如下所示的console.log。这是怎么回事?!
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);
}