[英]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.