Skip to main content
GameDev.net gamedev.net
🔒 Locked

Animation problems

Started by NegativeSpace Dec 15, 2005 at 4:51 PM 1 replies 850+ views
Original Post
NegativeSpace
NegativeSpace
Hi, I'm animating a tile-based game in Java and am having a problem. My animation works by multiplying the time elapsed since the last game tick by a set number, and then incrementing the image's x or y location by that number. Fairly standard. Now, my tiles are 16x16 pixels, and to stop the sprite moving after 16 pixels, I simply use: float xDist = 0; //check for sprite stop float x = 0; //player's x loc private static final int TILE_DIM = 16; . . . if ((xDist >= TILE_DIM ) || (xDist <= -TILE_DIM)){ xDist = xDist % TILE_DIM; stop(); } else { x += dx * elapsedTime; xDist += dx * elapsedTime; } It's not very elegant, but I just check that if a number, xDist, is greater than 16, mod it by 16 and stop the animation, else increment x and xDist. It kind of works, but unfortunately it won't always move 16 pixels -- sometimes it'll be offset by two or three pixels which, whilst doesn't sound a lot, is noticeable. After a couple more moves it will work itself out, but another few more moves it'll be out again. This isn't really accurate enough. If anyone has any ideas on how to keep the sprite moving 16 pixels ONLY each turn, I'd appreciate it. Cheers, NegativeSpace
SnadderLars
SnadderLars
I'm not suer I understand correctly, but are you thinking about something like this:

movement = some integer movement times 16 pixels
newpos = pos + movement
if no collision at newpos
pos = newpos
else
don't move


?
bzroom
bzroom
if ((xDist >= TILE_DIM ) || (xDist <= -TILE_DIM)){xDist = xDist % TILE_DIM;stop();} else {x += dx * elapsedTime;xDist += dx * elapsedTime;}


In the bold line, it looks like your forgetting to set the x variable, your only updating xDist, which is just the counter. How does this look?

if ((xDist >= TILE_DIM ) || (xDist <= -TILE_DIM)){   xDist = xDist % TILE_DIM; //?  17 % 16 = 1 right?   x = xDist % TILE_DIM;     //?   stop();} else {   x += dx * elapsedTime;   xDist += dx * elapsedTime;}



I would do it like this, keeping your style. I probably would have seperated the first two if statements. But my example should work.

if ((xDist >= TILE_DIM ) || (xDist <= -TILE_DIM)){   float txDist = xDist; //store value   xDist = (TILE_DIM*Sign(xDist)); //set xdist to either - or + tile_dim, this () set can replace the xDist below, since I assume you zero it somewhere else.   x += txDist - xDist;  //correct over shoot, add (current distance - expected distance).   stop();} else {   x += dx * elapsedTime;   xDist += dx * elapsedTime;}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.