簡體   English   中英

點在圓內碰撞響應:如何將點保持在圓內?

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

我留下評論的部分是我無法弄清楚的地方。 我確實嘗試將點的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%算法,因為我非常依賴於基於矢量的運動。

要閱讀的提示:

  1. 如果您正在研究對象的運動以及與矢量的碰撞,請尋求基於矢量的算法。
  2. 如果您正在處理角度(度數或弧度),請使用Oli Charlesworth的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM