Homework 2OpenGL and Glut |
Modified: |
ASSIGNMENT (2 parts)
init(x, y, theta) Initialize (x,y) turtle position and orientation angle. forward( distance ) Move turtle forward distance in turtle world. Draw line if pen is down. right( angle ) Turn turtle right by angle degrees. left( angle ) Turn turtle left by angle degrees. pen( position ) Position down for line drawing as turtle moves forward or up for no line drawing.
For example, a turtle (OK, a cat):
- initially at the center of turtle world pointing toward the positive x axis - init(0,0,0)
- positions the pen down - pen(down)
- moves forward 2 - forward( 2 )
- turns left 90 degrees - left( 90 )
- and moves forward 1 - forward( 1 )
ö
#
ö""ö
Test by:
- rotating a square about one corner,
- generating the Sierpinski gasket of Figure 2.36 on page 80.
Hints:
- the turtle is in degrees but C math functions use radians (the conversion of angle q from degrees to radians is: q*3.14159/180.0)
- forward(distance) must compute the change in X and Y position when moving distance at orientation q degrees.
X += distance * cos(q*3.14159/180.0)
Y += distance * sin(q*3.14159/180.0)
Hints: The simulation can be relatively simple but should account for gravity and the elasticity of the ball. You should consult an elementary physics text for a detailed discussion of gravity and elasticity.
y = y0 + velocity * time - gravity * time2 / 2
When the balls bounces the velocity is:
velocity = (velocity - gravity * time)*e
where e (coefficient of restitution which we can arbitrarily choose 0..1). This is intuitively appealing time is always incremented and downward velocity is positive or negative.
- Determining the distance traveled by the ball and upward travel time following the rebound. The vertical distance traveled is:
distance traveled = gravity * time2 / 2
One way to model the ball bouncing is to increment time on the way down and decrement time on the way up after a bounce.
The height of the bounce is determined by e (coefficient of restitution which we can arbitrarily choose 0..1) where:
height' = e2*distance traveled
A ball with elasticity of 0.4 dropped from 10 meters would rebound to 1.6 meters.
The height of the rebound allows the calculation of the time of upward travel by:
time = sqrt( 2*height'/gravity)
TEST
- Using your turtle graphics API, draw a square with the upper-right corner centered in the window; redraw the square in small, uniform angle increments, 360 degrees about the original square's upper-right corner. To verify the accuracy of your algorithm, repeat, rotating 3600 degrees about the original upper-right corner. The visual effect will be interesting.
- Using your turtle graphics API, generate the Sierpinski gasket of FIGURE 2.38 on page 80.
- The bouncing ball; the visual effect should be based on the physical model of a bouncing ball.
Files stored on your W: drive are Web accessible by: http://homepages.ius.edu/username/filename
|
Ftp files by:
COMPILING AND EXECUTING IN VISUAL STUDIO
The following assignment serves as an introduction to OpenGL and Glut on a Microsoft system. OpenGL is part of Visual Studio while Glut for Windows must be downloaded (see above link).
To create an empty console project:
- In Visual Studio, select: File | New | Project
- In Other Languages select: C++ | Console Application
- Name: gasket
- On next screen, select: Application Settings | Empty Project
To add the code for the Sierpinski gasket:
- Select: View | Solution Explorer
- Right click: Source Files | Add | New Item
- Download gasket.c
- Copy and paste the code.
To define the location of glut.h and glut libraries
- Select: Project | Properties | Configuration Properties | C/C++
- Enter: Additional Include Directories, the location of the glut installation, probably C:\glut
- Select: Linker
- Enter: Additional Library Directories, the location of the glut installation, probably C:\glut
To execute and produce Figure 2.2 on page 45 of the text:
- Select: Build | Build Solution (ignore the warning).
- Select: Debug | Start without Debugging