2010-04-20 164 views
4

我有一个简单的Java applet,它有两个用户控制的球,使用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; 

和用于第二球:

if (xPosition2 > (300 - radius)){ 
    xSpeed2 = -xSpeed2; 
} 
else if (xPosition2 < radius){ 
    xSpeed2 = -xSpeed2; 
} 
else if (yPosition2 > (300 - radius)) { 
    ySpeed2 = -ySpeed2; 
} 
else if (yPosition2 < radius){ 
    ySpeed2 = -ySpeed2; 
} 
xPosition2 += xSpeed2; 
yPosition2 += ySpeed2; 
  • 小应用程序是300个像素乘以300个像素。
  • radius存储圆的半径。
  • xPositionxPosition2存储两个球的x坐标。
  • yPositionyPosition存储用于两个滚珠的y坐标,
  • xSpeedxSpeed2商店两个球在x的速度。
  • ySpeedySpeed2存储两个球的y速度。
+0

欢迎SO,鲍勃。这实际上是一个数学问题,而不是一个编程问题,因为没有办法做到你想要的内容到Java语言中。为了让你开始,但是,这样想吧:检测碰撞意味着检测球何时会重叠或碰触,对吗?你知道他们的立场和规模.... – Pops 2010-04-20 21:13:18

+0

雅,但我想不出任何东西。我尝试着通过数学来完成,最后我得到了彼此相交的球以及大约30行无用代码 – 2010-04-20 21:20:58

回答

5

使用http://java.sun.com/j2se/1.5.0/docs/api/java/awt/geom/Point2D.html,那里有一个距离方法,如果它小于他们碰撞的半径。

编辑: 错误,小于半径* 2,对不起

+0

Kool谢谢!我听说过一个相交的命令..但显然这只能用于矩形。 – 2010-04-20 21:15:14

+0

我会+1你,但我不能 – 2010-04-20 21:15:34

+0

在记忆鲍勃Twinkles,我有+1 – 2010-04-20 21:16:51

1

还有的Point2D在Java中,或者你可以自己做,这是很轻松的圆/圈的碰撞或球/球的碰撞。

int distXX = (xPosition1 - xPosition2) * (xPosition1 - xPosition2); 
int distYY = (yPosition1 - yPosition2) * (yPosition1 - yPosition2); 
if (radius*radius > distXX * distYY) { 
    ... // There's a collision 
} 
+1

请注意,典型的游戏开发人员新手错误将开始使用“平方根”来计算距离并检查半径,而不是平方根和检查*半径x半径*。上面的解决方案只使用了离散匹配,实际上*是*很多游戏中使用的东西(几十年前一直在写游戏,这是*做的方式)。 – SyntaxT3rr0r 2010-04-20 21:18:22

+0

oooh谢谢你! – 2010-04-20 21:24:13

+0

好吧我只是编译和运行,球与everyhing结果,但其他球 – 2010-04-20 21:30:44

-1
public boolean colliding(Ball anotherBall) { 
    double xDelta = (this.x + this.ballSize/2 + this.dx) - (anotherBall.x + anotherBall.ballSize/2 + anotherBall.dx); 
    double YDelta = (this.y + this.ballSize/2 + this.dy) - (anotherBall.y + anotherBall.ballSize/2 + anotherBall.dy); 
    double distance = Math.sqrt(Math.pow(xDelta, 2) + Math.pow(YDelta, 2)); 

    return (distance <= this.ballSize/2 + anotherBall.ballSize/2); 

} 
+0

erm这很好,但我运行的一个类的整个应用程序 – 2010-04-20 23:42:10

-1

这种联系是非常有用的!

Circle-Circle Collisions

这是非常详细和didatic


在该页面的底部有另一个链接,以更详细的东西!


我用中心方法之间的距离--- Circles

通过测量每个中心可以,如果他们说的碰撞之间的距离。 距离不应超过2半径的总和。

这里就是我所做的:

private boolean checkDrawContains(ShapeDrawable newHole) 
{ 
    long newCenterX = newHole.getBounds().left + (newHole.getBounds().width()/2); //Get the center of my shapes 
    long newCenterY = newHole.getBounds().top + (newHole.getBounds().height()/2); 

    for(ShapeDrawable hole: mHoles) // I was storing the circles in an ArrayList 
    { 
     long centerX = hole.getBounds().left + (hole.getBounds().width()/2); //Get the center of my shapes 
     long centerY = hole.getBounds().top + (hole.getBounds().height()/2); 
     long x = centerX - newCenterX; 
     long y = centerY - newCenterY; 
     long aux = (long) ((Math.pow(Math.abs(x),2)) + (Math.pow(Math.abs(y),2))); //Pythagoras the hard way :P 
     long distance = (long) Math.sqrt(aux); 
     long sRads = (newHole.getBounds().width()/2) + (hole.getBounds().width()/2); 

     if(distance <= sRads) { 
      return true; //Is Colliding! 
     } 
    } 
    return false; // Is not Colliding! 
}