import java.awt.*; import graphics.Animation; import graphics.Sphere; import graphics.Cube; import graphics.Cone; public class hw5 extends ThreeDApplet { double speed = 0.5; Sphere unitSphere; Cube unitCube; Cone unitCone; Cube body; Cube headTop; Cube headBottom; Sphere leftEye; Sphere rightEye; Cone leftTooth; Cone rightTooth; Animation bodyTurn; Animation walkTowardsUs; Animation cutAcross; Animation headTurn; Animation chomp; public void init() { super.init(); unitSphere = new Sphere(); unitSphere.calculate(); unitCube = new Cube(); unitCone = new Cone(); unitCone.calculate(); bodyTurn = new Animation(8); bodyTurn.linear = true; bodyTurn.looping = false; bodyTurn.addKeyFrame(0, Math.PI/4); bodyTurn.addKeyFrame(3, 0); bodyTurn.addKeyFrame(6, -Math.PI/4); bodyTurn.addKeyFrame(10, 0); walkTowardsUs = new Animation(2); walkTowardsUs.looping = false; walkTowardsUs.addKeyFrame(0, -40); walkTowardsUs.addKeyFrame(10, -2); cutAcross = new Animation(8); cutAcross.linear = false; cutAcross.looping = false; cutAcross.addKeyFrame(0, -15); cutAcross.addKeyFrame(3, 20); cutAcross.addKeyFrame(6, -5); cutAcross.addKeyFrame(10, 0); headTurn = new Animation(3); headTurn.addKeyFrame( 0, -Math.PI/6); headTurn.addKeyFrame(1.5, Math.PI/4); headTurn.addKeyFrame(3.0, -Math.PI/6); chomp = new Animation(3); chomp.addKeyFrame( 0, -Math.PI/4); chomp.addKeyFrame(0.25, 0); chomp.addKeyFrame( 0.5, -Math.PI/4); } void draw(Graphics g) { //background g.setColor(Color.white); g.fillRect(0,0,w,h); g.setColor(Color.black); drawShape(g, body); drawShape(g, headTop); drawShape(g, headBottom); drawShape(g, leftEye); drawShape(g, rightEye); drawShape(g, leftTooth); drawShape(g, rightTooth); } void animate() { body = (Cube)unitCube.clone(); headTop = (Cube)unitCube.clone(); headBottom = (Cube)unitCube.clone(); leftEye = (Sphere)unitSphere.clone(); rightEye = (Sphere)unitSphere.clone(); leftTooth = (Cone)unitCone.clone(); rightTooth = (Cone)unitCone.clone(); push(); //scene //move everything out translate(0,0,-15); push(); //gator translate(0, -1,0); //move down a bit translate(0,0, walkTowardsUs.get(elapsed)); //walk along z-axis translate(cutAcross.get(elapsed), 0, 0); //walk along x-axis //rotateX(Math.PI/8); //turn whole body rotateY(bodyTurn.get(elapsed)); push(); //head translate(0,1,1); //move head relative to body rotateY( headTurn.get(elapsed)); //rotateY( headTurn.get(elapsed) ); push(); //top jaw rotateX( chomp.get(elapsed) ); translate(0,0.25,1); //move center of rotation to edge push(); translate(-0.5, 0.75, -0.65); scale(0.35, 0.35, 0.35); transform(leftEye); pop(); push(); translate(0.5, 0.75, -0.65); scale(0.35, 0.35, 0.35); transform(rightEye); pop(); push(); translate(-0.5, -0.85, 0.65); scale(0.25, 0.5, 0.25); rotateX(-Math.PI/2); transform(leftTooth); pop(); push(); translate(0.5, -0.85, 0.65); scale(0.25, 0.5, 0.25); rotateX(-Math.PI/2); transform(rightTooth); pop(); scale(1, 0.25, 1); transform(headTop); pop(); //top jaw push(); //bottom jaw translate(0, -0.25, 1); scale(1, 0.25, 1); transform(headBottom); pop(); //bottom jaw pop(); //head scale(1, 0.5, 1); transform(body); pop(); //gator pop(); //scene } //opposite of projecting/rendering - turns screen coordinates into 3D coordinates //(sort of) private double[] screenTo3D(int x, int y) { double[] point = new double[2]; point[0] = 2.0 * (x - w/2.0) / w; point[1] = -2.0 * (y - h/2.0) / h; //flip y-axis return point; } }