Basic collision - Gornova/MarteEngine GitHub Wiki

After finishing the Keyboard and mouse input tutorial, it's time to let two entities interact with each other.

You have some basic stuff on screen, you can move your player image.. but what's happening when two entities touch each other? This is called collision and thanks to MarteEngine it is more easy to deal with than you can imagine.

Let's start by creating two Entities: Player and Wall

public class Player extends Entity {

	/**
	 * @param x, x coordinate on screen where Player starts
	 * @param y, y coordinate on screen where Player starts
	 */
	public Player(float x, float y) {
		super(x, y);
		// load Image from disk and associate it as player image
		Image img = ResourceManager.getImage("player");
		setGraphic(img);
		
		// define basic collision info
		// hitbox is a rectangle around entity, in this case it is exactly the size of the player image
		setHitBox(0, 0, img.getWidth(), img.getHeight());
		// declare type of this entity
		addType("PLAYER");
	}
	
}
public class Wall extends Entity {

	public Wall(float x, float y) {
		super(x, y);
		
		// load Image from disk and associate it as wall image
		Image img = ResourceManager.getImage("wall");
		setGraphic(img);
		
		// define basic collision info
		// hitbox is a rectangle around entity, in this case it is exactly the size of the wall image
		setHitBox(0, 0, img.getWidth(), img.getHeight());
		// declare type of this entity
		addType(SOLID);		
	}
}

What do the lines of code we added? For every entity you want to react on collisions, you need to declare two things:

  1. hitbox: the hitbox specifies the borders of an entity. When MarteEngine checks for collisions, it looks at every entity's borders if they overlap with any other hitbox,
  2. type: to speed up collision checks against different groups of entities, you can define the type of an Entity. Every entity can have many types. In our example, the Player has type "PLAYER" and the Wall has the (builtin) basic type "SOLID".

With these two basic concepts you can do anything you want.. using the collision check method for example before the player updates his/her position, it's possibile to check and avoid a collision with other entities with type "SOLID":

public class Player extends Entity {

	/**
	 * @param x
	 *            , x coordinate on screen where Player starts
	 * @param y
	 *            , y coordinate on screen where Player starts
	 */
	public Player(float x, float y) {
		super(x, y);
		// load Image from disk and associate it as player image
		Image img = ResourceManager.getImage("player");
		setGraphic(img);

		// define basic collision info
		// hitbox is a rectangle around entity, in this case it is exactly the size of the
		// player image
		setHitBox(0, 0, img.getWidth(), img.getHeight());
		// declare type of this entity
		addType("PLAYER");
	}

	@Override
	public void update(GameContainer container, int delta)
			throws SlickException {
		super.update(container, delta);

		// check if a key is down
		if (check("RIGHT")) {
			// do anything you like, for example: only move right if we don't collide with some SOLID entity
			if (collide(SOLID, x + 10, y)==null) {
				x = x + 10;
			}
		}
	}

}

In the update method, before the Player moves (after hitting right arrow on your keyboard), it's possible to check if the Player collides with another Entity of type SOLID (in our example, a WALL). If this is not the case the Player can move!

You can also have an utility method ("callback") to handle collisions from the Wall point of view:

public class Wall extends Entity {

	public Wall(float x, float y) {
		super(x, y);
		
		// load Image from disk and associate it as wall image
		Image img = ResourceManager.getImage("wall");
		setGraphic(img);
		
		// define basic collision info
		// hitbox is a rectangle around entity, in this case it is exactly the size of the wall image
		setHitBox(0, 0, img.getWidth(), img.getHeight());
		// declare type of this entity
		addType(SOLID);		
	}

	@Override
	public void collisionResponse(Entity other) {
		// called when colliding with another entity
	}
	
}

When the Player collides with a Wall, the wall can perform some code by overriding the collisionReponse method from the base Entity class. For example the wall could break down after 20 hits - assuming your player is a tough guy like the Hulk...

Now you are ready for the Animate sprite tutorial!


MarteEngine version 0.3

⚠️ **GitHub.com Fallback** ⚠️