//(c) Rory Clune 02/10/2011 //Bouncing ball with cor < 1 and frictional energy loss float posX, posY, theta; float velX, velY, angVel; int radius = 50; float crN = 0.8; float crT = 0.1; //Sound import ddf.minim.*; Minim minim; AudioSample bounce; void setup() { size(600, 400); noStroke(); frameRate(100); smooth(); posX = width/2; posY = height/2; velX = 0; velY = 30; angVel = 0.5; //Sound: minim = new Minim(this); bounce = minim.loadSample("ball.mp3", 128); } void draw() { background(40,40,40); velY += 0.08; posX += velX; posY += velY; theta += angVel; if ((theta>0)&&(theta>2*PI)){ theta-=2*PI; } if ((posX - radius < 0)||(posX > width-radius)){ velX *= -1*crN; float newVelY = 0.9*((1-0.4*crT)*velY+0.4*(1+crT)*radius*angVel)/1.4; float newAngVel = 0.9*((1+crT)*velY+(0.4-crT)*radius*angVel)/(radius*1.4); velY = newVelY; angVel = newAngVel; if (posX - radius < 0){posX = radius;} else if(posX > width-radius){posX = width-radius;} if(abs(velX) > 0.1){ bounce.trigger(); } } if ((posY > height-radius)||(posY - radius < 0)){ velY *= -1*crN; float newVelX = 0.9*((1-0.4*crT)*velX+0.4*(1+crT)*radius*angVel)/1.4; float newAngVel = 0.9*((1+crT)*velX+(0.4-crT)*radius*angVel)/(radius*1.4); velX = newVelX; angVel = newAngVel; if(posY > height-radius){posY = height-radius;} else if (posY - radius < 0){posY = radius;} if(abs(velY) > 0.1){ bounce.trigger(); } } stroke(253); ellipse(posX, posY, 2*radius, 2*radius); stroke(126); line(posX + radius*cos(theta), posY + radius*sin(theta), posX - radius*cos(theta), posY - radius*sin(theta)); strokeWeight(4); }