How to snap a point in the normal 3d grid to a point on a sphere coordinate

Viewed 108

So I need to snap any floating point in 3D space to the nearest point on a sphere, the size of the sphere also needs to change as the point gets further from the origin, idk if that made sense or I'm just thinking about something incorrectly, but I made a picture of it in blender:

floating points snapped to a sphere

In the picture I have green dots which represent the possible places(there are more of them) that any point in space could snap to.. So how can I make any point in space snap to the closest green dot? And IK I could just loop through those points to check the closest one, but there are gonna be MANY of these points so its not a solution and so any point has to find its closest green dot without distance checks. An example of how this could work if I didn't need it to be on a sphere is this, So lets say I have point x, and I want to snap it to its closest green dot(here the green dot isn't a point on a sphere but actually a point on a "cartesian" grid), so to snap point x I just do round(x / gridSize) * gridSize; and that snaps x to its closest grid point, but in my case I need it on a sphere, so how can I convert that to snap to a spehre? IDK if I'm making any sesne.

1 Answers
  1. compute spherical coordinates a,b,r of point-center

    where a=<0,2*PI> is longitude, b=<-PI/2,+PI/2> is latitude and r>=0 is radius

  2. r=snapping_sphere_radius

  3. snap a angle a= 2*PI*round(a*n/(2*PI))/n

    where n is number snapping points on equator

  4. snap b angle b= PI*round(b*m/PI)/m

    where m is number of snapping circles (layers of sphere) usually m=n/2 ...

  5. recompute Cartesian position of point from spherical (a,b,r) and add center

Related