# How would you go about simulating accleration on an object....

• 08-15-2005
How would you go about simulating accleration on an object....
I need to simulate accelration on an object when the the left or right key is held down. At this point when hold down the left/right key I have the object moving 2 pxiles at an interval of 1 millisecond. I am guessing there will have to be some equation that works this over time. None the less I am confused and any insight would be great....

Thanks

• 08-15-2005
I guess it would be worth mentioning that there is no gravity issues invloved. This is simply the longer you hold the right key the faster the object will move to the right.
• 08-15-2005
Epo
Code:

```if(KeyIsDown) { //We Want to Accelerate (Set an Acceleration Value) } else { //We Don't Want to Accelerate (Set an Acceleration Value) } //Update Object's Velocity Based on Acceleration //Update Object's Position Based on Velocity```
There's a Pseudo Start
• 08-15-2005
jverkoey
The key components to acceleration are velocity and time. Therefor you will need to handle each.

For example, working in purely one dimension (the x-plane):
Code:

`float x,xv;`
You have an x position and an x velocity variable. To create acceleration/deceleration, you add and subtract to the velocity (usually multiplying the amount you add/subtract by the time that has passed). You then add the velocity to the position and yet again multiply by the time that has passed.

The reasoning behind this is as such:

Velocity is in m/s
Acceleration is in m/s/s or v/s

Therefor we take this scenario:

A car is accelerating at 10 m/s/s from rest (initial velocity=0m/s). How fast is the car going after one half second?

You figure this out by taking v=vi+a*t (v=velocity, vi=initial velocity, a=acceleration, t=time). Giving us: v=0m/s + 10m/s/s * 1/2s

Canceling out the units we get: 5m/s. This is how fast the object is going after one half second.

Now, to get back to the code, the reasoning why I showed you the above example is because it works the same way code-wise. Here is an example program:

Code:

```float x,xv; x=0;  // Initial position is 0 xv=0; // Initial velocity is 0 m/s xv+=10*0.5f; // 10 is our acceleration (in m/s/s) and 0.5 is the time elapsed in seconds x+=xv*0.5f; // Now we add the velocity to the position and this will give us our new position.```
If you run the above code's last two lines repeatedly, you will have an object accelerating faster and faster. Replace the 10 with how much acceleration you wish, and the 0.5f with the actual amount of time elapsed since the last calculation and you're set!
• 08-15-2005
C+noob
http://nehe.gamedev.net deals with alot of thatt just ignore the opengl and look how it increses texture mapping tutorials has this i think
• 08-16-2005
I just want to thank everyone for the help....

One problem is that as far as I can tell I do not have control over my time variable(at least as far as I can tell). I am coding in manage c++ and this is what I have in my Keydown event handler......
Code:

```        private: System::Void keyDown_press(System::Object *  sender, System::Windows::Forms::KeyEventArgs *  e)                 {                         switch(e->KeyCode)                         {                                 case Keys::Right:                                         dx=2;                                         break;                                 case Keys::Left:                                         dx=-2;                                         break;                                        case Keys::Space:                                         fire();                                         break;                         }                 }```
The variable dx represent the accleration of 2pixels per 1 millisecond (The 1 millisecond is handle in another function). Is there way to do this with out the variable time?

I was messing around with things like dx +=2 but that made the object acclerate way to fast.

EDIT: Beign that I am newer to programming and espically mananged C++ I don't even konw if I am correct in my assumptions. Now that I am thinking about it the 1 millisecond might not be the "time" I was thinking about. I think it is how oftern my the event handler is triggered when I hold a Keydown. I have to do some more thinking :(...
• 08-16-2005
Epo
Basing updates on time isn't generally a good idea (in my opinion). I could lead to problems such as, "What if the user only accelerates for half a second?", etc.

In many games, you generally want to update as fast as possible (i.e. every frame). Most operate on a level of looping through:

CheckWhichKeysArePressed();
UpdateAllPositionsAndVariables();
RedrawTheFrame();

Of course, other functions can be added for specific needs.

By doing this, you can avoid the problem above. If we throw in a "CheckTime()" function which returns the current time, before UpdateAllPositionsAndVariables(), then we can speed up the object, regardless of how much time has passed...I'll try to explain it well...:)

Code:

```long TimeNow, TimeLast, TimeDifference; TimeLast = CheckTime(); while(OurGameIsRunning == true) {         CheckWhichKeysArePressed();         TimeNow = CheckTime();         TimeDifference = TimeNow - TimeLast;         UpdateAllPositionsAndVariables(TimeDifference);         RedrawTheFrame();         TimeLast = TimeNow; }```
Then, in the UpdateAllPositionsAndVariables() function, (I.e. jverkoey's example), you can manipulate each variable (In his example, TimeDifference = 0.5 seconds), and you're not relying on a necessary 1 millisecond to pass.

Note: CheckTime() isn't a real function. Other functions such as GetTickCount() (To name one) are however.