Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags
(1 edit)

Hi people,

Progress is going quite well today. I have created a Box2D contact listener to listen for the number of contacts that the player has. This involved creating a new fixture for the player (foot), which will tell the contact listener when the player is on the ground. It will be important to know this for displaying the correct player image.

public class Box2DContactListeners implements ContactListener {

 private int numFootContacts;

 public Box2DContactListeners() {
 super();
}

 public void beginContact(Contact contact) {

 Fixture fa = contact.getFixtureA();
Fixture fb = contact.getFixtureB();


 if (fa == null || fb == null) return;

 if (fa.getUserData() != null && fa.getUserData().equals("foot")) {
 numFootContacts++;
}
 if (fb.getUserData() != null && fb.getUserData().equals("foot")) {
 numFootContacts++;
}
 }


 public void endContact(Contact contact) {

 Fixture fa = contact.getFixtureA();
Fixture fb = contact.getFixtureB();

 if (fa == null || fb == null) return;

 if (fa.getUserData() != null && fa.getUserData().equals("foot")) {
 numFootContacts--;
}
 if (fb.getUserData() != null && fb.getUserData().equals("foot")) {
 numFootContacts--;
}
 }

 public boolean playerCanJump() {
 return numFootContacts > 0;
}

 public void preSolve(Contact c, Manifold m) {
 }

 public void postSolve(Contact c, ContactImpulse ci) {
}

As you can see, by implementing the Box2D ContactListener interface, a custom listener can be created which listens for the 'foot' fixture to collide with another fixture. For a normal jumping game, this would be used for judging when the user could make the player jump, however, since I'm using a jet-pack behavior in this game, this will only be used for sprite assignment, for now.

Next I worked on making the camera follow the player. This didn't have much involved since the camera position will always be based off of the player position. The following method in the GameScreen make sure that the cameras are updated with the player position:


private void updateCameras() {
 playerPos = player.getBody().getPosition();
 float targetX = playerPos.x * PPM + MainGame.WIDTH / 50;
 float targetY = playerPos.y * PPM + MainGame.HEIGHT / 50;

cam.setPosition(targetX, targetY);
b2dCam.setPosition(playerPos.x + MainGame.WIDTH / 50 / PPM, playerPos.y + MainGame.HEIGHT / 50 / PPM);

b2dCam.update();
cam.update();
}

It is important that both cameras are updated together so that our sprite view and our Box2D world are always rendered correctly.

That's pretty much it for today, I'm also doing some Art work in Spriter to make my spaceman a bit cooler looking with more animations so hopefully that will be in the next post.

Source here: https://github.com/RobertFOConnor/SpaceDoctor


Thanks for reading (sorry for no pictures :( ),

Yellowbyte x