0

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?

DanielV
  • 23,556
  • The intersection of 2 spheres is a circle. Can you please explain what the 2 point you are trying to connect with a line actually are? Also your code has a weird definition for P2, might want to check it. – DanielV Nov 12 '13 at 11:58
  • Theres two points are the coordinates on the x axis that the sphere1 cuts into sphere 2. Almost like plane intersection. – RacconLauncher Nov 12 '13 at 12:02
  • The intersection of 2 spheres is a collections of points that form a circle. Are you trying to find the range of X values is that could be a valid X value of one of the points of the circle? Instead of posting C# code and asking us to reverse engineer what it is trying to do, why can't you just tell us what it is suppose to accomplish? Spheres don't have axises, so the phrase "two points on the x and z axis of two spheres" doesn't make any sense at all. – DanielV Nov 12 '13 at 12:10
  • the end goal is to create a function that will give visual feedback of the points that the sphere - spheres intersect. I can see your point with the circle. The sphere is set in a 3d Cartesian space with its position at (0,0,0) the axis is my confusion with the terms and characteristics of a sphere. I am not versed in spherical geometry so I apologize about the confusion. Would you be able to inform me of how to create the circle that forms at the point(s) of intersection? – RacconLauncher Nov 12 '13 at 12:27
  • So you are trying to draw the circle that results from 2 spheres intersecting? On a screen it will be an ellipse. Is a parallel projection (objects far away are the same size as objects close up) fine? – DanielV Nov 12 '13 at 12:38
  • yeah that would be fine Daniel, thank you. – RacconLauncher Nov 12 '13 at 13:44

1 Answers1

0

The intersection of 2 spheres is a circle.

Let $C_1$ be the center of Sphere 1, and $C_2$ the center of Sphere 2. Let $\Omega$ be the center of the circle, this is the $P2$ value in the above code. Let $r$ be the radius of the circle, this is $h$ from the above C# code.

Let $C$ be an arbitrary point on the circle.

Two properties define the circle. First, every point must be the same distance from the center of the circle:

$$ |C - \Omega| = r$$

Second, a vector pointing from the center of the circle to a point on the circle must be perpendicular to a vector pointing from the center of the first sphere to the center of the second sphere:

$$ (C - \Omega) \cdot (C_2 - C_1) = 0 \tag {Dot product property}$$

The 2 conditions on a circle may be expanded:

$$ (C_x - \Omega_x)^2 + (C_y - \Omega_y)^2 + (C_z - \Omega_z)^2 = r^2 \tag{Eq1}$$

$$ (C_x - \Omega_x)(C_{2,x} - C_{1,x}) + (C_y - \Omega_y)(C_{2,y} - C_{1,y}) + (C_z - \Omega_z)(C_{2,z} - C_{1,z}) = 0 \tag{Eq 2}$$

To draw the circle you just need the $C_x$ and $C_y$ values. So solve Eq2 for $C_z$ and plug it into Eq1 , you get an ellipse as expected:

$$ K_1\,C_x^2 + 2K_2\,C_xC_y + K_3\,C_y^2 + 2K_4\,C_x + 2K_5\,C_y + K_6 = 0$$

with

$\begin{align} K_1 &= (C_{2,z} - C_{1,z})^2 + (C_{2,x} - C_{1,x})^2 \\ K_2 &= (C_{2,x} - C_{1,x})(C_{2,y} - C_{1,y})\\ K_3 &= (C_{2,z} - C_{1,z})^2 + (C_{2,y} - C_{1,y})^2 \\ K_4 &= -\Omega_x\left( (C_{2,z} - C_{1,z})^2 + (C_{2,x} - C_{1,x})^2\right) - \Omega_y(C_{2,x}-C_{1,x})(C_{2,y}-C_{1,y})\\ K_5 &= -\Omega_y\left( (C_{2,z} - C_{1,z})^2 + (C_{2,y} - C_{1,y})^2\right) - \Omega_x(C_{2,y}-C_{1,y})(C_{2,x}-C_{1,x})\\ K_6 &= (\Omega_y^2 + \Omega_x^2)(C_{2,z} - C_{1,z})^2 + \left(\Omega_x(C_{2,x} - C_{1,x}) + \Omega_y(C_{2,y} - C_{1,y})\right)^2 - (C_{2,z} - C_{1,z})^2 r^2 \end{align}$


Now as far as how to draw an ellipse...note that $\Omega$ is the center of the circle so it must be inside your ellipse. Consider the value of

$$ Q(V) = K_1\,V_x^2 + 2K_2\,V_xV_y + K_3\,V_y^2 + 2K_4\,V_x + 2K_5\,V_y + K_6$$

One nice property, because $Q$ is of continuous and $\Omega$ is inside your ellipse:
1) If $Q(\Omega)$ is positive, then $Q$ of every point inside the ellipse is positive
2) If $Q(\Omega)$ is negative, then $Q$ of every point inside the ellipse is negative

So you can only draw negative points, or only draw positive points, or only draw points where the sign changes to get the edge.

DanielV
  • 23,556