chelder
chelder

Reputation: 3987

Getting position of a coordinate on the surface of a sphere

There is an object A. I want to create a object B. I'm trying to avoid that A covers B:

A hides B from camera

I have thinking how to achieve that. Take a look of the following drawing:

A does not hide B from camera

I have been thinking in a solution in a math - geometry way before searching for some Unity3D function to solve the problem. I have realise A and B would be on the same surface if the camera is within a sphere with radius c-A. So I guess the solution can be related to get a point at a B at a distance A-B from A on the surface of the sphere with radius c-A. Does it has sense? Any other idea? How to do it with maths and Unity?

Upvotes: 0

Views: 1334

Answers (1)

meowgoesthedog
meowgoesthedog

Reputation: 15035

What you are describing is a ring on the sphere's surface whose plane is perpendicular to the direction c-A.

This is fairly simple to compute. First we need to find 2 perpendicular vectors to c-A (we'll call this d):

  • Call the vector "c-A" = d, the radius of the sphere R = abs(d) and the distance "A-B" = D.
  • If d.X / R < 0.5, set a temporary vector w = X, else set w = Y.
  • Compute the first perpendicular vector u = normalize(cross(w, d)).
  • Compute the second one v = normalize(cross(d, u)).

The center point of this ring is given by c + d * cos(D / R). From here we can generate any point on the ring with

p(t) = c + d * cos(D / R) + u * cos(t) + v * sin(t)   ,   0 ≤ t ≤ 2π

UPDATE:

enter image description here

Upvotes: 3

Related Questions