【问题标题】:Box2D: How to get the position of a sensor?Box2D:如何获取传感器的位置?
【发布时间】:2011-04-14 14:40:18
【问题描述】:

编辑我在这里更新了这个问题:Box2D: How to get the position of a static body?

我在 Box2D 项目中使用传感器。在与传感器发生碰撞时,我想知道传感器在世界上的位置。我正在检测与接触侦听器的碰撞,但传感器的位置始终为 0 0。

来自我的代码:(编辑,添加了整个类)

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

除此之外,传感器正常工作并正常记录碰撞。任何想法为什么会发生这种情况,以及我该如何解决?

我使用的是 Box2DFlash 2.1a 版,但我不认为这是 Flash 特有的。

编辑我的代码以在世界上创建身体:

    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 方法的示例输出:

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

两次碰撞后接触侦听器的示例输出:

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

编辑我想我明白了:这是因为传感器是静态物体,它们在世界上没有“真实”的位置。

【问题讨论】:

    标签: flash actionscript-3 box2d collision sensors


    【解决方案1】:

    作为测试,您可能希望使用身体的变换,如下所示:

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

    您能否发布更多有关创建主体和固定装置的代码?也许有一些线索可以说明为什么您当前的代码没有按预期工作。

    【讨论】:

    • 谢谢,但不幸的是它也给了零。我添加了更多代码来展示如何创建身体和固定装置,以及如何正确检索玩家的位置。非常感谢您对此的任何帮助。
    猜你喜欢
    • 1970-01-01
    • 2021-05-15
    • 1970-01-01
    • 2012-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-23
    • 1970-01-01
    相关资源
    最近更新 更多