小编Bob*_*les的帖子

圆的碰撞检测

我有一个简单的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)

java awt collision-detection

4
推荐指数
1
解决办法
1392
查看次数

标签 统计

awt ×1

collision-detection ×1

java ×1