# How to calculate the intersect of two spheres in a 3d space?

Right now, I have the equation for working out the two intersects of two circles in 2D space, however when I was studying http://paulbourke.net/geometry/circlesphere/ and reading about the 3D intersection it did not make much sense it what they are doing. I am aware that in a 3D space with two spheres intersecting there is an infinite number of possible solution along a circle.

Here is what I have so far in RBX.Lua for 2D intersection:

function calculateIntersect(p1, p2, j1, j2, el)
local x1, y1 = p1.Position.X, p1.Position.Y
local x2, y2 = p2.Position.X, p2.Position.Y
local r1, r2 = j1.Size.Z, j2.Size.Z
local DX, DY = x2 - x1, y2 - y1

local d = sqrt(DX^2 + DY^2)

if d > r1 + r2 then
--Out of range
return nil
elseif d < abs(r2-r1) then
--One circle is inside another
return nil
else
local cd = (r1^2 - r2^2 + d^2)/(2*d)
local halfcd = sqrt(r1^2 - cd^2)
local cmx, cmy = x1 + (cd*DX)/d, y1 + (cd*DY)/d
local intersect = {(cmx + (halfcd*DY)/d), (cmy - (halfcd*DX)/d)}

return intersect,
cf(el.p, p1.Position) * cf(0, 0, -r1/2),
cf(el.p, p2.Position) * cf(0, 0, -r2/2)
end
end


Given that you have $x_1, y_1, x_2, y_2$ for the positions of the circles and $r_1, r_2$ for the radii you can do:

$$d = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2}$$

Where $d$ is the distance between the circles.

$$chorddistance = {\frac{r_1^2 - r_2^2 + d^2}{2d}}$$

Where $chorddistance$ is the distance from the first circles center to the chord between intersects.

$$halfchordlength = \sqrt{r_1^2 - chorddistance^2}$$ $$chordmidpointx = {\frac{x_1 + (chorddistance * (x_2 - x_1))}{d}}$$ $$chordmidpointy = {\frac{y_1 + (chorddistance * (y_2 - y_1))}{d}}$$

And therefore the intersection:

$$intersectionx = {\frac{chordmidpointx+(halfchordlength*(y_2-y_1))}{d}}$$ $$intersectiony = {\frac{chordmidpointy+(halfchordlength*(x_2-x_1))}{d}}$$

Which is what is being used to calculate the 2 dimensional intersection, however the 3 dimensional intersection is much more complex, and is what I need help with. If when you explain could show and explain your logic and reasoning behind what you are doing, that would be very helpful, and also explaining what everything means. I would also like to refrain from using trigonometric functions such as $sin$ and $cos$ since I would like to explain it to other people using RBX.Lua.

To sum up : I would like to know a solution to solve a suitable intersection along a circle when two spheres intersect in a 3 dimensional space, and how to change it, explained in a way that would make sense.

Given that you only know their position and radii.

Thank you!

A more immediately understandable description is via a parametric vector equation of the form $$\mathbf c+r\mathbf u\cos t+r\mathbf v\sin t.$$ (This is where those trigonometric functions that you mention are likely coming into the picture.) Here, $\mathbf c$ is the circle’s center, $r$ its radius and $\mathbf u$ and $\mathbf v$ an orthogonal pair of orthogonal vectors that are parallel to the circle’s plane. Geometrically, this is just the parametric equation $r\cos t(1,0,0)+r\sin t(0,1,0)$ of a circle in the $x$-$y$ plane of radius $r$ centered at the origin that’s been rotated and translated into position. To tie this to the first paragraph, $\mathbf c+r \mathbf u$ is a solution to a reduced planar circle intersection problem (in the plane through the sphere centers and $\mathbf c+\mathbf u$), and the above formula describes all possible rotations of this point around the line through the sphere centers. I’m sure that one can find other parameterizations of the intersection circle, but I think this one is the most transparent.