简体   繁体   English

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

[英]Point Inside Circle Collision Response: How do you keep the Point inside of the circle?

这个问题的含义图。

I have given a diagram of my current small problem that I need help with. 我给出了当前需要解决的小问题的图表。 My main purpose is to keep the point from going outside the circle. 我的主要目的是防止该点超出圆弧范围。 Nothing else. 没有其他的。

The center of the circle is positioned at (x, y). 圆的中心位于(x,y)。

I only solved a little bit of the problem, and that is the collision detection part of my problem, as given below: 我只解决了一点问题,这就是我的问题的碰撞检测部分,如下所示:

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.
    }
}

The part where I left a comment is the spot I couldn't figure anything out. 我留下评论的部分是我无法弄清楚的地方。 I did tried to set the point's velocityX and velocityY to 0, but I realized the point will just stay put whenever it touches the circle. 我确实尝试将点的velocityXvelocityY为0,但是我意识到,只要该点接触圆,它就会保持原状。

So, I'm sort of stuck. 所以,我有点卡住了。

I have resolved this issue. 我已经解决了这个问题。

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;
    }
}

I tried Oli Charlesworth's method from the comments, but it made things more... "complicated" than I expected. 我从评论中尝试了Oli Charlesworth的方法,但是它使事情比预期的要复杂得多。 Someone else mentioned I used a completely 100%, vector-based algorithm, since I'm relying a lot on vector-based movements. 有人提到我使用了完全基于矢量的100%算法,因为我非常依赖于基于矢量的运动。

TIPS TO THOSE WHO DO READ THIS: 要阅读的提示:

  1. If you're working on object movements and collisions with vectors, seek vector-based algorithms. 如果您正在研究对象的运动以及与矢量的碰撞,请寻求基于矢量的算法。
  2. If you're working with angles (either degrees or radians), use Oli Charlesworth's method. 如果您正在处理角度(度数或弧度),请使用Oli Charlesworth的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM