Hi Guys,
Im currantly having a problem with simulating balls bouncing of each other in a 2d space, everything is working and all,
but it just doesant seem natural for me...
If we had any math wizzes here now is your time please:)
my circles are painted inside of rectangles.....
This is what is the code im using to find the new x and y coordinates of the circles.
specifically this piece here...
//Pythagerum theorem, a^2 + b^2 = c^2, to figure out the distance between the two circles' centers.
int distance = (int) Math.sqrt(((currantXa - currantXb) * (currantXa - currantXb)) + ((currantYa - currantYb) * (currantYa - currantYb)));
if (distance < radiasa + radiasb) {//if the distance is less than both thier radius combined, than they collided
int newSpeedXA = (xSpeeda * (massA - massB) + (2 * massA * xSpeedb)) / (massB + massA);
int newSpeedYA = (ySpeeda * (massA - massB) + (2 * massA * ySpeedb)) / (massB + massA);
int newSpeedXB = (xSpeedb * (massB - massA) + (2 * massB * xSpeeda)) / (massB + massA);
int newSpeedYB = (ySpeedb * (massB - massA) + (2 * massB * ySpeeda)) / (massB + massA);
full loop
//here we are detecting collision with other balls...
int xSpeeda, xSpeedb, ySpeeda, ySpeedb, currantXa, currantXb, currantYa, currantYb, radiasa, radiasb, massA, massB;
Color Colora, Colorb;
for (int x = 0; x < CircleRect.size(); x++) {
for (int y = 0; y < CircleRect.size(); y++) {
//first checking if the rectangle colide, to save cpu
if (CircleRect.get(x).intersects(CircleRect.get(y)) && x != y) {//!=y so we dont check the same circle against itself
Ball balla, ballb;
//getting the objects
balla = Circles.get(x);
ballb = Circles.get(y);
//getting thier data
xSpeeda = balla.getxSpeed();
xSpeedb = ballb.getxSpeed();
ySpeeda = balla.getySpeed();
ySpeedb = ballb.getySpeed();
currantXa = balla.getX();
currantXb = ballb.getX();
currantYa = balla.getY();
currantYb = ballb.getY();
radiasa = balla.getDiameter() / 2;
radiasb = ballb.getDiameter() / 2;
massA = balla.getMass();
massB = ballb.getMass();
//Pythagerum theorem, a^2 + b^2 = c^2, to figure out the distance between the two circles' centers.
int distance = (int) Math.sqrt(((currantXa - currantXb) * (currantXa - currantXb)) + ((currantYa - currantYb) * (currantYa - currantYb)));
if (distance < radiasa + radiasb) {//if the distance is less than both thier radius combined, than they collided
Colora = balla.getColor();
Colorb = ballb.getColor();
int newSpeedXA = (xSpeeda * (massA - massB) + (2 * massA * xSpeedb)) / (massB + massA);
int newSpeedYA = (ySpeeda * (massA - massB) + (2 * massA * ySpeedb)) / (massB + massA);
int newSpeedXB = (xSpeedb * (massB - massA) + (2 * massB * xSpeeda)) / (massB + massA);
int newSpeedYB = (ySpeedb * (massB - massA) + (2 * massB * ySpeeda)) / (massB + massA);
balla.setxSpeed(newSpeedXA);
balla.setySpeed(newSpeedYA);
ballb.setxSpeed(newSpeedXB);
ballb.setySpeed(newSpeedYB);
//adding thier new speeds so they are not colided anymore and the loops doesant check them twice
balla.setX(currantXa + newSpeedXA);
balla.setY(currantYa + newSpeedYA);
ballb.setX(currantXb + newSpeedXB);
ballb.setY(currantYb + newSpeedYB);
balla.setColor(Colorb);
ballb.setColor(Colora);
Circles.set(x, balla);
Circles.set(y, ballb);
ConvertBallToRect(x, balla);
ConvertBallToRect(y, ballb);
}
}
}
}