I am currently writing a little zelda like game and have a little bit of a problem with collision detection.
It works but the the character stands to far away from the object.
Here is my player class:
public class Player extends Sprite {
/** the movement velocity */
private Vector2 velocity = new Vector2();
private float speed = 60 * 2, gravity = 6 * 1.8f, animationTime = 0;
private Animation still, left, right, up , down;
private TiledMapTileLayer collisionLayer;
private String blockedKey = "blocked";
public Player(Animation still, Animation left, Animation right,
Animation up, Animation down, TiledMapTileLayer collisionLayer){
super(still.getKeyFrame(0));
this.still = still;
this.left = left;
this.right = right;
this.up = up;
this.down = down;
this.collisionLayer = collisionLayer;
}
public void draw(SpriteBatch spriteBatch){
update(Gdx.graphics.getDeltaTime());
super.draw(spriteBatch);
}
public void update(float delta){
//save old position
float oldX = getX(), oldY = getY();
boolean collisionX = false, collisionY = false;
//move on x
setX(getX() + velocity.x * delta);
if(velocity.x < 0) // going left
collisionX = collidesLeft();
else if(velocity.x > 0) // going right
collisionX = collidesRight();
//react to x collision
if(collisionX){
setX(oldX);
velocity.x = 0;
}
//move on y
setY(getY() + velocity.y * delta);
if(velocity.y < 0) // going down
collisionY = collidesBottom();
else if(velocity.y > 0) // going up
collisionY = collidesTop();
//react to y collision
if(collisionY){
setY(oldY);
velocity.y = 0;
}
// update animation
animationTime += delta;
setRegion(velocity.x < 0 ? left.getKeyFrame(animationTime) : velocity.x > 0 ? right.getKeyFrame(animationTime) : velocity.y > 0 ? up.getKeyFrame(animationTime) : velocity.y < 0 ? down.getKeyFrame(animationTime) : still.getKeyFrame(animationTime));
if(velocity.x == 0 && velocity.y == 0) animationTime = 0;
}
private boolean isCellBlocked(float x, float y) {
Cell cell = collisionLayer.getCell((int) (x / collisionLayer.getTileWidth()), (int) (y / collisionLayer.getTileHeight()));
return cell != null && cell.getTile() != null && cell.getTile().getProperties().containsKey(blockedKey);
}
public boolean collidesRight() {
for(float step = 0; step < getHeight(); step += collisionLayer.getTileHeight() / 2)
if(isCellBlocked(getX() + getWidth() , getY() + step))
return true;
return false;
}
public boolean collidesLeft() {
for(float step = 0; step < getHeight(); step += collisionLayer.getTileHeight() / 2)
if(isCellBlocked(getX(), getY() + step))
return true;
return false;
}
public boolean collidesTop() {
for(float step = 0; step < getWidth(); step += collisionLayer.getTileWidth() / 2)
if(isCellBlocked(getX() + step, getY() + getHeight()))
return true;
return false;
}
public boolean collidesBottom() {
for(float step = 0; step < getWidth(); step += collisionLayer.getTileWidth() / 2)
if(isCellBlocked(getX() + step, getY()))
return true;
return false;
}
I guess it has something to do with the sprite sheet. Here is my Spritesheet:
Here is the pack file for the spritesheet:
linksprite.png format: RGBA8888 filter: Nearest, Nearest repeat: none stillup rotate: false xy: 0, 0 size: 32, 32 orig: 16, 16 offset: 0, 0 index: -1 still rotate: false xy: 0, 64 size: 32, 32 orig: 16, 16 offset: 0, 0 index: -1 stillleft rotate: false xy: 0, 32 size: 32, 32 orig: 16, 16 offset: 0, 0 index: -1 up rotate: false xy: 0, 0 size: 32, 32 orig: 16, 16 offset: 0, 0 index: -1 up rotate: false xy: 32, 0 size: 32, 32 orig: 16, 16 offset: 0, 0 index: -1 up rotate: false xy: 64, 0 size: 32, 32 orig: 16, 16 offset: 0, 0 index: -1 up rotate: false xy: 96, 0 size: 32, 32 orig: 16, 16 offset: 0, 0 index: -1 up rotate: false xy: 128, 0 size: 32, 32 orig: 16, 16 offset: 0, 0 index: -1 up rotate: false xy: 160, 0 size: 32, 32 orig: 16, 16 offset: 0, 0 index: -1 left rotate: false xy: 0, 32 size: 32, 32 orig: 16, 16 offset: 0, 0 index: -1 left rotate: false xy: 32, 32 size: 32, 32 orig: 16, 16 offset: 0, 0 index: -1 left rotate: false xy: 64, 32 size: 32, 32 orig: 16, 16 offset: 0, 0 index: -1 left rotate: false xy: 96, 32 size: 32, 32 orig: 16, 16 offset: 0, 0 index: -1 left rotate: false xy: 128, 32 size: 32, 32 orig: 16, 16 offset: 0, 0 index: -1 left rotate: false xy: 160, 32 size: 32, 32 orig: 16, 16 offset: 0, 0 index: -1 down rotate: false xy: 0, 64 size: 32, 32 orig: 16, 16 offset: 0, 0 index: -1 down rotate: false xy: 32, 64 size: 32, 32 orig: 16, 16 offset: 0, 0 index: -1 down rotate: false xy: 64, 64 size: 32, 32 orig: 16, 16 offset: 0, 0 index: -1 down rotate: false xy: 96, 64 size: 32, 32 orig: 16, 16 offset: 0, 0 index: -1 down rotate: false xy: 128, 64 size: 32, 32 orig: 16, 16 offset: 0, 0 index: -1 down rotate: false xy: 160, 64 size: 32, 32 orig: 16, 16 offset: 0, 0 index: -1 right rotate: false xy: 0, 96 size: 32, 32 orig: 16, 16 offset: 0, 0 index: -1 right rotate: false xy: 32, 96 size: 32, 32 orig: 16, 16 offset: 0, 0 index: -1 right rotate: false xy: 64, 96 size: 32, 32 orig: 16, 16 offset: 0, 0 index: -1 right rotate: false xy: 96, 96 size: 32, 32 orig: 16, 16 offset: 0, 0 index: -1 right rotate: false xy: 128, 96 size: 32, 32 orig: 16, 16 offset: 0, 0 index: -1 right rotate: false xy: 160, 96 size: 32, 32 orig: 16, 16 offset: 0, 0 index: -1