点圆内碰撞响应:你如何将点保持在圆圈内?

时间:2012-07-20 14:51:42

标签: java collision-detection geometry point

Diagram of the meaning of this question.

我已经给出了一个我需要帮助的当前小问题的图表。我的主要目的是保持这个观点不会出现在外面圈子里。没别了。

圆心位于(x,y)。

我只解决了一点问题,那就是我的问题的碰撞检测部分,如下所示:

public void bound(Point p, Circle c){
    double distance = Math.hypot(p.x - c.x, p.y - c.y);
    if (distance >= c.radius){
        //Clueless from here on out.
    }
}

我发表评论的部分是我无法弄清楚的地方。我确实尝试将点velocityXvelocityY设置为0,但我意识到只要触及圆圈,该点就会保持不变。

所以,我有点卡住了。

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题。

public void reflect(Hole h){
    //R = -2*(V dot N)*N + V
    //N is normalized.
    double nx = (this.position[0]+this.diameter/2) - (h.x+16);
    double ny = (this.position[1]+this.diameter/2) - (h.y+16);
    double nd = Math.hypot(nx, ny);
    if (nd == 0)
        nd = 1;
    nx /= nd;
    ny /= nd;
    double dotProduct = this.speed[0]*nx+this.speed[1]*ny;
    this.speed[0] += (float)(-2*dotProduct*nx);
    this.speed[1] += (float)(-2*dotProduct*ny);
}

public void reflectResponse() {
    for (int i = 0; i <= 1; i++) {
        position[i] -= speed[i];
        speed[i] *= 0.992f;
    }
}

我从评论中尝试了Oli Charlesworth的方法,但它让事情变得更加“复杂”,超出了我的预期。其他人提到我使用完全100%的基于矢量的算法,因为我很依赖基于矢量的运动。

提示给他们的提示:

  1. 如果您正在处理物体移动和与矢量的碰撞,请寻找基于矢量的算法。
  2. 如果您正在使用角度(度数或弧度),请使用Oli Charlesworth的方法。