I've already had some experiences about the work of 2 dimensional square-based playfields and moving a player on them. Though, when a square has multiple pixel sizes, and player moves e.g. 1 pixel per frame, I have some problems. Currently, my program counts the coordinates of tile Player stands on like this:
Tile X coordinate = floor of Player X coordinate divided by the X size of a tile
a.k.a TileX = floor(PlayerX/TileXsize)
and same for Y. However, when I increase the moving speed to bigger px/frame speed, sometimes the player sinks into the tile that should stop it. How can I prevent this problem?
EDIT: codes. Language is Lua, framework is LOVE2D, gamefield is stored in tables. Player has an X, an Y, and Tilex&TileY, count this way on every frames:
Player.TileX = math.floor((Player.X)/TileSize)
Player.TileY = math.floor((Player.Y)/TileSize)
Moving belongs to key actions, they are simply like Player.X = Player.X + 1
There's a Gravity function as well, called on every frames:
function Gravity()
if not Solidchecker(Field[Player.TileX][Player.TileY+1]) then
Player.Y = Player.Y +GForce
end
end
In case of anyGforce
(which is a constant) where value is different from the size of the tile (24) the player sinks into the ground. Obviously in that case, everythings the same as if the system would be counted with only tiles, without separated Player.TileX calculations. Otherwise, it can't handle the occuring alignment.
Possibly I forgot showing some code parts, but I can't see now that any other functions would influence these variables.