Starting from:

$29

Problem Set 2 Plane Animation

CS334 Computer Graphics
Problem Set 2
Plane Animation

The purpose of this assigment is to become familiar with basic OpenGl and glut programming.
You will write an animation program for a circular ball in a polygonal room. Let (x(t), y(t))
denote the position of the ball after moving t seconds from initial position (x0, y0) with initial
velocity ( ˙x0, y˙0). The mass of the ball is 1. When the ball is in the air, it moves ballistically:
x(t) = x0 + ˙x0t
y(t) = y0 + ˙y0t − 0.5gt2
with g = 9.8 the gravitional constant. When the ball hits the polygon, an elastic collision occurs
then ballistic motion resumes. If the pre-collision velocity is v and the outward normal of the ball
at the point of collision is n, the post-collision velocity is w = v − 2(v · n)n.
The animation program computes the state (position and velocity) si of the ball at a sequence
of time steps ti starting from t0 = 0. Set the time step δ to 1/30 of a second. State ti+1 is computed
from state ti as follows. Compute the state sb at time ti + δ under the assumption that the ball
moves ballistically. If the ball is inside the polygon, set ti+1 = ti + δ and si+1 = sb. If not, set
ti+1 to the time tc where the ball hits the polygon, compute the position and velocity at tc under
ballistic motion, and update the velocity using the elastic collision formula.
User Interface
The program opens a window and processes keyboard and mouse commands.
p Create a new polygon a1, a2, . . . an and delete the previous polygon. Enter the first n − 1
vertices with left mouse clicks and the final vertex with a right mouse click. A middle mouse
click deletes the latest vertex. Display the polygon unfilled.
b Create a new ball and delete the previous ball. Press the left mouse key to specify the initial
position of its center, drag the mouse to specify its radius, and release the mouse key to
finish. Display the ball filled.
a Animate the ball from its initial position with initial velocity (0, 0). A mouse click restarts
the animation from the mouse position with velocity (0, 0). If the ball is not inside the
polygon, it does not move.
s Stop the animation.
q Exit the program.
1
Hints
• Use a variable to record the current state: creating a polygon, creating a ball, animating, or
neutral. Store the polygon, the ball and the state in global variables.
• Render the ball as a triangle mesh using problem 1 from homework 1.
• Use shader.h to construct shaders, as explained in class.
• Use vertexarray2.h to define vertex arrays and vertex index arrays. The vertices are 2D and
have no color.
• Use shader2p.vs and shader1.fs to draw the polygon in a specified color. Create a second
vertex shader for drawing the ball at its current position.
• Use glutTimerFunc and double buffering for animation.
2

More products