I am having a problem with a collision detection between the character and items, both created as box2d objects. What I am trying to do is to delete the item or remove item from the collision method before character hits item and bounces away. Thus, I want to detect when the item and character collided to gain item effects.

Current sources are as below :

mWorld.setContactListener( new ContactListener() {
        public void endContact(Contact contact) {
        public void beginContact(Contact contact) {
            Body bodyA = contact.getFixtureA().getBody() ;
            Body bodyB = contact.getFixtureB().getBody() ;

            Box2DUserData dataA = (Box2DUserData) bodyA.getUserData() ;
            Box2DUserData dataB = (Box2DUserData) bodyB.getUserData() ;

            if( dataA.id == Box2DUserData.ID_ITEM ) {
                //bodyA.setTransform(new Vector2( 0, 0 ), 0 ) ;
                bodyA.setActive(false );
            else if( dataB.id == Box2DUserData.ID_ITEM ) {
                //bodyB.setTransform(new Vector2( 0, 0 ), 0 ) ;
                bodyB.setActive(false );

This source works okay, but the bodies are removed after collision detection. So I can get the character walk through items, after the character bumped in item and stopped for a frame or so.

I am not sure if I am supposed to use World.setContactListener or another method, it would be grateful if someone could give me the right method or another way to detect collision and the way to remove body from collision method before it actually collides.



The easiest way is to use a sensor instead a Body. A sensor generates a collision, but not a response. So you will be notified when they hit, but the hero will not bounce off.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.