# Trouble calculating acceleration

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

## Recommended Posts

Hi Guys,

I am trying to calculate acceleration due to gravity for a game I am making. So, far I have come up with this;

#include<windows.h> #include<iostream> double a=9.8; double v=0,u=0,t=0,tslf,currenttick,lasttick; int main() { lasttick=GetTickCount(); while(true) { currenttick=GetTickCount(); tslf=currenttick-lasttick; lasttick=currenttick; t=t+(tslf/1000); v=u+a*t; u=v; system("cls"); std::cout<<"Time since last frame: "<<tslf<<"ms\r\n"; std::cout<<"Velocity: "<<v<<" M/Sec \r\n"; Sleep(1000); } return 0; }

This is working as I intended. But, if I change the Sleep() time to a different value eg. Sleep(10), then the acceleration results vary. With Sleep(10) the calcualted velocity blows out by 10.

I can't figure out why this is as the clock is sampled on each loop.

Any help would be awesome.

##### Share on other sites
GetTickCount() is a pretty low-res timer, so using it will only produce stable results if the time between each loop is high (does Sleep(2000) work as expected?). Look in to the windows performance timer.

##### Share on other sites

But, Sleep(2000) halves the expected results

##### Share on other sites
Could you give some sample output for the sleep(2000) or sleep(10) case, please?

##### Share on other sites
Sleep(1000) results (Secs / Result)

0 0
1.01399 9.93711
2.02797 39.7483
3.04197 99.3708
4.05595 198.742
5.06993 347.798
6.08392 556.476
7.0979 834.714
8.11189 1192.45
9.12588 1639.62
10.1399 2186.16

Sleep(2000)

0 0
2.01236 19.7212
4.02474 78.8848
6.03711 197.212
8.04949 394.424
10.0619 690.243

So, at 10 seconds the results are totally different.

I have just tried with the high performance query counter and the results are the same as above (give or take).

##### Share on other sites
[color=#1C2837][size=2][color=#000000]t[color=#666600]=[color=#000000]t[color=#666600]+([color=#000000]tslf[color=#666600]/[color=#006666]1000[color=#666600]);
I guess this is your problem:

it should simply be

t=tslf/1000.0f;

What's that "u" for? it doesnt do anything.

you can just take it out and do:

v+=a*t

##### Share on other sites
To elaborate - t is the time elapsed since the program started, so a*t would the total velocity, not the change in velocity. You're adding it to the velocity from the previous iteration, so you want the change in velocity, not the total velocity. You would need either:

v = a*t;

or

v = u + a*tslf/1000; u = v;

the first of which calculates the velocity all over again from the total time and the second of which updates it each iteration using the velocity in the previous iteration. I would recommend the second, as it's more flexible - you're not limited to constant acceleration, as you'll still get the correct result if you change a from iteration to iteration.

(edit: now uses code tags)

##### Share on other sites
The reason for the 'u' is that it is part of the physics equation for acceleration;

v=u+a*t

So, that is why I had it in there.

I tried your suggestion but the results are still off.

Sleep(2000) - 10.0619 sec 295.819

Sleep(1000) - 10.1399 sec 546.539

The amount of sleep time is directly impacting the results. More sleep, lower values. Less sleep, higher values

##### Share on other sites
I see what is happening. The results for the 1sec sample are correct.

But, when you change the sample rate the velocity is only getting accumulated at that time.

So a 1sec sample will give 147 at the 5 second mark. But if you do a 5sec sample it will only be 49 as v=u+at. 49=0+9.8*5.

The velocity gained due to acceleration isnt being added. Now, I have to figure out how to take that into account.

##### Share on other sites

I tried your suggestion but the results are still off.

no you havent

 #include<iostream> using namespace std; double a=9.8; double v=0,u=0,t=0,tslf,currenttick,lasttick; int main() { lasttick=GetTickCount(); int c=0; int tt=0; while(true && tt<10000) { currenttick=GetTickCount(); tslf=currenttick-lasttick; lasttick=currenttick; t=(tslf/1000.0f); v+=a*t; std::cout<<"T"<<tt<<"ms\r\n"<<endl; std::cout<<"Velocity: "<<v<<" M/Sec \r\n"<<endl; Sleep(500); c++; tt+=tslf; } return 0; } 
[color="#1C2837"][font="CourierNew, monospace"] [/font]

compile, run and see yourself

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 14
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631431
• Total Posts
3000039
×