Real time position calculation
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 3 of 3

Thread: Real time position calculation

  1. #1
    Join Date
    Jan 2012

    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?

  2. #2
    Join Date
    Feb 2011
    United States

    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:

    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 )
        //Get the absolute time
        // Wait for the next frame
        Thread.Sleep( Math.Max(0,frameRate -timeToExecuteThisLoopInstance) );
    Hope that helps!
    Best Regards,

    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Jan 2012

    Re: Real time position calculation

    Thanks for your reply!

    You are right about time measurement, it's also easier to implement it they way you suggested.

    Graphics are pretty simple, the output is something that looks like this: (I found this image online).
    I divided the image into layers, as some of them are always the same, some just rotate and some others change. I don't expect this to require heavy CPU usage.

    I haven't worked a lot with DirectX so far, I just used DirectMusic and DirectSound in a small program I made a few years ago. I'll look into Direct2D as well, I like the idea of having smoother gfx than GDI can provide.

    I'll try to write the update loop as you recommended, calculating the position is pretty simple stuff so the thread shouldn't take too much time to perform the operations.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


HTML5 Development Center