Jump to content
  • Advertisement
Sign in to follow this  
Evil Ozzy

Time based movement and smooth animation

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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;

//add to position
sprite_x += vector.x;
sprite_y += vector.y;

Share this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!