0

I'm trying to work out the 3D point in which 3 spheres will collide. At the moment, from a top down perspective, I have X, Z position of where these spheres meet. When they do, I place a game object to show where it is they have met.

This is the code:

public void calculatePoints_H(Vector3 c1p, Vector3 c2p, float c1r, float c2r, out Vector3 startLine, out Vector3 endLine)
{

    //get the positions
    Vector3 P0 = c1p;
    Vector3 P1 = c2p;

    float d,a,h;

    //d is the distance between centres
    d = Vector3.Distance(P0,P1);
    //a is checking if the spheres are inside, outside or colliding
    a = (c1r*c1r - c2r*c2r + d*d)/(2*d);
    //
    h = Mathf.Sqrt(c1r*c1r - a*a);

    //Debug.Log(d + " " + a + " " + h);

    Vector3 P2 = (P1 - P0);
            P2 = (P2 * (a/d));
            P2 = (P2 + P0);

    //Debug.Log(P2);

    float x3,y3,x4,y4 = 0;

    x3 = P2.x + h*(P1.y - P0.y)/d;
    y3 = P2.y - h*(P1.x - P0.x)/d;

    x4 = P2.x - h*(P1.y - P0.y)/d;
    y4 = P2.y + h*(P1.x - P0.x)/d;;

    //Debug.Log(x3 + " " + y3 + " " + x4 + " " + y4);

    Debug.DrawLine(new Vector3(x3,0,y3), new Vector3(x4,0,y4),Color.green);

    startLine = new Vector3(x3,0,y3);

    endLine = new Vector3(x4,0,y4);


}

Now what I'm trying to do is find the height at which point these 3 meet. The reason for this is because at the moment, my 3 spheres need to all be on the same plane. When really, I want the flexibility to have them placed anywhere I wish.

Could someone please help me amend my code so that I can incorporate height into the equation as well, or point me in the right direction of what I need to do?

4

1 回答 1

0

您是否知道这样一个事实,即“碰撞”(我假设您将其定义为“所有 3 个球体同时接触的点”)随机放置的三个球体产生0, 1, 2Inf碰撞点的数量,严格取决于位置和半径每个球体?

这不仅仅是一点。

您需要模拟空间并扫描感兴趣的点(即通过建议的 OctTree),或者您需要实际求解三个方程:

(x-x0)^2+(y-y0)^2=r0^2
(x-x1)^2+(y-y1)^2=r1^2
(x-x2)^2+(y-y2)^2=r2^2

其中 xNyNrN 是球体的参数,xy 是“碰撞点”坐标。

你可以用代数来做(如果你不喜欢数学,请参阅 wolfram aplha),或者你可以尝试任何零查找器:

(x-x0)^2+(y-y0)^2 + (x-x1)^2+(y-y1)^2 + (x-x2)^2+(y-y2)^2 - r0^2 - r1^2 - r2^2

我没有尝试过也没有分析过,但是球体是完全光滑且完全凸的,所以不应该有任何恶意的局部最小值,甚至简单的二等分或梯度滑动也可能就足够了。

但是,请注意,虽然“以代数方式求解”可以轻松地向您显示有 2 个或 INF 碰撞点,但“扫描空间”或零查找器可能只会返回一个点,即它击中的第一个/最好的点(或没有任何)。在这种情况下,要获得其他最小值,您可能必须以稍微不同的“起始位置”开始它。但这取决于您选择的确切扫描算法。

编辑:当然上面的方程应该是 3D 的,所以添加(z-z0)^2三次,抱歉。我不会纠正它,因为它会使所有内容的可读性降低。

于 2013-11-13T11:03:20.630 回答