Spurdow
Spurdow

Reputation: 2043

Proper way of detecting collision between dynamic body and static body in box2d in libgdx

here is a simple code that i used which I believe is effective, i dont know if there is a better solution to this.

// this code is in render part of my game world class
public void checkCollision(){
    int contacts = world.getContactCount();

    if(contacts > 0){
        for(Contact contact : world.getContactList()){
            Fixture fa = contact.getFixtureA();
            Fixture fb = contact.getFixtureB();
            boolean collideOnce  = false;
            if(fa.getBody().getType() == BodyType.DynamicBody){
                collideOnce = fa.getUserData()==null? true : false;
                fa.setUserData("t");
            }else if(fb.getBody().getType() == BodyType.DynamicBody){
                collideOnce = fb.getUserData()==null? true: false;
                fb.setUserData("t");
            }

            if((fa.getBody().getType() == BodyType.DynamicBody && fb.getBody().getType() == BodyType.StaticBody) || (fb.getBody().getType() == BodyType.DynamicBody && fa.getBody().getType() == BodyType.StaticBody)){
                if(collideOnce ){
                                   // play some sound or score or something in your game that would benefit to collision between static and dynamic bodies
                                    }

            }
        }
    }
}

I hope there is a better way than doing like the above code because im traversing through all bodies in the world and setting userData("t") => makes problem when sweeping dead bodies in the world.

UPDATE THIS IS MY NEW COLLISION DETECTOR thx to YOUR ANSWERS

 GameWorld implements ContactListener, ...
 ... //ommitted many codes just to be clear on the solution

 // this code is in render part of my game world class
 public void checkCollision(){
    if(contacts.isEmpty() ) return;
    while(!contacts.isEmpty()){
        Contact contact = contacts.pop();
        Fixture fa = contact.getFixtureA();
        Fixture fb = contact.getFixtureB();

        if(fa == null || fb == null){ return ;}

        boolean collideOnce  = false;
        if(fa.getBody().getType() == BodyType.DynamicBody){
            collideOnce = fa.getUserData()==null? true : false;
            fa.setUserData("t");
        }else if(fb.getBody().getType() == BodyType.DynamicBody){
            collideOnce = fb.getUserData()==null? true: false;
            fb.setUserData("t");
        }

        if((fa.getBody().getType() == BodyType.DynamicBody && fb.getBody().getType() == BodyType.StaticBody) || (fb.getBody().getType() == BodyType.DynamicBody && fa.getBody().getType() == BodyType.StaticBody)){
            if( collideOnce ){
                SoundManager.play(SoundType.DROP , 0.5f);
            }
        }
    }
}

...
// 
    private Stack<Contact> contacts = new Stack<Contact>();
    @Override
public void beginContact(Contact contact) {
    // TODO Auto-generated method stub
    contacts.push(contact);
}

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

}

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

}

much appreciated, Dave

Upvotes: 0

Views: 3497

Answers (2)

Matt
Matt

Reputation: 2634

Perhaps you can take a look at the PeaWorld demo code. I believe it pretty much solves the same problem.

Upvotes: 1

ajeetdl
ajeetdl

Reputation: 1284

Use a contact listener and you can avoid having to traverse all of the bodies yourself. The box2d online doc has information on how to use a contact listener. See section 9.4

You will be able to access the body type from the contact itself just as you are now.

Upvotes: 1

Related Questions