Speed of rotation with a timer...
I got my timer and its keeping everything the same on different computers like it should be doing but... How would I go about timing the speed at which my ship turns?
Here is the code that turns my ships (this is 2d btw if you didn''t know)
if(keyboard_state[DIK_LEFT])
{
Contention.TestPlayer->spriteset.frame--;
if(Contention.TestPlayer->spriteset.frame == 0)
Contention.TestPlayer->spriteset.frame = 39;
}
if(keyboard_state[DIK_RIGHT])
{
Contention.TestPlayer->spriteset.frame++;
if(Contention.TestPlayer->spriteset.frame == 39)
Contention.TestPlayer->spriteset.frame = 0;
}
Ok how this basically works is when the right or left arrows are pressed the it goes up or down one frame in my sprite bmp respectivly. This particular sprite turns 360 in 40 frames.
I have an Time_Elapsed variable which I normally use my multiplaying it by how many unit a second an object should move.
Here though I don''t have anything to multiply it with since TestPlayer doesn''t have a units per second variable for rotation, I guess my question is how would I go about using a units per second variable for rotation using this type of system.
I keep feeling the answer is obvious but I just can''t see it, thanks for any help.
~Kavos
I think you are getting confused between animation frames and displayed frames per second.
The formula you want is: the number of animation stages you want to move per second, times the number of frames per second.
You will need to keep an extra floating point variable to contain the overflow from Displayed Frame.
ie your code could look something like this, but you would organise it better (this just makes the example clear):
Hope that helps.
The formula you want is: the number of animation stages you want to move per second, times the number of frames per second.
You will need to keep an extra floating point variable to contain the overflow from Displayed Frame.
ie your code could look something like this, but you would organise it better (this just makes the example clear):
// Number of seconds to display the previous framefloat g_frameTime;// Current rotation framefloat g_rotateFrame = 0;// Number of Animation Frames you want to rotate every secondconst float g_rotateSpeed = 5.0f;{ // g_frameTime will be calculated each Display Frame. unsigned int currentAnimationFrame; if ( DIK_LEFT) { g_rotateFrame -= (g_frameTime * g_rotateSpeed); if ( g_rotateFrame >= 39.0f) { g_rotateFrame -= 39.0f } currentAnimationFrame = (unsigned int)g_rotateFrame; Contention.TestPlayer->spriteset.frame = currentAnimationFrame; } if ( DIK_RIGHT) { g_rotateFrame += (g_frameTime * g_rotateSpeed); if ( g_rotateFrame < 0) { g_rotateFrame += 39.0f; } currentAnimationFrame = (unsigned int)g_rotateFrame; Contention.TestPlayer->spriteset.frame = currentAnimationFrame; }}
Hope that helps.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement