Coordinate-based game, with smooth animations

Started by
6 comments, last by scgrn 18 years, 5 months ago
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!
my siteGenius is 1% inspiration and 99% perspiration
Advertisement
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
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.
Quote:Original post by load_bitmap_file
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.
....

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.
Quote:Original post by scgrn
Quote:Original post by load_bitmap_file
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.
....

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.
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.
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!
my siteGenius is 1% inspiration and 99% perspiration
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?

This topic is closed to new replies.

Advertisement