Sign in to follow this  
silverphyre673

Coordinate-based game, with smooth animations

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this