[英]Point Inside Circle Collision Response: How do you keep the Point inside of the circle?
我给出了当前需要解决的小问题的图表。 我的主要目的是防止该点超出圆弧范围。 没有其他的。
圆的中心位于(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.
}
}
我留下评论的部分是我无法弄清楚的地方。 我确实尝试将点的velocityX
和velocityY
为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%算法,因为我非常依赖于基于矢量的运动。
要阅读的提示:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.