Tutorial 3: Animation/Projectile Motion

Moving Sine Wave

To have a sine wave which moves i.e. is animated the function must somehow depend on time. One way to do this is to modify the angle argument: y = A * sin(k * x + w * t). Modify your code from last week’s tutorial:

Projectile Motion: Example code

Start with the projectile motion example code from the lecture available here. Compile this code with the usual flags and then run it.

Note that the code includes an update (or idle) function, which is set using the function callback glutIdleFunc(update); This registers the update function with glut so it can be called automatically, just like the display function. The update function will be called continuously, which makes it ideal for running any game logic or calculating any animation. The function also calls glutPostRedisplay() which ensures that the display function will be called each frame.

The update function also includes a calculation of a dt variable, which stores how much time has elapsed between each frame (in seconds). This is useful for numerical integration, which works using changes in time. It also includes a calculation of a t variable, which is how much overall time has elapsed, which is useful for analytical integration.

While previous example code has included a glutKeyboardFunc callback, this week’s code includes the use of the ‘s’ key to start the animation, as well as the ‘i’ key to toggle between analytical and numerical integration.

Note that the program includes code to draw an on screen display or OSD showing frame rate and frame time.

Drawing the Projectile

Extra:

Implement your own circle drawing function based on the lecture notes. Use parametric coordinates, i.e. angles and x=cos(θ) and y=sin(θ).

Velocity Vector

Circle with direction vector and axes

Extra: Smooth rotation/speed

It is possible to have the rotation/speed update smoothly using the callback functions glutKeyboardFunc to set a key flag to true for a key that is pressed and the glutKeyboardUpFunc to set that flag to false. You can then check the value of this flag in the idle function and increase or decrease the value of the rotation by 45 degrees per second when the appropriate key is being held down (as well as using dt to change the speed). Leave this for now and come back to it later after finishing the rest of the tutorial

Drawing the Parabola

Circle with direction vector, parabola and axes