Libgdx and Box2d.
-------------
I want enemy to move right and left. if enemy hits the wall than move the over way.
----
I have two senors set up on the enemy. one on enemy left side and other on enemy right side. so if wall hit enemy right senor than move enmey left. and if wall hit enemy left senor than move enmey right.
I am creating 3 enemies and I am storeing them in arraylist.
--------
if I create 1 enemy it works fine. enemy hits the wall and switch direction. I am doing collision stuff in contectListener also called (beginContact & endContact methods).
problem is when i create 2 or more enemies and store in arraylist. so for some reason if a enemy switch direction than all of enemies switch directions together.
I know why this is happening. its bc in contectListener I have one variable called "wallSwitch" which checks, if enemy hit a wall and enemy need to switch. so that is why it works for one enmey fine. that 'wallSwitch' variable is changing every enemies direction. I need to come up way so so every enemy is different.
-----
here is I am building enemy. It was two sensors. on enemy left side & other on enemy right side.
private void createEnemies() {
...
for (MapObject mo : layer.getObjects()) {
bdef.type = BodyType.DynamicBody;
....
bdef.position.set(x, y);
body = world.createBody(bdef);
/*** 1st fixture - enemy ***/
pshape.setAsBox(10f/PPM, 10f/PPM);
fdef.shape = pshape;
fdef.isSensor = false;
fdef.filter.categoryBits = Constants.BIT_ENEMY;
fdef.filter.maskBits = Constants.BIT_PLAYER;
body.createFixture(fdef).setUserData("enemy");
/*** 2nd fixture - left ***/
pshape.setAsBox(10f/PPM, 10f/PPM, new Vector2(-10f / PPM,
0f/PPM), 0);
fdef.shape = pshape;
fdef.isSensor = true;
fdef.filter.categoryBits = Constants.BIT_ENEMY; // id
fdef.filter.maskBits = Constants.BIT_GROUND;// collied with
body.createFixture(fdef).setUserData("enemyLeft");
/*** 3rd fixture - right ***/
pshape.setAsBox(10f / PPM, 10f / PPM, new Vector2(10f / PPM,
0f / PPM), 0);
fdef.shape = pshape;
fdef.isSensor = true;
fdef.filter.categoryBits = Constants.BIT_ENEMY; // id
fdef.filter.maskBits = Constants.BIT_GROUND;// collied with
body.createFixture(fdef).setUserData("enemyRight");// myContactListener
// create & store in array list
Enemies e1 = new Enemy1(body);
Enemies e2 = new Enemy1(body); //problem here
enemiesStore.add(e1);
enemiesStore.add(e2);
}
}
here is collision stuff. when enemy sensor touching a wall than switch direction.
boolean wallSwitch;
public void beginContact(Contact contact) {
Fixture fa = contact.getFixtureA();
Fixture fb = contact.getFixtureB();
// Enemy switch direction
if (fa.getUserData() != null && fa.getUserData().equals("enemyLeft")) {
wallSwitch = true;
}
if (fb.getUserData() != null && fb.getUserData().equals("enemyLeft")) {
wallSwitch = true;
}
if (fa.getUserData() != null && fa.getUserData().equals("enemyRight")) {
wallSwitch = true;
}
if (fb.getUserData() != null && fb.getUserData().equals("enemyRight")) {
wallSwitch = true;
}
}
// called when two fixtures no longer collide - run it onces
public void endContact(Contact contact) {
Fixture fa = contact.getFixtureA();
Fixture fb = contact.getFixtureB();
// Enemy switch direction
if (fa.getUserData() != null && fa.getUserData().equals("enemyLeft")) {
wallSwitch = false;
}
if (fb.getUserData() != null && fb.getUserData().equals("enemyLeft")) {
wallSwitch = false;
}
if (fa.getUserData() != null && fa.getUserData().equals("enemyRight")) {
wallSwitch = false;
}
if (fb.getUserData() != null && fb.getUserData().equals("enemyRight")) {
wallSwitch = false;
}
}