<?
import java.awt.*;
public class Bounce extends Animation {
protected double X1, X2, Y1, Y2, Vx1, Vy1, Vx2, Vy2, dX, dY, vectorX, vectorY, Va1, Va2, Vb1, Vb2, VaP1, VaP2,
deltaT, deltaB, distance, metersPerPixel, gravity, elasticColl;
protected int radius1, radius2, firstTime=1, pixelX1, pixelY1, pixelX2, pixelY2, mass1, mass2;
protected Color color1 = Color.white;
protected Color color2 = Color.red;
protected void initAnimator() {
deltaT = 0.0005;// simulation time interval in seconds
setDelay((int)(1000*deltaT)); // needed for Animation superclass
gravity = 20;
elasticColl = 1.5;
mass1 = 10;
mass2 = 20;
X1 = 1; // in meters
Y1 = 2; // Y reference direction downwards!
X2 = 4;
Y2 = 5;
Vx1 = 4; // in m/s
Vy1 = -8;
Vx2 = -5; // in m/s
Vy2 = -1.3;
metersPerPixel = 40;
radius1 = 20; // in pixels!
radius2 = 40;
pixelX1 = (int) (metersPerPixel * X1); // screen position
pixelY1 = (int) (metersPerPixel * Y1);
pixelX2 = (int) (metersPerPixel * X2); // screen position
pixelY2 = (int) (metersPerPixel * Y2);
}
protected void paintAnimator(Graphics g) {
g.setColor(Color.black);
if(firstTime==1) {
g.fillRect(0,0,d.width,d.height);
firstTime=0;
}
g.fillOval(pixelX1 - radius1, pixelY1 - radius1, radius1 * 2, radius1 * 2);
g.fillOval(pixelX2 - radius2, pixelY2 - radius2, radius2 * 2, radius2 * 2);
//collision with walls
if (pixelX1 < radius1 || pixelX1 > d.width - radius1) {
Vx1 = -Vx1;
}
if (pixelY1 < radius1 || pixelY1 > d.height - radius1) {
Vy1 = -Vy1;
}
if (pixelX2 < radius2 || pixelX2 > d.width - radius2) {
Vx2 = -Vx2;
}
if (pixelY2 < radius2 || pixelY2 > d.height - radius2) {
Vy2 = -Vy2;
}
//Apply the gravity on the balls
Vy1 = Vy1+gravity*deltaT;
Vy2 = Vy2+gravity*deltaT;
//calculate the distance between the center of the balls using pythagoras theorem
//to check see if the balls collide
dX = pixelX2-pixelX1;
dY = pixelY2-pixelY1;
distance = Math.sqrt(dX*dX+dY*dY);
//collision check
if (distance < radius1+radius2) {
//Calculate the vector towards the center of the ball (in the direction of the collision)
vectorX = dX/distance;
vectorY = dY/distance;
//The new velocities in x- and y-axes of the collision
Va1 = (Vx1*vectorX + Vy1*vectorY);
Vb1 = (-Vx1*vectorY + Vy1*vectorX);
Va2 = (Vx2*vectorX + Vy2*vectorY);
Vb2 = (-Vx2*vectorY + Vy2*vectorX);
//The new velocities after collision
VaP1 = Va1 + elasticColl*(Va2-Va1)/(1+mass1/mass2);
VaP2 = Va2 + elasticColl*(Va1-Va2)/(1+mass2/mass1);
//Undo the projections
Vx1 = VaP1*vectorX - Vb1*vectorY;
Vy1 = VaP1*vectorY + Vb1*vectorX;
Vx2 = VaP2*vectorX - Vb2*vectorY;
Vy2 = VaP2*vectorY + Vb2*vectorX;
}
X1 += Vx1 * deltaT;
Y1 += Vy1 * deltaT;
X2 += Vx2 * deltaT;
Y2 += Vy2 * deltaT;
pixelX1 = (int) (metersPerPixel * X1);
pixelY1 = (int) (metersPerPixel * Y1);
pixelX2 = (int) (metersPerPixel * X2);
pixelY2 = (int) (metersPerPixel * Y2);
//paint the balls
g.setColor(color1);
g.fillOval(pixelX1 - radius1, pixelY1 - radius1, radius1 * 2, radius1 * 2);
g.setColor(color2);
g.fillOval(pixelX2 - radius2, pixelY2 - radius2, radius2 * 2, radius2 * 2);
}
}
?>
| ||||||||||||||||||||||||||||||||||||||||||||