如何使box2d身体对象与另一个身体碰撞 [英] How to make a box2d Body object collidable with the other Body

查看:112
本文介绍了如何使box2d身体对象与另一个身体碰撞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当两种正文类型均为动态时,如何在使用Box2d扩展名时使com.badlogic.gdx.physics.box2d.Body对象与Scene2d(libgdx)中的另一个Body发生碰撞??

How to make a com.badlogic.gdx.physics.box2d.Body object collidable with the other Body in Scene2d(libgdx) while working with the Box2d extension, when both the body types are Dynamic.?

推荐答案

如果我正确理解,您的问题会有所帮助

if I understand correctly, your question can this helps

在创建Body2的位置,在代码中添加以下示例:

where to create the Body2, add this example in your code:

.//
YourBody2 = YourWorld.createBody(bd);

YourBody2.createFixture(fixDef).setUserData("YourBody2");

在创建Body1的位置,在代码中添加以下示例:

where to create the Body1, add this example in your code:

.//
YourBody1 = YourWorld.createBody(bd);

YourBody1.createFixture(fixDef).setUserData("YourBody1");

现在您必须使用联系人监听器,这是一个示例,

Now you have to use a contactlistener, this is an example,

 private class CrearContactListener implements ContactListener {

        public CrearContactListener(){

        }
        @Override
        public void preSolve(Contact contact, Manifold oldManifold) {
            // TODO Auto-generated method stub

        }

        @Override
        public void postSolve(Contact contact, ContactImpulse impulse) {
            // TODO Auto-generated method stub

        }

        @Override
        public void endContact(Contact contact) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beginContact(Contact contact) {
            // TODO Auto-generated method stub

            Fixture fixtureA = contact.getFixtureA();
            Fixture fixtureB = contact.getFixtureB();

            if(fixtureA.getUserData() != null && fixtureA.getUserData().equals("YourBody1") 
               && fixtureB.getUserData() != null && fixtureB.getUserData().equals("YourBody2")){

                Gdx.app.log("Contact","1");
            }

            if(fixtureB.getUserData() != null && fixtureB.getUserData().equals("YourBody1")
                && fixtureA.getUserData() != null && fixtureA.getUserData().equals("YourBody2")){

                Gdx.app.log("Contact","2");
            }

        }
}

创建世界的位置,在代码中添加以下示例:

where to create the World, add this example in your code:

.//

YourWorld = new World(..., ....);

YourWorld.setContactListener(new CrearContactListener());

您可以查看 http://box2d.org/manual.pdf ,并阅读有关preSolve和更多

you can look http://box2d.org/manual.pdf, and read about preSolve and more ect

这篇关于如何使box2d身体对象与另一个身体碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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