<- previous    index    next ->

Lecture 12, Timing


Suppose you want to update the display at a uniform rate,
independent of the speed of the computer. Assuming the computer
you are using is reasonably fast.

 initialize your program
 loop
    start = get_the_time
    cause_display_update
                          repaint(); in Java

                          glFlush();
                          glutSwapBuffers(); in OpenGL

    do your calculations that will be used by your paint
    or display routine. (Physics, motion, etc.)

    loop
      now = get_the_time
      if ( now > start + your_update_time ) break
    end loop
 end loop

The above allows for a reasonable variation in the time to
do your calculations.

Double buffering is desirable for smooth motion.

To get the time in "C", unfortunately may be coarse.
time_cpu.c

To get the time of day in C.
time_of_day.c

The output is:
time_of_day.out


To get the time in Java, milliseconds.
time_of_day.java

The output shows all the stuff in "now"
time_of_day.outj

For dynamic display, you may want to follow somewhat
standard design of separating the display from the
physics of the motion. The classic "Three Body Problem"
has three masses, for us, The Sun, Earth and Moon,
and the basic laws of physics.

    F = G m1 m2 / d^2  F, A, V, S have x,y,z components        
    F = m2 v^2 / d     d^2 = (x2-x1)^2 + (y2-y1)^2 + (z2-z1)^2 
    A = F / m1         acceleration along force components     
    V = V + A dt       velocity update at delta time  dt       
    S = S + V dt       position update at delta time  dt       




For a simple delay in Java, see "sleep" in thread at end.
body3.java

Basically the Sun, Earth orbiting Sun and Moon orbiting the Earth.
Ummm? Just a little unstable numerically.
Actually, the Earth and Moon are slowly changing their orbit.
The Sun does not have enough mass, thus the Earth moves it a little.
Actually, the planets near alignment do move the Sun a little.


body3 in C, very close to Java version.
body3.c

With a snapshot of the output:
body3.jpg
body3.java

Python thread using tkinter and 2 second delay
threads_tk.py3 source code


submit quiz1 midnight or later 

    <- previous    index    next ->

Other links

Many web sites on Java GUI, AWT, Swing, etc.
Many web sites on Python wx, tk, qt, etc.

Go to top