Find height of a sphere, given it forms a tangent with a plane defined by a point and normal, and the X and Z coordinates of the sphere are known.

91 Views Asked by At

I'm currently making a game and have run into a problem I'm not quite sure how to solve, I'll try to lay it out as a maths question. None of the values are fixed, so I'm looking for an equation that solves the below question:

A plane lies on the position vector p0 <x0, y0, z0> and has a normal unit vector n <a, b, c>.

Given a sphere with a radius of R forms a tangent with the plane at an unknown position vector t <tx, ty, tz>:

Find the highest height of the center of a sphere, when the sphere is located at the position vector s <sx, sy, sz>, and sx and sz are known values, but sy is not.

The known vectors are p0, n, and the other known values are sx, sz, and R. The position vector t is unknown, and the height of the sphere sy is also unknown. Should it be necessary, another point on the plane p1 <x1, y1, z1> can be provided.

In my case, the Y axis is the up axis, the Z axis is the forward axis, and the X axis is the right axis.

I wanted to essentially do something similar to what is shown in this video, but in my case I know the length of the vector but not the positions: https://www.youtube.com/watch?v=zWMTTRJ0l4w

Here is 2d visualisation of the problem: Sphere and plane

I was only able to find the positions due to trial and error, but these were approximately correct.

Here is what I do know: The vector t to s will have the same direction as the normal vector, but with the magnitude being equal to R. It could then be defined as Rn, given that n is a unit vector. Given that, the vector could be defined as a translation of the vector Rn along the plane.

Beyond this point, I am a little confused, so any help would be appreciated. I would prefer to avoid cartesian form, and values can be stored in variables at later dates if necessary. I have v1:Dot(v2) and v1:Cross(v2) methods available to me, and I expect this probably will use the dot product at some point.

Thank you.

3

There are 3 best solutions below

0
On BEST ANSWER

Given

$$R, \ \ \cases{ \vec p_0 = (x_0,y_0,z_0)\\ \vec n = (n_x,n_y,n_z)\\ \vec s = (s_x,u,s_z)\\ } $$

determine $\vec t=(t_x,t_y,t_z),\ u,\ \lambda$ such that

$$ \cases{ (\vec t-\vec p_0)\cdot \vec n = 0\\ \|\vec t - \vec s\|^2 = R^2\\ \vec t-\vec s = \lambda \vec n } $$

five equations and five unknowns $t_x,t_y,t_z,u,\lambda$.

NOTE

One solution is

$$ \cases{ t_x = s_x + \frac{R n_x}{\|\vec n\|}\\ t_y = \frac{1}{n_y}\left(n_x(x_0-s_x)+n_y y_0+n_z(z_0-s_z)-\frac{(n_x^2+n_y^2)R}{\|\vec n\|}\right)\\ t_z = s_z + \frac{R n_z}{\|\vec n\|}\\ u = \frac{1}{n_y}\left(n_x(x_0-s_x)+n_yy_0+n_z(z_0-s_z)-\|\vec n\|R\right)\\ \lambda = \frac{R}{\|\vec n\|} } $$

2
On

It took some thinking, but I managed to find a solution to finding sy.

Essentially, the length of the vector between t and s, which I will call vector R can be defined using:

abs|n ⋅ (s - p0)|.

Since the length of the vector is already known, this allows us to set up an equation such that:

R = abs|n · (s - p0)|

Then, expanding:

R = abs|<nx, ny, nz> · <sx - x0, sy - y0, sz - z0>|

R = abs|nx(sx - x0) + ny(sy - y0) + nz(sz - z0)|

R = abs|nx·sx + ny·sy + nz·sz - nx·x0 - ny·y0 - nz·z0|

Given that it is absolute, that means that it could be +R or -R.

R = nx·sx + ny·sy + nz·sz - nx·x0 - ny·y0 - nz·z0

OR

-R = nx·sx + ny·sy + nz·sz - nx·x0 - ny·y0 - nz·z0

First I will re-arrange the positive, keeping in mind there is also a negative solution.

ny·sy = R - nx·sx - nz·sz + nx·x0 + ny·y0 + nz·z0

Dividing by Y:

sy = ((R - nx·sx - nz·sz + nx·x0 + nz·z0) / ny) + y0

Remembering there is also -R this gives us the two solutions:

sy1 = ((R - nx·sx - nz·sz + nx·x0 + nz·z0) / ny) + y0

sy2 = ((-R - nx·sx - nz·sz + nx·x0 + nz·z0) / ny) + y0

Then, selecting the maximum of each:

sy = math.max(sy1, sy2)

Which then gives us our vector S <sx, sy, sz>

The tangent point can then be calculated by taking away Rn from our calculated S vector.

0
On

The center of the sphere lies on the line

$ C(t) = (s_x, 0, s_z) + t (0, 1, 0) = (s_x, t, s_z) $

We want the distance of a point on this line to be equal to the known given value of $R$ (the radius of the sphere). The formula for the signed distance is

$ d = \hat{n} \cdot ( C(t) - p_0) $

where $\hat{n}$ is the normalized normal vector of the plane.

Equating $d$ to $R$ results in the center of the sphere that on the side of the plane pointed to by $\hat{n}$, while setting $d$ to $(-R)$ gives the center of the sphere that is on the opposite side as the one pointed to by $\hat{n}$.

Now the tangency point is given by

$ T = C - d \hat{n} $

where $d$ can be $+R$ or $-R$, but $C$ is different between these two cases.

Now the height of the sphere is

$ h = C_z + R $