box2d,如何检测静态和动态物体之间的碰撞

发布于 2024-10-31 14:17:39 字数 936 浏览 0 评论 0原文

我正在聆听动态物体之间的接触。但无法聆听动静体之间的接触。

我可以使用此方法处理 b2contactListener 类上的碰撞检测。

    public override function BeginContact(contact:b2Contact):void {
        // getting the fixtures that collided
        var fixtureA:b2Fixture=contact.GetFixtureA();
        var fixtureB:b2Fixture=contact.GetFixtureB();
        // if the fixture is a sensor, mark the parent body to be removed

        if(fixtureB.GetBody().getUniqueBodyName() == "towerBody"){
            //Towerbody is static body
                    trace("BINGOOOOOO");
                    }  

        if (fixtureA.GetBody().getUniqueBodyName() == "bomb"){
            // Bomb is dynamic body
        //  main.gamePhysics.destroyBodyWhenCollide(fixtureA.GetBody());

            var spr:Sprite = fixtureA.GetBody().GetUserData();
            spr.alpha = 0;
            fixtureA.GetBody().collisionDetected = true;            

        }

请帮帮我...

i am listening contact between dynamic bodies. But cannot listen contact between dynamic and static bodies .

I can handle collision detection on my b2contactListener class with this metod.

    public override function BeginContact(contact:b2Contact):void {
        // getting the fixtures that collided
        var fixtureA:b2Fixture=contact.GetFixtureA();
        var fixtureB:b2Fixture=contact.GetFixtureB();
        // if the fixture is a sensor, mark the parent body to be removed

        if(fixtureB.GetBody().getUniqueBodyName() == "towerBody"){
            //Towerbody is static body
                    trace("BINGOOOOOO");
                    }  

        if (fixtureA.GetBody().getUniqueBodyName() == "bomb"){
            // Bomb is dynamic body
        //  main.gamePhysics.destroyBodyWhenCollide(fixtureA.GetBody());

            var spr:Sprite = fixtureA.GetBody().GetUserData();
            spr.alpha = 0;
            fixtureA.GetBody().collisionDetected = true;            

        }

Plz help me out...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆伤 2024-11-07 14:17:39

静态主体将是fixtureB 并不是一个有效的假设...它可能是fixtureA。

尝试测试一下...

public override function BeginContact(contact:b2Contact):void {
    // getting the fixtures that collided
    var fixtureA:b2Fixture=contact.GetFixtureA();
    var fixtureB:b2Fixture=contact.GetFixtureB();
    // if the fixture is a sensor, mark the parent body to be removed

    if(fixtureA.GetBody().getUniqueBodyName() == "towerBody"){
        //Towerbody is static body
                trace("BINGOOOOOO - A");
                }  

    if(fixtureB.GetBody().getUniqueBodyName() == "towerBody"){
        //Towerbody is static body
                trace("BINGOOOOOO - B");
                }  


    if (fixtureA.GetBody().getUniqueBodyName() == "bomb"){
        // Bomb is dynamic body
    //  main.gamePhysics.destroyBodyWhenCollide(fixtureA.GetBody());

        var spr:Sprite = fixtureA.GetBody().GetUserData();
        spr.alpha = 0;
        fixtureA.GetBody().collisionDetected = true;            

    }

It's not a valid assumption that the static body will be fixtureB... it could be fixtureA.

Try testing this out...

public override function BeginContact(contact:b2Contact):void {
    // getting the fixtures that collided
    var fixtureA:b2Fixture=contact.GetFixtureA();
    var fixtureB:b2Fixture=contact.GetFixtureB();
    // if the fixture is a sensor, mark the parent body to be removed

    if(fixtureA.GetBody().getUniqueBodyName() == "towerBody"){
        //Towerbody is static body
                trace("BINGOOOOOO - A");
                }  

    if(fixtureB.GetBody().getUniqueBodyName() == "towerBody"){
        //Towerbody is static body
                trace("BINGOOOOOO - B");
                }  


    if (fixtureA.GetBody().getUniqueBodyName() == "bomb"){
        // Bomb is dynamic body
    //  main.gamePhysics.destroyBodyWhenCollide(fixtureA.GetBody());

        var spr:Sprite = fixtureA.GetBody().GetUserData();
        spr.alpha = 0;
        fixtureA.GetBody().collisionDetected = true;            

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文