<?
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);
}
}
?>
TranceAddict Forums (www.tranceaddict.com/forums)
- Chill Out Room
-- Java problem: bouncing balls
Java problem: bouncing balls
I dunno if there's any Java geeks in here (tell me where I could get help if you know). Anyway this might be more of a physics problem. I've gotten 2 balls bouncing good (assignment in a math course), but only when one balls weight is twice as big as the other one's. I want to preserve the energy in the collisions.
So:
if
mass1 = 2*mass2 or mass2 = 2*mass1 and elasticColl = 1.5
everything works fine. I'm not sure what's wrong.
CLICK HERE FOR APPLET
If anyone feels they could find the problem (I doubt it because it has to be done in a few hours), feel free to try.
PHP:
yes
Fucking hell...you can tell I'm desperate... I'M ASKING THE COR FOR HELP LOL.
Bah... waste of time I guess... 
| quote: |
| Originally posted by DjDeComp yes |
Collision and energy remind me a lot of these equations:
Momentum = mass*velocity <---Applies to collisions
kintetic energy(KE) = 1/2*(mass)(velocity)^2
You say you want to preserve the energy so somehow coming up with an equation relevant to the above you can set it equal to zero. Do you have a book outlining some sort of procedure and do you understand what I'm trying to say?
| quote: |
| Originally posted by Cloudburst Fucking hell...you can tell I'm desperate... I'M ASKING THE COR FOR HELP LOL. Bah... waste of time I guess... |
you may want to check this site out
http://javahelp.com/mass
Powered by: vBulletin
Copyright © 2000-2021, Jelsoft Enterprises Ltd.