本文介绍了碰撞检测圈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有了两个用户控制的球,一个简单的Java小程序,使用抽 java.awt中。我需要一种方法来检测与它们之间的碰撞。我有一个算法,用于检测与壁碰撞:

I have a simple Java applet that has two user-controlled balls, drawn using java.awt. I need a way to detect a collision with between them. I have an algorithm for detecting collision with the walls:

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 x 300像素。

  • 半径存储圆的半径。

  • xPosition位置 xPosition2 存储两个球的X坐标。

  • yPosition yPosition 存储y坐标两球,

  • xSpeed​​ xSpeed​​2 存储的X速度为两球。

  • ySpeed​​ ySpeed​​2 存储在y速度为两球。

    • The applet is 300 pixels by 300 pixels.
    • radius stores the radius of the circles.
    • xPosition and xPosition2 store the x coordinates for the two balls.
    • yPosition and yPosition store the y coordinates for the two balls,
    • xSpeed and xSpeed2 store the x velocities for the two balls.
    • ySpeed and ySpeed2 store the y velocities for the two balls.
    • 推荐答案

      使用的,还有一段距离的方法有,如果这是不到他们的碰撞半径。

      Use http://java.sun.com/j2se/1.5.0/docs/api/java/awt/geom/Point2D.html, there's a distance method there, if it's less than the radius they're colliding.

      编辑:
      犯错,小于半径* 2,不好意思

      Err, less than the radius * 2 , sorry

      这篇关于碰撞检测圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 20:26