我有一个简单的Java小程序,有两个用户控制的球,使用绘制java.awt.我需要一种方法来检测它们之间的碰撞.我有一个检测墙壁碰撞的算法:
if (xPosition > (300 - radius)){
xSpeed = -xSpeed;
}
else if (xPosition < radius){
xSpeed = -xSpeed;
}
else if (yPosition > (300 - radius)) {
ySpeed = -ySpeed;
}
else if (yPosition < radius){
ySpeed = -ySpeed;
}
xPosition += xSpeed;
yPosition += ySpeed;
Run Code Online (Sandbox Code Playgroud)
而对于第二球:
if (xPosition2 > (300 - radius)){
xSpeed2 = -xSpeed2;
}
else if (xPosition2 < radius){
xSpeed2 = -xSpeed2;
}
else if (yPosition2 > (300 - radius)) {
ySpeed2 = -ySpeed2; …Run Code Online (Sandbox Code Playgroud)