synchronising with a physics thread

Hi all,

A problem I just stumbled on today: Since Jogl uses an animation thread (which I have synched to monitor refresh rate), how should I go about synchronising it with my physics thread (being called at a constant 30hertz)?

Has anyone dealt with similar issues?

TIA!

If you are drawing the same scene, that your physic engine is simulating, you will probably need to lock the entire scene for each of the tasks. So there is no point in using Threads at all.

I would suggest using the display loop as a basis.

Save the start time of your application and plan a physical simulation step every 30 (or so) msec. Before drawing each frame, read the current time and do all outstanding physical simulation steps before drawing.

I cannot get the description right, so I’ll try an Example:

Loop iteration 4:
Total elapsed time: 100
Simulation steps so far: 1
Outstanding steps: 2 (no. 2 (at 60 msec) and no. 3 (at 90 msec))
-> Do 2 steps, then paint.

loop iteration 5:
Total elapsed time: 110
Simulation steps so far: 3
Oustanding steps: none
-> paint

This solution can never run into any race condition and (sometimes important) the physical simulation is completely independent from the speed of the machine. Only the frame rate drops.