Andengine Collision Not detected

65 views Asked by At

I have created a player rect and an enemy rect that creates Rectangle Bodies that are supposed to be colliding and the beginContact() method of contact listener should be called. DOnt know why it isnt getting called :/

public class GameScene extends Scene{
Engine mEngine;
Camera mCamera;
BaseGameActivity activity;
VertexBufferObjectManager vbom;

PhysicsWorld world;

Rectangle player;
Rectangle enemy;

public GameScene(Engine mEngine, Camera mCamera, BaseGameActivity activity, VertexBufferObjectManager vbom) {
    this.mEngine = mEngine;
    this.mCamera = mCamera;
    this.activity = activity;
    this.vbom = vbom;

    load();
}

void load(){

    world = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH),false);


    player = new Rectangle(mCamera.getWidth()/2,mCamera.getHeight()/2,50,50,vbom){
        @Override
        public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
            fire();
            return true;
        }
    };

    enemy = new Rectangle(mCamera.getWidth()/2,mCamera.getHeight()/2 - 400,50,50,vbom){
        @Override
        public void onAttached() {

            TimerHandler handler = new TimerHandler(3, new ITimerCallback() {
                @Override
                public void onTimePassed(TimerHandler pTimerHandler) {
                    enemyFire();
                }
            });

            handler.setAutoReset(true);

            registerUpdateHandler(handler);
            super.onAttached();
        }
    };

    attachChild(enemy);
    attachChild(player);
    registerUpdateHandler(world);

    world.setContactListener(new ContactListener() {
        @Override
        public void beginContact(Contact contact) {
            Log.e("begin","Contact");
        }

        @Override
        public void endContact(Contact contact) {

        }

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

        }

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

        }
    });
    registerTouchArea(player);
}

void enemyFire(){
    Rectangle rect = new Rectangle(mCamera.getWidth()/2,mCamera.getHeight()/2 - 400,50,50,vbom);
    FixtureDef fix = PhysicsFactory.createFixtureDef(0.5f,0.5f,0.5f);
    Body b = PhysicsFactory.createBoxBody(world,rect, BodyDef.BodyType.KinematicBody,fix);

    b.setLinearVelocity(0, 7);
    attachChild(rect);
    world.registerPhysicsConnector(new PhysicsConnector(rect, b));

}


void fire(){
    Rectangle rect = new Rectangle(mCamera.getWidth()/2,mCamera.getHeight()/2,50,50,vbom);
    FixtureDef fix = PhysicsFactory.createFixtureDef(0.5f,0.5f,0.5f);
    Body b = PhysicsFactory.createBoxBody(world,rect, BodyDef.BodyType.KinematicBody,fix);

    b.setLinearVelocity(0,-7);
    attachChild(rect);
    world.registerPhysicsConnector(new PhysicsConnector(rect,b));

}

}

1

There are 1 answers

2
Łukasz Motyczka On

Because it is a bit more complicated.It should be something like this:

private ContactListener createContactListener()
{
ContactListener contactListener = new ContactListener()
{
    @Override
    public void beginContact(Contact contact)
    {
        final Fixture x1 = contact.getFixtureA();
        final Fixture x2 = contact.getFixtureB();

        if (x2.getBody().getUserData().equals("player")&&x1.getBody().getUserData().equals("monster"))
        {
        Log.i("CONTACT", "BETWEEN PLAYER AND MONSTER!");
        }
    }

    @Override
    public void endContact(Contact contact)
    {

    }

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

    }

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

    }
};
return contactListener;

}

Here you will find more info: http://www.matim-dev.com/handling-collisions-between-bodies.html