** NEWBIE ** Moving sprites over tiles - questions
Hello all,
First, let me get the heresy over with - I''m not using C++/DirectX. I''m using Python/SDL/pygame, and enjoying it very much, thank you. But I don''t think API is relevant to my questions.
Ok, so here''s my situation. I have a (very) simple 2D square-tile map. Each tile is 32 pixels square, and the map itself is the Python equivalent of a 2D array. Therefore, each tile object can be referenced by an X and Y index in the array.
My NPC''s (no player character yet) blit instantly from one tile to another, which at 30 fps (throttled) looks pretty freakin'' unrealistic.
So how do you make characters move smoothly on a tile-based map? My current theory is this:
STEPS
1. Get character''s destination tile (through whatever method).
2. Move character some constant number of x and y pixels per turn.
3. Check to see if character''s surface upper-left pixel is equal to upper-left of tile (I''d do the center pixels, but the tiles have even numbered pixel dimensions).
4. - If equal, you''re done.
- If difference is more than character''s move rate, move
again.
- If difference is less than character''s move rate, move
character to destination.
Is that it? If so, I have two questions -
1. Does it matter where in my walking animation the character is when he hits his destination? In other words, if I switch from mid-walk to a standing image, do I just depend on the frame rate to make it look smooth, or do I actually have to sync the stride with the number of steps to move from one tile to another?
2. How do you do collision detection in a system like this? Is it tile-based, or pixel/rectangle based?
Thanks for helping a newbie,
Chris
1. I''d say it really depends on how many frames of animation you have for your walking character. If the number is small, say 3, then you don''t really have to worry about it.
2. The easiest way is to just do collision based on the tile. There is either something in the tile or not. You can get more complicated by checking the tile first, then if there is a collision, check the actual coordinates or bounding box to see if there is a real collision.
---
Make it work.
Make it fast.
"Commmmpuuuuterrrr.." --Scotty Star Trek IV:The Voyage Home
2. The easiest way is to just do collision based on the tile. There is either something in the tile or not. You can get more complicated by checking the tile first, then if there is a collision, check the actual coordinates or bounding box to see if there is a real collision.
---
Make it work.
Make it fast.
"Commmmpuuuuterrrr.." --Scotty Star Trek IV:The Voyage Home
quote:Original post by Gollum
So how do you make characters move smoothly on a tile-based map? My current theory is this:
STEPS
1. Get character''s destination tile (through whatever method).
Not quite clear on what you mean to convey, the wording leaves me unsure of what you speak in regard to.
quote:2. Move character some constant number of x and y pixels per turn.
Yes. Actually if the background scrolls it is generally easiest to "lock" your character to the center of the screen just scrolling the background. Then when a map boundry is present at the visible edge "unlock" the character sprite for free movement over the background. I use a system of fine, tile, and world cordnates for my sprites in which smaller cords increment the larger ones. For example; the fine x cord is incremented when the character movement takes place, after the fine x = tile boundry you would modify the tile x and fine x cords to reflect the new position in the world. You can now use the tile cord to find which background tile to begin drawing and use the fine cord to determain the pixel in the tile to begin drawing to screen from. The result is the background can smoothly scroll and you draw your sprite beginning at screen x,y over the background if "locked", or if "unlocked" just begin drawing your sprite to screen over your background at screen x,y (based on tile x,y at x,y pixel information) instead of moving the background.
A divided system like that can be a help in determaining if you need to check for collision, and is a gift from god if used properly in programming things like MMORPGs where simplifing collision detection on numerous objects at one time is a priority.
quote:3. Check to see if character''s surface upper-left pixel is equal to upper-left of tile (I''d do the center pixels, but the tiles have even numbered pixel dimensions).
See under #2 above, and I always base everthing on the upper-left pixel... Oh, even numbers are a good thing!
-Now on to section 2-
quote:1. Does it matter where in my walking animation the character is when he hits his destination? In other words, if I switch from mid-walk to a standing image, do I just depend on the frame rate to make it look smooth, or do I actually have to sync the stride with the number of steps to move from one tile to another?.
As stated by CaptainJester it is highly dependent on the number of frames in the walking animation. If you are using several frames I still recommend an "x number walking frames then standing frame then x number walking frames" type of design, but you really need to sync the stride to movement. This way would have it where you continue sprite movement for at least however many pixels it takes "x number walking frames" to arrive at the standing frame, also it makes for an inertia(excuse my spelling) effect like in real human movement.
quote:2. How do you do collision detection in a system like this? Is it tile-based, or pixel/rectangle based?
Please see section 1, question 2''s reply. I use the tile cords to find collision and then test collision per pixel on effected tiles if fine collision is needed. In other words; which tiles are sprites over>>does that tile register as a collision for that sprite>>if so take required actions. For sprite to sprite collision it is basicly the same idea. If you are doing three axis collision detection you check tile x,y then check the z values if tile x,y suggests a possible collision.
Hope this was helpful!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement