![]() |
| | #1 |
| Registered User Join Date: Dec 2008
Posts: 99
| Smooth walking on tile based map system I am developing a small game which requires the use of a map system. I chose to make that map system work upon a tile-based system. At the moment, each tile is 32x32, and I contain 24x20 tiles on my map. A walking system is also required for this game I'm developing, so I ran into some issues with making the loaded sprite walk "smoothly". At the moment, it jumps from tile to tile in order to walk. This makes it seem very unrealistic. I have tried many different ways to make the walking "smoother", but it requires me to change my tile's size in order to accommodate the sprite's size. This would not be an issue if I only used the same sprite in the game, but since I load different sprites which contain different measurements, I do not know how to make my walking system accommodate all of the sprites in order to make the walking realistic. I am not requesting any code whatsoever, simply ideas. Thank you |
| abraham2119 is offline | |
| | #2 |
| Registered User Join Date: Sep 2004 Location: California
Posts: 2,845
| This should probably go in the Game Programming forum. |
| bithub is offline | |
| | #3 |
| Registered User Join Date: Dec 2008
Posts: 99
| True, but it's more of a design issue then anything else. Therefore, I assumed it would be proper to post it here in order to see some ideas. |
| abraham2119 is offline | |
| | #4 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,258
| So you want to use a grid for your background basically, but you don't want to actually make movement stick to this grid? You want it to slide slowly across from one tile to the next? Well, then just decide how many frames you are going to use for sliding, and update that many times. You need to remember that now you can't use the indexes of the background tiles for movement points, but will instead have to use the position on the screen. IE: Take "frames" number of baby steps across, rather than one big jump. Now, do you move your character on your background, or does your character stay centered, and the background moves around them?
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #5 |
| Registered User Join Date: Dec 2008
Posts: 99
| I move my character on my background. I did not completely comprehend your idea; if you could reword it, it would be great. |
| abraham2119 is offline | |
| | #6 |
| Guest Join Date: Aug 2001
Posts: 4,923
| >> I did not completely comprehend your idea; if you could reword it, it would be great. In other words, forget about the tiles for a moment. You have a sprite, and the coordinate attached to some point on the sprite (eg: center, corner, etc). To make things simple the coordinate is relative to the screen (upper left point being origin on most systems, but it varies). When you move the 'character', you simply update the coordinate associated with the sprite, and then call a redraw function that copies the sprite at that position on the screen. So if you're coordinate was attached to the center of the sprite, the x offset would be: start_x = screen_x + sprite_x - ( sprite_width / 2 ) Of course screen_x is probably 0, so that could be ommited. Naturally, using the top left corner of the sprite instead make the calculation much simpler. Once you have that working, just set it up so that the background is drawn first, of course. |
| Sebastiani is offline | |
| | #7 |
| Registered User Join Date: Dec 2008
Posts: 99
| Sebastiani, thank you for your response. That was not my specific question, but no matter, your reply helped me come up with a simplistic idea. My problem was that my character was attached to a tile, and drawn on that tile. I simply relieved the character from being drawn on a tile, and limited only the background (grass, lake, etc) to be drawn in tiles. So basically, my paint method is as follows: Code: //draw tiles //draw sprites onto the screen Anyways, thank you all for your suggestions. |
| abraham2119 is offline | |
| | #8 | |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,258
| Quote:
If you want them to not be instantly from one tile to the next, then you need to draw them at various stages along the way from tile A to tile B. That was exactly your question. Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? | |
| quzah is offline | |
| | #9 |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,379
| The sprite has both a tile where it is located and an offset in pixels from the corner of that tile. To move the sprite, you increment the pixel offset smoothly and check for when it exits the bounds of the tile, and move it to the next tile. (Of course, the sprite also has an origin with respect to it's position, which I didn't implement here since that's orthogonal to this problem. I also didn't do any bounds checking, since the game should do that, not the sprite) Code: class Sprite
{
Sprite( int initialX, int initialY )
: mOffsetX( initialX ), mOffsetY( initialY )
{
mTileX = mOffsetX / TILE_WIDTH;
mOffsetX = mOffsetX % TILE_WIDTH;
mTileY = mOffsetY / TILE_HEIGHT;
mOffsetY = mOffsetY % TILE_HEIGHT;
}
void Move( int moveX, int moveY )
{
BumpCoordinate( mOffsetX, mTileX, TILE_WIDTH, moveX );
BumpCoordinate( mOffsetY, mTileY, TILE_HEIGHT, moveY );
}
// Get the coordinate of the current tile (measured in tiles)
int GetTileX() const { return mTileX; }
int GetTileY() const { return mTileY; }
// Get the absolute position in pixels
int GetX() const { return mTileX * TILE_WIDTH + mOffsetX; }
int GetY() conset { return mTileY * TILE_HEIGHT + mOffsetY; }
private:
int mTileX;
int mTileY;
int mOffsetX;
int mOffsetY;
void BumpCoordinate( int &coord, int &tile, int size, int bump )
{
coord += bump;
// Avoid division and modulus for the common case where we don't move
// farther than a single tile at once.
while( coord >= size ) { coord -= size; ++tile; }
while( coord < 0 ) { coord += size; --tile; }
}
};
__________________ "Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot |
| brewbuck is online now | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| New editor updates | Bubba | Game Programming | 8 | 11-05-2005 03:26 PM |
| [question]Simulating the Reliability of a Component-Based System [with my code] | burbose | C Programming | 4 | 06-13-2005 09:03 AM |
| [question]Simulating the Reliability of a Component-Based System | burbose | C Programming | 3 | 06-13-2005 07:28 AM |
| Setting Up a sector based grid system | Auron | C Programming | 6 | 05-20-2005 08:19 AM |
| Operating system construction | AdamLAN | Tech Board | 7 | 03-05-2005 01:31 PM |