开发者

Box2D: How to get the position of a sensor?

Edit I've updated this question here: Box2D: How to get the position of a static body?

I'm using sensors in a Box2D project. On a collision with a sensor, I'd like to know the sensor's position in the world. I'm detecting the collision with a contact listener, but the sensor's position is always 0 0.

From my code: (edit, added the whole class)

public cla开发者_运维知识库ss MyContactListener extends b2ContactListener {

    public var onSensorEvent:Function;

    override public function BeginContact(contact:b2Contact):void {                 
        if(contact.GetFixtureA().IsSensor()) {
            var ud1:GameEntity = contact.GetFixtureA().GetBody().GetUserData();
            Util.traceVec(contact.GetFixtureA().GetBody().GetTransform().position);
            Util.traceVec(contact.GetFixtureB().GetBody().GetTransform().position);
            Util.traceVec(contact.GetFixtureA().GetBody().GetPosition());
            Util.traceVec(contact.GetFixtureB().GetBody().GetPosition());
            onSensorEvent(ud1);
        }

        if(contact.GetFixtureB().IsSensor()) {
            var ud2:GameEntity = contact.GetFixtureB().GetBody().GetUserData();
            Util.traceVec(contact.GetFixtureA().GetBody().GetTransform().position);
            Util.traceVec(contact.GetFixtureB().GetBody().GetTransform().position);
            Util.traceVec(contact.GetFixtureA().GetBody().GetPosition());
            Util.traceVec(contact.GetFixtureB().GetBody().GetPosition());
            onSensorEvent(ud2);
        }
    }       
}

Other than this, the sensor works normally and registers collisions as normal. Any ideas why might this be happening, and how I can fix it?

I'm using Box2DFlash version 2.1a, but I don't expect this is specific to Flash.

Edit my code to create bodies in the world:

    private function createWorldBody(x_origin:Number, y_origin:Number, box_width:Number, box_height:Number, angle:Number, 
                                     density:Number, friction:Number, bounce:Number, entity:GameEntity):b2Body {

        trace("creating body: x=" + x_origin + " y="+ y_origin + " w=" + box_width + " h=" + box_height); 
        var body:b2BodyDef= new b2BodyDef();
        if(entity.type == GameEntity.TYPE_PLAYER) body.type = b2Body.b2_dynamicBody;

        // shape
        var box:b2PolygonShape = new b2PolygonShape();
        if(entity.type == GameEntity.TYPE_PLAYER) {
            box.SetAsOrientedBox(box_width/2/pixelsPerMeter, box_height/2/pixelsPerMeter, new b2Vec2(0,0), angle);
        } else {
            box.SetAsOrientedBox(box_width/2/pixelsPerMeter, box_height/2/pixelsPerMeter, new b2Vec2(x_origin/pixelsPerMeter,y_origin/pixelsPerMeter), angle);
        }

        // fixture
        var fixture:b2FixtureDef = new b2FixtureDef();
        fixture.shape = box;
        fixture.density = density;
        fixture.friction = friction;
        fixture.restitution = bounce;
        if(entity.type == GameEntity.TYPE_ANIMATION_TRIGGER) {
            fixture.isSensor = true;
        }

        // world body
        var worldBody:b2Body = world.CreateBody(body);
        worldBody.CreateFixture(fixture);

        if(entity.type == GameEntity.TYPE_PLAYER) {
            worldBody.SetPosition(new b2Vec2(0, 6));
            worldBody.SetAngularDamping(playerAngDamping);
        } else {
            worldBody.SetAngularDamping(0.1);
        }

        Util.traceVec(worldBody.GetPosition());
        worldBody.SetUserData(entity);
        return worldBody;           
    }

Sample output from the createWorldBody method:

creating body: x=10768.85 y=129.1 w=242.1 h=508.2 angle=0
0 0 
creating body: x=13314.9 y=-176.35 w=176.05 h=39.4 angle=0
0 0
creating body: x=14324.4 y=304.3 w=116.05 h=207.6 angle=0
0 0
creating body: x=12819.449999999999 y=336.5 w=905.3 h=156.2 angle=0
0 0

Sample output from the contact listener after two collisions:

20.84107631571526 6.26063858024741
0 0
20.84107631571526 6.26063858024741
0 0
34.444376903802855 4.2002747636658855
0 0
34.444376903802855 4.2002747636658855
0 0

Edit I think I see it: it's because the sensors are static bodies, which don't have 'real' positions in the world.


As a test, you may want to use the body's transform, as in:

contact.GetFixtureB().GetBody().GetTransform().position;

Can you post more code pertaining to the creation of the bodies and fixtures? Maybe there are some clues there as to why you current code is not working as expected.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜