3 つの球体が衝突する 3D ポイントを計算しようとしています。現時点では、トップダウンの観点から、これらの球が交わる場所の X、Z 位置があります。彼らが出会ったとき、私はゲームオブジェクトを配置して、彼らが出会った場所を示します。
これはコードです:
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);
}
今私がやろうとしているのは、これら3つが出会う高さを見つけることです. これは、現時点では、3 つの球がすべて同じ平面上にある必要があるためです。本当に、好きな場所に配置できる柔軟性が欲しいのです。
高さを方程式に組み込むことができるように、または私がする必要があることの正しい方向に私を向けることができるように、誰かが私のコードを修正するのを手伝ってくれませんか?