// Dartmouth CS 2, Winter 2009, Chris Bailey-Kellogg // Notes 11 | Sketch 4 Ball ball = new Ball(); float shotSpeed=10; float cannonLength=25; color shotColor = color(random(255),random(255),random(255),100); void setup() { size(400,300); smooth(); background(0); strokeCap(SQUARE); // for cannon } void draw() { // Erase just the area where the cannon is fill(0); stroke(0); strokeWeight(ball.r*2); ellipse(0,height,2*cannonLength,2*cannonLength); // Draw the cannon, pointing toward the mouse stroke(shotColor); strokeWeight(ball.r*2); pushMatrix(); translate(0,height); rotate(atan2(mouseY-height, mouseX)); line(0,0,cannonLength,0); popMatrix(); noStroke(); // Handle the ball ball.draw(); ball.update(); } void keyPressed() { if (key==' ') { // shoot // Compute the position at the end of the cannon by cos and sin // Compute the initial velocity the same way float angle = atan2(mouseY-height,mouseX); ball.shoot(cannonLength*cos(angle), height+cannonLength*sin(angle), shotSpeed*cos(angle), shotSpeed*sin(angle), shotColor); } else if (key=='c') // color shotColor = color(random(255),random(255),random(255),100); else if (key=='s') { // shot speed shotSpeed++; println("shot speed:"+shotSpeed); } else if (key=='S') { if (shotSpeed>1) shotSpeed--; println("shot speed:"+shotSpeed); } else if (key=='g') { // half as much gravity ball.gravity *= 0.5; println("gravity:"+ball.gravity); } else if (key=='G') { ball.gravity *= 2; println("gravity:"+ball.gravity); } else if (key=='d') { // half as much drag (measured as 1-d) ball.drag = 1 - (1-ball.drag)*0.5; println("drag:"+ball.drag); } else if (key=='D') { ball.drag = 1 - (1-ball.drag)*2; println("drag:"+ball.drag); } else if (key=='x') { // half as much damping ball.frictX = 1 - (1-ball.frictX)*0.5; println("x friction:"+ball.frictX); } else if (key=='X') { ball.frictX = 1 - (1-ball.frictX)*2; println("x friction:"+ball.frictX); } else if (key=='y') { // half as much damping ball.frictY = 1 - (1-ball.frictY)*0.5; println("y friction:"+ball.frictY); } else if (key=='Y') { ball.frictY = 1 - (1-ball.frictY)*2; println("y friction:"+ball.frictY); } }