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));
}
}
Because it is a bit more complicated.It should be something like this:
}
Here you will find more info: http://www.matim-dev.com/handling-collisions-between-bodies.html