Set contact enabled in box2d andengine

放肆的年华 提交于 2020-01-06 08:39:17

问题


How to disable the contact between two bodies in box2d (Andengine). I have used contact.setEnabled(false) but this was not working for me. I have given code below for reference.

    @Override
    public void beginContact(final Contact pContact) 
    {
        final Fixture fixtureA = pContact.getFixtureA();
        final Body bodyA = fixtureA.getBody();
        final Object userDataA = bodyA.getUserData();

        final Fixture fixtureB = pContact.getFixtureB();
        final Body bodyB = fixtureB.getBody();
        final Object userDataB = bodyB.getUserData();


        if(userDataA==null || userDataB==null)
            return;


        if(userDataA.equals(target) && userDataB.equals(ball)
        {   
              pContact.setEnabled(false);             

            }               
    }

回答1:


    @Override
    public void preSolve(Contact contact, Manifold oldManifold)
    {
        PhysicsConnectorManager mPC =     this.mPhysicsWorld.getPhysicsConnectorManager();
        Body targetBody = mPC.findBodyByShape(target);
        Body ballBody = mPC.findBodyByShape(ball);
        if (contact.getFixtureA().getBody() == targetBody && contact.getFixtureB().getBody() == ballBody)
        {
            contact.setEnabled(false);
        }
        else
        {
            contact.setEnabled(true);
        }
    }



回答2:


As far as Box2D is concerned your code looks mostly correct, apart from that you will also need to check if the ball and the target are the other way around:

if( (userDataA.equals(target) && userDataB.equals(ball)) || 
    (userDataB.equals(target) && userDataA.equals(ball)) )

But I think your main problem might be the use of final... why final? I am no java expert but it looks very suspicious and a quick look at wikipedia says: "A final variable can only be initialized once". To keep the contact disabled you'll need to do SetEnabled(false) every frame.




回答3:


I had the same problem and thanks to @Rooban and his comment "fixture.isSensor=true;" I solved it. I'm working with box2dweb (JavaScript port) and because all box2d APIs are similar this might help you.

Let say you have two objects, a WALL and a TOY. The TOY is passing through the WALL.

    //WALL (toy detector)

    var toyDetectorFixDef = new box2d.b2FixtureDef();
    toyDetectorFixDef.density = 1;
    toyDetectorFixDef.friction = 0.5;
    toyDetectorFixDef.restitution = 0.5;
    toyDetectorFixDef.shape = new box2d.b2PolygonShape;
    toyDetectorFixDef.shape.SetAsBox(80 / SCALE, 5 / SCALE);//, new box2d.b2Vec2(bodyDef3.x, bodyDef3.y ), 4);

    //BE SURE YOU ADDED THIS LINE, SO THIS OBJECT IS ACTING AS A SENSOR
    toyDetectorFixDef.isSensor = true;

    var toyDetectorBodyDef = new box2d.b2BodyDef();
    toyDetectorBodyDef.type = box2d.b2Body.b2_staticBody;
    toyDetectorBodyDef.position.x = 100 / SCALE;
    toyDetectorBodyDef.position.y = 280 / SCALE;

    var rbDataToyDetector = new Object();
    rbDataToyDetector.bodyType = "toyDetector";
    toyDetectorBodyDef.userData = rbDataToyDetector;

    var toyDetectorBox = world.CreateBody(toyDetectorBodyDef);
    toyDetectorBox.CreateFixture(toyDetectorFixDef);

Then you have to add the TOY object.

    //TOY object

    var fixDef = new box2d.b2FixtureDef();
    fixDef.density = 1;
    fixDef.friction = 0.5;
    fixDef.restitution = 0.5;
    bodyDef = new box2d.b2BodyDef();
    bodyDef.type = box2d.b2Body.b2_dynamicBody;
    bodyDef.position.x = 200 / SCALE + Math.random() * 400 / SCALE;
    bodyDef.position.y = 0;
    var rbDataBall = new Object();
    rbDataBall.bodyType = "toy";
    bodyDef.userData = rbDataBall;
    fixDef.shape = new box2d.b2CircleShape(20 / SCALE);


    var toyBox = world.CreateBody(bodyDef);
    toyBox.CreateFixture(fixDef);

Now when you have the objects you have to check for collision.

        try {
        var listener = new Box2D.Dynamics.b2ContactListener;
        listener.BeginContact = function (contact) {

            if ((contact.GetFixtureA().GetBody().GetUserData().bodyType == "toyDetector" && contact.GetFixtureB().GetBody().GetUserData().bodyType == "toy") ||
                    (contact.GetFixtureA().GetBody().GetUserData().bodyType == "toy" && contact.GetFixtureB().GetBody().GetUserData().bodyType == "toyDetector")) {

                //YOUR CODE HERE
                console.log("Toy detected!");

            }

        }

        world.SetContactListener(listener);
    }
    catch (e) {
        console.log(e.toString());
    }

I hope this will help you.



来源:https://stackoverflow.com/questions/6703285/set-contact-enabled-in-box2d-andengine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!