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?