I am working on a Pacman clone. I am using Java for the project. I have been having problems with collisions, and allowing the player to pass through objects. Note that the tile size (and player) is 16x16px.
Here is the collision code in the player class.
private void processInput(Input input, int delta) {
if (input.isKeyDown(Input.KEY_LEFT) || input.isKeyDown(Input.KEY_RIGHT) ||
input.isKeyDown(Input.KEY_UP) || input.isKeyDown(Input.KEY_DOWN)) {
if (input.isKeyDown(Input.KEY_LEFT)) {
velocity = new Vector2(-1 * speed * delta, 0);
rotation = 180f;
}
else if (input.isKeyDown(Input.KEY_RIGHT)) {
velocity = new Vector2(1 * speed * delta, 0);
rotation = 0f;
}
else if (input.isKeyDown(Input.KEY_UP)) {
velocity = new Vector2(0, -1 * speed * delta);
rotation = 270f;
}
else if (input.isKeyDown(Input.KEY_DOWN)) {
velocity = new Vector2(0, 1 * speed * delta);
rotation = 90f;
}
}
processCollision();
this.position.x += velocity.x;
this.position.y += velocity.y;
}
private void processCollision() {
this.positionTile = new Vector2(position.x, position.y);
this.positionTile.x -= board.getX();
this.positionTile.y -= board.getY();
this.positionTile.x /= 16;
this.positionTile.y /= 16;
Vector2 normalisedVelocity = new Vector2(velocity.x, velocity.y);
normalisedVelocity = velocity.getNormal();
if (collision(normalisedVelocity)) {
velocity = Vector2.ZERO;
}
}
private boolean collision(Vector2 direction) {
Vector2 index = new Vector2(positionTile.x + direction.x, positionTile.y + direction.y);
if (!board.getTiles()[(int)index.y][(int)index.x].collidable) {
return true;
}
return false;
}
And here is the board class.
public class Board {
TileSheet sheet;
TileRenderer renderer;
Tile[][] tiles;
Vector2 initPlayerPosition;
float x = 0;
float y = 0;
public float getX() {
return x;
}
public float getY() {
return y;
}
public TileSheet getSheet() {
return sheet;
}
public Tile[][] getTiles() {
return tiles;
}
public int getWidth() {
return tiles[0].length;
}
public int getHeight() {
return tiles.length;
}
public Vector2 getInitPosition() {
return initPlayerPosition;
}
public Board() {
}
public void loadLevel(String name) {
// Temp, the following five lines (including the whitespace) look ugly
// TODO: Make the map placing dynamic
this.x = 800 / 2 - (tiles.length * sheet.getTileWidth()) / 2;
this.y = 20;
initPlayerPosition.x += this.x;
initPlayerPosition.y += this.y;
}
public void init(GameContainer gameContainer, StateBasedGame game) throws SlickException {
Image sheetImg = new Image("data/textures/tileset.png");
sheet = new TileSheet(sheetImg, 16, 16, 1.0f);
renderer = new TileRenderer(this);
}
public void render(GameContainer gameContainer, StateBasedGame game, Graphics graphics) {
renderer.render(tiles, gameContainer, game, graphics);
}
...
public class Tile {
int sheetX;
int sheetY;
bool collidable;
public Tile(int sheetX, int sheetY) {
this.sheetX = sheetX;
this.sheetY = sheetY;
// If there is nothing (0,0) allow the player to collide
if (sheetX == 0 &&
sheetY == 0) {
collidable = true;
} else {
collidable = false;
}
}
}
Any ideas?