sprite position (map vs screen)

Started by
3 comments, last by fredjordans 21 years, 11 months ago
hi. i have a functional 2d scrolling graphics engine. i am currently able to draw my sprites onto the screen using coordinates relative to the screen. However, I need to make them move as the map moves. What is the best way to approach this? Everytime I draw the map, should I check every single sprite to see if it is onscreen, and if so draw it? This seems as though it would take a long time if you had many characters moving around in the world. Any suggestions? Also, what about if my map only has tile coordinates, but I need pixel specific coordinates for sprite positions? [edited by - fredjordans on April 29, 2002 5:25:35 PM]
-eat me!-but i''m not a cannibal!
Advertisement
To find screen coordinates, just do like sprite.xPos - map.xWin. Then yes, I would check if they''re on screen before drawing. It takes a lot longer to draw a sprite than to do a few if statements.
For tile coordinates, you can either store their position in tile coords, and then have an offset value for while they''re moving, or you can just give them pixel coordinates, and divide by the tile size to find what tile they''re on (which won''t really slow it down if you use a multiple of 2 for your tile size and use shifts). Or if you don''t mind using a little extra memory, you can store pixel coords and tile coords, so you can just update the tile position once per frame to save a few shifts.


-Deku-chan

DK Art (my site, which has little programming-related stuff on it, but you should go anyway^_^)
Assuming that your sprites only know their map coordinates, you can figure out scrren coords using the '%' operator. Like this:

int x = sprite.x % SCREEN_WIDTH;
int y = sprite.y % SCREEN_HEIGHT;

the x and y are exactly where, on the screen, the sprite should go. That help?

...wait, something is wrong.... ill revise this later tonight

[edited by - Mulligan on April 30, 2002 12:56:28 PM]
"This seems as though it would take a long time if you had many characters moving around in the world. Any suggestions?"

Search around the web for space partitioning systems such as quad trees. As your world gets larger and the number of game objects increases this will save greater amounts of time (and can probably be used for faster collision detection as well).
Great! Thanks a lot for the help. This definitely gives me several possibilities and things to investigate. Appreciate it!
-eat me!-but i''m not a cannibal!

This topic is closed to new replies.

Advertisement