I'm quite new to Java and I am using the ACM library for graphics. What I'm trying to do with this code is to:
- Introduce two balls, one on the left side and one in the middle. Both of them are in the middle, y coordinate-wise.
- Make the first ball move until meeting the second ball. Then stop and give the second ball a velocity proportional to its size (resembling a conservation of momentum).
- Make the second ball bounce of the wall, and move until meeting the first ball. Then stop and give the first ball a velocity proportional to its size once again.
- Make this action infinite.
My code is as such:
import java.awt.Color;
import acm.graphics.*;
import acm.program.*;
import acm.util.RandomGenerator;
public class Collision extends GraphicsProgram {
private static final double RADIUS_ONE = 35;
private static final double RADIUS_TWO = 65;
public void run() {
GOval ballOne = new GOval(0, (getHeight() - RADIUS_ONE)/2, RADIUS_ONE, RADIUS_ONE);
ballOne.setFilled(true);
ballOne.setColor(Color.GREEN);
add(ballOne);
GOval ballTwo = new GOval((getWidth() - RADIUS_TWO)/2, (getHeight() - RADIUS_TWO)/2, RADIUS_TWO, RADIUS_TWO);
ballTwo.setFilled(true);
ballTwo.setColor(Color.ORANGE);
add(ballTwo);
double velocityOne = 3;
double velocityTwo = 0;
while (true) {
ballOne.move(velocityOne, 0);
ballTwo.move(velocityTwo, 0);
if (ballOne.getX() >= getWidth() - RADIUS_ONE || ballOne.getX() <= 0) {
velocityOne = -velocityOne;
}
if (ballTwo.getX() >= getWidth() - RADIUS_TWO || ballTwo.getX() <= 0) {
velocityTwo = -velocityTwo;
}
if (ballOne.getX() + RADIUS_ONE >= ballTwo.getX()) {
if (velocityTwo == 0) {
velocityTwo = (velocityOne * RADIUS_ONE * RADIUS_ONE)/(RADIUS_TWO * RADIUS_TWO);
velocityOne = 0;
} else if (velocityOne == 0) {
velocityOne = (velocityTwo * RADIUS_TWO * RADIUS_TWO)/(RADIUS_ONE * RADIUS_ONE);
velocityTwo = 0;
}
}
pause(10);
}
}
}
If you run this code, you'll see it malfunctions. Upon meeting the second ball, the first ball doesn't stop moving and moves along with the second ball. But if you change velocityOne
to 2 instead of 3, it works all fine. Or if you RADIUS_TWO
to 45 instead of 65, the code functions as it should for every value of velocityOne
.
Could anybody explain me why this code is malfunctioning for most values, while working as it is supposed to with so little? Thanks a lot.