# Real time position calculation

• March 6th, 2013, 02:46 AM
Chrono251
Real time position calculation
Hi everyone!
I'm developing a small program that is supposed to simulate a plane flying and to show the position relative to a ground station on a gauge.
I wrote the code to draw the gauge with GDI, but I'm not sure how to calculate the position of the plane efficiently.
The plane position will be simply plotted on the form with a god's eye view.

My initial thought was to use a timer set to, say, 0.1s, and calculate how much the plane has moved by calculating the exact time difference between subsequent frames. I belive this method can be quite inefficient.
The second idea I had was to use XNA snd determine the movement in the update method. I consider this to be an overkill for the purpose of my program.

Any suggestions?
• March 6th, 2013, 10:19 AM
BioPhysEngr
Re: Real time position calculation
It's probably best to measure time on an absolute scale (from the start of the simulation) rather than constantly calculating time distances between frames; otherwise you will probably accumulate some error as frames progress. GDI might be too slow for your purposes (depends on how complex the rendering; if it's quite simple it will probably be OK, otherwise consider DirectX).

I think the timer will be too inefficient; instead try something like:

Code:

```frameRate = 1000 / 30; //30 Frames per second while( true ) {     //Get the time of the frame     //Update the plane position     //Draw the new position     //Some simulation termination     if( someCondition )         break;     //Get the absolute time     timeToExecuteThisLoopInstance;     // Wait for the next frame     Thread.Sleep( Math.Max(0,frameRate -timeToExecuteThisLoopInstance) ); }```
Hope that helps!
• March 6th, 2013, 11:43 AM
Chrono251
Re: Real time position calculation