Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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() {
        @Override
        public void endContact(Contact contact) {
        }
        @Override
        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 ) {
                //mWorld.destroyBody(bodyA);
                //bodyA.setTransform(new Vector2( 0, 0 ), 0 ) ;
                bodyA.setActive(false );
            }
            else if( dataB.id == Box2DUserData.ID_ITEM ) {
                //bodyB.getWorld().destroyBody(bodyB);
                //bodyB.setTransform(new Vector2( 0, 0 ), 0 ) ;
                //mWorld.destroyBody(bodyB);
                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.

Thanks

share|improve this question

1 Answer 1

up vote 4 down vote accepted

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.

share|improve this answer
    
Thanks! I will have a try with using sensor! –  minami Mar 2 '12 at 1:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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