I've been creating a game on my own in LIBGDX, BOX2D and there' s a problem I can' t solve. When two World bodies collide, method "beginContact" is called in MyContactListener class that implements ContactListener interface.
How am I supposed to change player's healthpoints when it's fixture collides with other bodies' fixtures. I know I have to do this somehow through MyContactListener class, but I can' t find the way how to call the method I need..
Note: My player's body and fixture are created in "Player" class and there are methods "setRemainingHealth()" and "getRemainingHealth()".
Here's also some code, which might illuminate this situation.
MyContactListener.java
package utils;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.ContactImpulse;
import com.badlogic.gdx.physics.box2d.ContactListener;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.Manifold;
public class MyContactListener implements ContactListener {
@Override
public void beginContact(Contact c) {
Fixture fa = c.getFixtureA();
Fixture fb = c.getFixtureB();
if (fa == null || fb == null) return;
if (fa.getUserData() == null || fb.getUserData() == null) return;
if (isTunnelMachineCollision(fa, fb)) {
// call somehow method "player.setRemainingHealthPoints(player.getRemainingHealthPoints-0.01f)" here
}
}
@Override
public void endContact(Contact c) {
}
@Override
public void postSolve(Contact c, ContactImpulse arg1) {
}
@Override
public void preSolve(Contact arg0, Manifold arg1) {
// TODO Auto-generated method stub
}
private boolean isTunnelMachineCollision(Fixture a, Fixture b) {
return (a.getUserData()) == "terrain" && (b.getUserData()) == "machine";
}
}
TestPlayer.java
package testPackage;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.World;
public class TestPlayer {
private float remainingHealthPoints = 3;
private CircleShape shape;
private CircleShape defenseBarrier;
private Fixture defenseBarrierFixture;
private Fixture mainBody;
private BodyDef bodydef;
private Body body;
private float x;
private float y;
private float width;
public TestPlayer(World world, float x, float y, float width) {
this.x = x;
this.y = y;
this.width = width;
bodydef = new BodyDef();
bodydef.type = BodyType.DynamicBody;
bodydef.position.set(x, y);
bodydef.fixedRotation = true;
body = world.createBody(bodydef);
shape = new CircleShape();
shape.setRadius(width);
mainBody = body.createFixture(shape, 1.0f);
mainBody.setUserData("machine");
}
public float getRadius() {
return shape.getRadius();
}
public BodyDef getBodydef() {
return bodydef;
}
public CircleShape getShape() {
return shape;
}
public Body getBody() {
return body;
}
public void activateDefenseBarrier(World world) {
defenseBarrier = new CircleShape();
defenseBarrier.setRadius(width*1.5f);
defenseBarrierFixture = body.createFixture(defenseBarrier, 1.0f);
defenseBarrierFixture.setUserData("defenseBarrier");
}
public void destroyDefenseBarrier() {
if (defenseBarrierFixture != null) {
System.out.println("olemas");
body.destroyFixture(defenseBarrierFixture);
System.out.println(defenseBarrierFixture);
}
else {
System.out.println("fixture on null");
}
}
public Fixture getDefenseBarrier() {
return defenseBarrierFixture;
}
public float getRemainingHealthPoints() {
return remainingHealthPoints;
}
public void setRemainingHealthPoints(float remainingHealthPoints) {
this.remainingHealthPoints = remainingHealthPoints;
}
}