gpt4 book ai didi

java - 圆内点碰撞响应 : How do you keep the Point inside of the circle?

转载 作者:行者123 更新时间:2023-12-01 05:14:00 24 4
gpt4 key购买 nike

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,但我意识到只要接触到圆,该点就会保持原状。

所以,我有点陷入困境。

最佳答案

我已经解决了这个问题。

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% 基于 vector 的算法,因为我非常依赖基于 vector 的运动。

给阅读本文的人的提示:

  1. 如果您正在研究对象移动和 vector 碰撞,请寻求基于 vector 的算法。
  2. 如果您要处理角度(度数或弧度),请使用 Oli Charlesworth 的方法。

关于java - 圆内点碰撞响应 : How do you keep the Point inside of the circle?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11581738/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com