# Coordinate-based game, with smooth animations

This topic is 4786 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have developed a game similar, in a way, to snake/nibbles. This game is inherantly grid-based, and the non-moving objects store their position as a grid location rather than a screen coordinate. However, the player's ships must move around the screen smoothely. I have had, and continue to have, a lot of trouble converting the players' positions on the screen into grid coordinates, which really screws with my collision detection, among other things. I'm just looking for suggestions on how to implement this without too much complication, in general. If you have any ideas, please let me know. Thanks!

##### Share on other sites
Make each square on your grid power of two sized (eg. 8 x 8, 16 x 16, etc)

To find an object's grid position:

gridX = x / GRIDSIZE
gridY = y / GRIDSIZE

To place an object at a grid position:

x = gridX * GRIDSIZE
y = gridY * GRIDSIZE

To find an object's offset into it's grid position:

offsetX = x % GRIDSIZE
offsetY = y % GRIDSIZE

##### Share on other sites
I did a type of coordinate->grid calculation for a minesweeper clone but I seem to have the code. However, I recall my implementation being something like this:

If you have your grid[][], first subtract from your ship coordinate the top leftmost coordinate point of your grid, so you're basically starting from a (0, 0) point. Then divide your x and your y by the width and the height respectively of a single cell (use integer division). The result of the x and y coordinate division should give you the grid position.

##### Share on other sites
Quote:
 Original post by load_bitmap_fileIf you have your grid[][], first subtract from your ship coordinate the top leftmost coordinate point of your grid, so you're basically starting from a (0, 0) point.....

You don't need to do this first step if you're using integers, the fractional part will be lopped off and fed to the sharkticons.

##### Share on other sites
Quote:
Original post by scgrn
Quote:
 Original post by load_bitmap_fileIf you have your grid[][], first subtract from your ship coordinate the top leftmost coordinate point of your grid, so you're basically starting from a (0, 0) point.....

You don't need to do this first step if you're using integers, the fractional part will be lopped off and fed to the sharkticons.

Nope, it's necessary. The point is that if your grid's starting point (top left hand corner) isn't at (0, 0), you need to "pretend" it is, otherwise your calculations for the grid element will be messed up.

##### Share on other sites
Oh, I'm sorry, I misunderstood. Right you are. I thought you meant that when converting world coordinates to grid coordinates, you have to shift the world coordinates to the upper left of the current grid position.

##### Share on other sites
I know how to do it in a simple fashion; I'm already dividing on-screen position by the width or height of the sprite to find its coordinate position. I think the main problem is coming from rounding errors and such. Do most people think this is the best way to approach doing a game of this overall nature? Any more tips? Post them here. I appreciate all who responded, though! Thanks!

##### Share on other sites
You should be rounding the screen position by the size of each grid element, not the sprite that's occupying the grid, even if they're the same size.

What makes you think rounding errors are involved? Care to post some code?

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5
khawk
11

• 10
• 9
• 9
• 11
• 11
• ### Forum Statistics

• Total Topics
633679
• Total Posts
3013301
×