Box2D的:如何获得传感器的位置? [英] Box2D: How to get the position of a sensor?

查看:337
本文介绍了Box2D的:如何获得传感器的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

修改我已经更新了这个问题在这里:<一href="http://stackoverflow.com/questions/5666834/box2d-how-to-get-the-position-of-a-static-object">Box2D:如何获取静态物体的位置?

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

我在Box2D的项目中使用的传感器。在带传感器的碰撞,我想知道在世界上的传感器的位置。我检测了接触听众的碰撞,但传感器的位置总是0 0

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.

这是我的code:(编辑,添加了全班)

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

public class 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?

我使用Box2DFlash版本2.1A,但我不认为这是特定于Flash。

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

修改我的code创建世界机构:

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;           
    }

从createWorldBody方法示例输出:

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;

您可以发布更多code关于设立机构和夹具?也许有一些线索那里,为什么你目前的code不能按预期工作。

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.

这篇关于Box2D的:如何获得传感器的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆