本文介绍了碰撞后如何使两个身体粘住?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的坚持这个我可以成功检测到碰撞,但是我无法让碰撞中的两个物体粘在一起。

I am really stuck on this I can successfully detect a collision but I can't make the two bodies involved in the collision to stick.

这是我的 ContactListener

world.setContactListener(listener);

    listener = new ContactListener() {

        @Override
        public void preSolve(Contact contact, Manifold oldManifold) {

        }


        @Override
        public void postSolve(Contact contact, ContactImpulse impulse) {

        }

        //called when two fixtures cease to touch
        @Override
        public void endContact(Contact contact) {
            Fixture fixtureA = contact.getFixtureA();
            Fixture fixtureB = contact.getFixtureB();
            Gdx.app.log("beginContact", "between" + fixtureA.toString() + "and" + fixtureB.toString());
        }

        //called when two fixtures begin to touch
        @Override
        public void beginContact(Contact contact) {
            Fixture fixtureA = contact.getFixtureA();
            Fixture fixtureB = contact.getFixtureB();
            Gdx.app.log("beginContact", "between" + fixtureA.toString() + "and" + fixtureB.toString());
        }
    };

这也是我在world.step()行之后直接放在我的render()中的内容/ p>

Also this is what I put in my render() straight after the world.step() line

int numContacts = world.getContactCount();

    if(numContacts > 0)
    {
        Gdx.app.log("contact", "start of contact list");
        for(Contact contact: world.getContactList())
        {
            Fixture fixtureA = contact.getFixtureA();
            Fixture fixtureB = contact.getFixtureB();
            Gdx.app.log("contact", "between" + fixtureA.toString() + "and" + fixtureB.toString());
        }
        Gdx.app.log("contact", "end of contact list");
    }

我非常坚持在解决问题或预先解决的问题上困惑。我按照iforce2d粘性射弹但是我不懂C ++,在eclipse中工作时会遇到很多语法错误。请有人给我看一个工作碰撞的示例代码,在java中碰撞后机体粘在一起。

I am extremely stuck on what to put on post solve or pre-solve really confused. I followed the iforce2d sticky projectiles http://www.iforce2d.net/b2dtut/sticky-projectiles but I don't understand C++ and i get a lot of syntax errors when working in eclipse. Please can someone show me an example code of a working collision where bodies stick together after colliding in java please.

推荐答案

这是怎么回事你创建了一个,创建一个包含2个实体的对象:

Ok, like in the iforce2d tutorial,create an object to contain 2 bodies:

public class StickyInfo{
    Body bodyA;
    Body bodyB;
    public StickyInfo(Body bodyA, Body bodyB){
        this.bodyA = bodyA;
        this.bodyB = bodyB;
    }
};

然后创建

Array<StickyInfo> collisionsToMakeSticky = new Array<StickyInfo>();

当身体碰撞时(好吧,技术上他们的固定装置),将它们添加到此列表中:

When the bodies collide (well, technically their fixtures), add them to this list:

collisionsToMakeSticky.add(new StickyInfo(body1, body2))

然后在 world.step 之后,如果数组不为空。创建WeldJoints:

And then just after world.step, if the Array is not empty. Create the WeldJoints:

while(collisionsToMakeSticky.size>0){
    StickyInfo si = collisionsToMakeSticky.removeIndex(0);
    //Make the WeldJoint with the bodies si.bodyA and si.bodyB
}

这篇关于碰撞后如何使两个身体粘住?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 12:05