Ive had a look over numerous 3D sphere-sphere intersection questions and unfortunately they are either too far ahead of my ability to comprehend or not tailored to what I am looking for.
This is within the Unity Game Engine and using c#
I have managed to get this piece of code working:
public void calculatePoints_H(Vector3 c1p, Vector3 c2p, float c1r, float c2r, out Vector3 startLine, out Vector3 endLine)
{
//c1p = circle one position
//c1r = circle one radius
Vector3 P0 = c1p;
Vector3 P1 = c2p;
float d,a,h;
// d is the Distance Between the Center of the Spheres
d = Vector3.Distance(P0,P1);
// 'a' is the distance from the first sphere to the center of the
// circle resulting from the intersection of the spheres
a = (c1r*c1r - c2r*c2r + d*d)/(2*d);
// h is the radius of the resulting circle
h = Mathf.Sqrt(c1r*c1r - a*a);
// P2 is the center of the resulting circle
Vector3 P2 = (P1 - P0);
P2 = (P2 * (a/d));
P2 = (P2 + P0);
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;;
//draw visual to screen (unity 3D engine)
Debug.DrawLine(new Vector3(x3,0,y3), new Vector3(x4,0,y4),Color.green);
//out parameters for a line renderer
startLine = new Vector3(x3,0,y3);
endLine = new Vector3(x4,0,y4);
}
Currently this code allows me to calculate the two points on the x and z axis of two spheres intersecting and then draw a line.
What I want to achieve is a xyz intersection point so I can also add height (y vector 3 value) into the method so I can have a sphere intersect another from any direction/height
Could someone help me understand the way to go around doing this please, my brains a little fried and I fear its a simple solution I am missing?