Keeping player aligned to grid in Pacman
Posted
by
user17577
on Game Development
See other posts from Game Development
or by user17577
Published on 2012-06-30T14:37:43Z
Indexed on
2012/06/30
15:26 UTC
Read the original article
Hit count: 327
I am making a Pacman game using XNA. The game is tile based, with each tile being 32 pixels. As the player moves, I need to know whenever it is perfectly on a tile (ie position of 32, 64, etc...) so that I can check to see if the next tile is free. I am using the following logic to test this.
if (position.X % 32 == 0 && position.Y %32 == 0)
{
onTile = true;
}
I figure that I need to make the player's speed evenly divide 32. Everything works fine if I make the player's speed an integer such as 4 or 8. But if I make the speed something like 6.4, I end up with positions such as 64.00001, and my if statement no longer works correctly.
How can I keep the player aligned with the grid, while allowing a wider range of player speeds than 1, 2, 4, 8, 16, and 32? Or is there some better way to go about this?
Thanks
© Game Development or respective owner