Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to coordinate graphical and logical position in a 2D tile based game using an array?

I'm programming a 2D tile based game in Java like Bomberman (my first game).

I use a 2D array to represent the map where I keep track of walls, walkable spaces, etc. By now I was able to make the player move smoothly swaping sprites depending on the direction key he presses (but not interacting with the background/map, it can walk randomly and get out of the screen).

My problem comes when I want to keep track of the player's position in the array. Logically the character always belongs to only one tile but graphically it can be between two tiles. How can I solve this issue?

I already tried this:

currentTileArrayX = x / tileWidth;

currentTileArrayY = y / tileHeight;

Where x and y are the top-left coordinates of the sprite. But everything became very buggy because the player's sprites aren't the same size as the tiles.

I had another option in mind that would solve the previous problem: When the player presses a direciton key, move the character to the next tile like an animation. This wouldn't let the player stop in the middle of two tiles... But what if he interrupts it with another key press? How can I stop user's action while the animation is executing? I think this is the easiest solution but I don't know how to implement it.

I already tried to find the solution in other questions but I had no luck. I hope you can help me.

Thanks a lot.

like image 728
Rama Avatar asked Oct 11 '22 07:10

Rama


2 Answers

Aaaaaaaaaaaaaaah! Encountering the 'discrete space problem' :) Your logic map is discrete but your UI movements are in the continuous space. I don't the user can actually be in both tiles "at the same time" - visually yes, but never 'internally'. Now you are getting the position of the player and that can be "anywhere" within the tile or "on the boundary" of 2 tiles so to speak...

You have two solutions to this problem, IMO:

  1. Use simple rounding: If the player position is < tile-boundary just keep him on the previous tile. If Greater on the next. Now the problem of boundary - you can use a heuristic, since you shouldn't trust the accuracy of the floating point comparisons (i.e, comparing to 0 is not a good thing to do). So you can try something like this if(currentPosition - boundary <= 0.000000001) then assume it's the next tile else stay on the same one.
  2. Well @Jay beat me to it. So the second one is to actually have a movement speed defined and move the character that many steps per 'click'/'button press' - but you say you have the problem of key pressed in "between". I'm guessing if you code is designed like this then it shouldn't be a problem:
    update()
        {
           calculate(); //update all datastructures/calculations
           draw(); //update the visual representation based on calculations
        }

update() is a.k.a, gameLoop. I think this should solve your problem...

like image 132
PhD Avatar answered Oct 13 '22 01:10

PhD


I'd try to keep the logic part (tile the player belongs to) and the visualization part (animation between tiles) as seperate as possible. My approach would be to have a logical location (tile the player is set on) and a movement speed (which essentially means an amount of time the player isn't allowed to move on to the next tile after moving to a tile). Then the animation from one tile to another would mean that your sprite is always moving towards the screen coordinates of your tile - the time this takes could easily be determined by the movement speed (one full tile movement should take as long as the player has to rest on the current tile).

The only special case would be this "turning around", where you would allow the player to return to the tile he came from at any time, taking only (timeToRecoverOnTile - (timeToRecoverOnTile - timeSinceMovementStarted)).

like image 30
Jay Avatar answered Oct 13 '22 02:10

Jay