CZ2104, Lab 2, due Wed, 30 August 2000

 

This set of exercises lets you use OpenGL to do animation or simulation.  Part 1 and 2 are compulsory (100 marks).  Part 3 is optional and can be very difficult.  If you do Part 3 correctly, you earn extra 40 marks of credits.

 

[Best viewed by IE5 or Word]

 

Questions:

 

 

Comments, and what to hand in:

 

Part 1

 

Draw a big circle of radius R=10, with center at (0,0).  In side the big circle, at angle q = p/6, draw a small circle (i.e. a disk) of radius r=1, centered at  ( (R-r)cos q, -(R-r) sin q ),

so that the two circles (balls) just touch.  The angle q is counted from downward vertical direction, counter-clockwise.   You decide how the circles should look like (color, just curves, or filled).

 

You can fix the window size at 500x500 pixels; you fix the viewing window at reasonable size so that the big circle is displayed.

 

You may ask how to draw a circle in openGL.  You can either use a polygon to approximate a circle, or use functions in GLU for circles.

 

1.: hand in the code. Your code should be carefully commented.  Also keep your soft copy in your directory for TA to inspect.  Same for Part 2, and Part 3.

 

TA will draw example figures on the whiteboard in the lab.

 

You can use your lab 1 programs as a starting point.  Write the graphic part in the display() function.

 

Part 2

 

Continuing with the program developed in Part 1, we let the ball rolling [Actually, it is more correct to say it is a rotating pendulum].   Instead of fixing the position of the smaller circle, we let the angle vary according to the law of physics:

                d2q/dt2 = - sin q.

 

One way to solve this is to write

               

                dv/dt = - sin q,

                dq/dt = v.

  

The differential equations can be solved with the updating rule.

 

                v(t+Dt) = v(t)  - Dt sin q(t), 

                q(t+Dt) = q(t) + Dt  v(t+Dt),

 

This means that, given the old values of v and q, the new value of v and q are obtained using the above formulas.   The ball is plotted at the new location.   Better algorithms for solving differential equations exists, such as Runge-Kutta method, which you’ll learn or have learned in other modules.

 

You can set the initial angle q = p/6, initial (angular) velocity to v=0.0, and fixed time step size Dt = 0.1.   Experimenting also with other values for q and v to see the effect.

 

VERY IMPORTANT HINTS:  the calculation of the new ball position should be done in the idle call-back function for one step only.  The display call-back function sets up the initial graph.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Write the one-step updating part in idle() function.  This function needs to be registered in the main().

 

The idle call-back may have not been discussed in class, you can read the text book and consult the example programs.

 

Use of other call-back functions, such as reshape, and

use of mouse and keyboard are optional.

 

Part 3 (Optional)

Same small circles in a big circle as above, this time, we assume that the circles flight in straight line inside the ball (except when two circles collide).  Your program should handle n circles, where n is a user option. When the small circle hits the big circle’s wall, the small circle gets reflected.  The direction of the circle is such that the incident angle is equal to the outgoing angle.  The incident/outgoing angles are measured with respect to the normal direction, i.e., radial direction of the big circle.  When two small balls (i.e. circles) collide, they follow the law of physics again, i.e., elastic collision with conserved energy and momentum.

 

 

 

 

 

3: This is a bit challenging. Don’t try unless you have time.

 

Read books on mechanics to study how to handle the collision of balls.