- Home /
Question by
artak10t · Aug 23, 2018 at 08:50 AM ·
programmingmathgeometryprojectionalgebra
How to project a point on to a sphere?
If i have a point (x,y,z) how to project it on to a sphere(x0,y0,z0,radius) (on its surface). My input will be the coordinates of point and sphere. The output should be the coordinates of the projected point on sphere.
I found an answer! :D
public Vector3 ProjectOnSphere(Vector3 OriginalPosition, Vector3 SpherePosition, float radius)
{
Vector3 p = OriginalPosition - SpherePosition;
float pLenght = Mathf.Sqrt(p.x * p.x + p.y * p.y + p.z * p.z);
Vector3 q = (radius / Mathf.Abs(pLenght)) * p;
Vector3 pointOnSphere = q + cameraPosition;
return pointOnSphere;
}
Comment
Answer by elenzil · Aug 23, 2018 at 07:22 PM
another answer would be
Vector3 projectedPoint = (pointPos - spherePos).normalized * sphereRadius + spherePos;
.. actually, now that i see that 'cameraPosition' should have been 'OriginalPosition', these are the same.