2

I'm trying to implement some collision detection to a test game i've been making. I've used TiledMap to create the map and set a property on one of the tiles to blocked=true This tile is then drawn on layer 0.

I then check to see if the tile exists in the direction the player is moving, using the following code

if (input.isKeyDown(Input.KEY_DOWN)) {
    sprite = down;
    sprite.update(delta);
    int tileID = map.map.getTileId((int) x / map.map.getTileWidth(), (int) y / map.map.getTileHeight() + 1, 0);
    String value = map.map.getTileProperty(tileID, "blocked", "false");
    if (value.equals("true")) {
        y += delta * 0.1f;
        System.out.println("Tile ID: " + (int) (x / map.map.getTileWidth()) + ", " + (int) (y / map.map.getTileHeight() + 1) + " Try to walk down. Tile value below the player is:" + value);
    }

}

This is repeated for each direction.

The problem I'm running into is it's picking up the blocked property for incorrect tiles You can understand better with this video. The yellow tiles are the collision/blocked tiles.

Anubian Noob
  • 13,426
  • 6
  • 53
  • 75
Ceri Turner
  • 830
  • 2
  • 12
  • 36
  • Just out of curiosity, where is the origin of your map? Based on the video it looks like you may be checking the blocks behind you instead of in front of you (where you're adding 1 to your block, maybe you should be subtracting one). The other thing I'd suggest is maybe figure out where your interpreting collision from. Is x and y the middle of the top of the sprite or is it the center of the body? – user3507600 Apr 29 '14 at 14:11
  • map origin is always 0,0; that's not the question though. Where is the origin being drawn? Where is 0,0 on the drawn image? Or more specifically, what quadrant are you working in? – user3507600 Apr 29 '14 at 14:17
  • The origin is in the top left. – Anubian Noob May 02 '14 at 15:06
  • Ok can you post the full code for your movement? All the if statements? I think I got a plausible answer, but I need to see that code. – Anubian Noob May 04 '14 at 01:16

1 Answers1

-1

I think you have this issue because you update the graphics outside your if statement

Try to move the ones below inside the if:

sprite = down; 
sprite.update(delta);

However I suppose one of these put the face of the sprite down and therefore should not be inside.

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
QGA
  • 3,114
  • 7
  • 39
  • 62
  • neither can be outside the if. The first because its what sets the sprites direction image, and the second if its outside the if the characters animation is always playing, not just when moving – Ceri Turner Apr 29 '14 at 14:15
  • can you please check value.equals("true") without "" so value.equals(true) – QGA Apr 29 '14 at 14:35