# C++ Using clock() with IsKeyDown

I am trying to replicate the game Asteroids as a class project. I am having trouble measuring the time elapsed while the "W" key is pressed.

This code is supposed to increase the force applied to the spaceship to increase the movement speed incrementally. This is supposed to let the space ship rotate while moving along a 'line' and then force while be slowly applied to the new direction the ship is angled.

I have tried to research this over many hours and I still don't understand the concept. It seems there are a few ways to go about this but neither attempts have been successful.This is for school submission for the first project of the first year, so obviously my code is very basic and not making use optimal use of classes/structs however I would still like to achieve this movement I have been seeking so that I can understand the concept for use in future projects.

I have tried the use of current/previous variables for speed but haven't been able to come up with an equation to accomodate that. I have also attempted the use of a timestep but have been told that is not the correct approach. Any help would be appreciated. You can view the bulk of my program at https://gist.github.com/Rifuli/4762704

• Okay. What's not working? – Anko Feb 9 '13 at 23:07

All that is doing is measuring the time taken for the processor to move from the first statement in your code to the third instruction. It isn't measuring the amount of time the key is pressed at all.

To measure the time that the key is pressed, you need to measure the time when the key is first pressed (and only when first pressed, not each time through the loop if the key is down), and subtract that from the current time.

However, there's no need for you to do this to implement asteroids. To accelerate while the key is held down you only need to apply a constant force on each iteration, ie. fPlayerSpeed += fPlayerAccel. This will make the speed increase over time, as expected.

The one complication is that if fPlayerAccel is measured in units per second, then you need to scale that accordingly, not by how long the key has been pressed, but by how long has elapsed since the last frame of logic. To do this you need to call clock() each frame, and compare the result each time to the result you got in the previous frame, and work out what fraction of a second has elapsed. Then you can multiply fPlayerAccel by that value before adding it to the speed.

There are other ways to approach game loops that some recommend, and which can make the game a bit more complex to write in general but which make the physics simpler and more consistent. It's worth reading about them and trying to understand what the pros and cons of each method are.

• This is what I have tried to do with an increment to fPlayerAccel per frame, and that is the point where I decided to try and use a timer between each frame rather than incrementing a variable per frame and basing the speed off that. – Tim Feb 9 '13 at 14:32
• What I am saying is that you don't need to increment acceleration each frame. Acceleration is usually a constant quantity; it's the speed that rises over time as the force from acceleration gets added to it. – Kylotan Feb 9 '13 at 15:57
• I'm not increasing acceleration though. It is a static float set to 0.4f. Could you please give me a code example; based on what I have written, as to how to implement this. From what I have gathered so far, my use of clock() is incorrect and thus isn't correctly calculating elapsed time. Sorry if I'm struggling to grasp the concept here, this is my first introduction to basic physics, and also my first game in C++. Cheers – Tim Feb 9 '13 at 16:33
• You just said "an increment to fPlayerAccel" above, hence the confusion. I can't really give an example of what you want because you don't need to calculate the elapsed time the key has been pressed to perform this operation properly. – Kylotan Feb 9 '13 at 16:44
• Please see my OP for an update. I would really like to understand how this is achieved. – Tim Feb 12 '13 at 13:07