Public Group

# Time based movement and smooth animation

This topic is 3826 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, here's a problem that's been bugging me. (This is for DirectX7 - DirectDraw (C/C++) and only for 2D games.) I base all my sprite movement on time and therefore use a high precision timer. I specify a maximum number of pixel a sprite can move in 1 second, and this means no matter what the hardware my sprite will move 100 pixel in 1 second regardless of frame rate or cpu speeds. E.g. sprite_x = sprite_x + VELOCITY * FrameTime (VELOCITY is set to 100 (max pixels the sprite will travel in that direction in 1 second), and FrameTime is the time it took to render the previous frame) And I do this in order to move my sprites on both the X and Y axis. This is great when moving sprites left/right/up/down or diagonally. However, I want is to continue to base all sprite movement on time, so movement is consistant but I want my enemy sprites to move in nice attack patterns, e.g. SIN or COS waves, or round in circles. I am either going slightly insane or I just can't see the wood for the trees, as I can't figure out how to do that and ensure all my sprites still move no more than a set number of pixels per second therefore ensuring all movement is consistant on all types of hardware.... Has anyone else had/solved this issue....if so, some advice would be really appreciated. Cheers :) EDIT: Gave your thread a more descriptive title rather than "Untitled". - jba.

##### Share on other sites
Why don't you use a vector then? Let's say you have a vector with members x and y, you can normalize and scale it to a length you wish. Then do vector.x *= time and vector.y *= time and add the vector to the position. For sine, cosine or circular movement just calculate the vector differently.

Example (circular movement):

//create direction vector (change angle over time)
vector.x = sin(angle);
vector.y = cos(angle);

//get the length
length = sqrt(vector.x * vector.x + vector.y * vector.y);

//scale to target length tl
vector.x *= tl/length;
vector.y *= tl/length;

//multiply with time
vector.x *= time;
vector.y *= time;

sprite_x += vector.x;
sprite_y += vector.y;

##### Share on other sites
Hi Evil_Lord,

Ah, that's quite interesting. I've not used vectors before, but I like the idea.

Could you possibly explain a bit more about the Target Length. I don't understand what that value means or represents.

Also I see that you multiply the (x,y) co-ords by the 'frame time' but how do you stop a faster machine moving your sprite futher in the same amount time as a slower machine, as you don't seem to have defined a maximum distance it would travel in a set period of time. Or is that the Target Length bit??

Cheers

##### Share on other sites
Yes, target length is the target distance to move per period. I just used the expression to create a link to the length of the vector ;). Basically every machine will move the object with the same speed or by the same distance.

Another annotation: I'd not use pixels as the unit to measure distances but rather define a standard unit in your coordinate system. Then you might have a fixed "world"-unit to pixel ratio but you also have the flexibility to use different resolutions.

1. 1
2. 2
3. 3
Rutin
20
4. 4
khawk
14
5. 5

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633655
• Total Posts
3013187
×