1

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.

Spektre
  • 49,595
  • 11
  • 110
  • 380
Shiv-iwnl
  • 13
  • 5

1 Answers1

0
  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

Spektre
  • 49,595
  • 11
  • 110
  • 380
  • Thanks for the reply but I don't understand, but I've come up with something else. I can just take point x and snap it to the grid by dividing it by size and rounding it, than multiplying it by size, that will snap it to the nearest grid cell, than I can take that new point and normalize it, than multiply the normalized vector by a value that defines the radius of a sphere. – Shiv-iwnl Feb 11 '22 at 15:44
  • @Shiv-iwnl so you have concentric spheres as grid (not just single one)? in that case `r=round(r*grid_size)/grid_size;` ... what you are doing will do the same however you will not align to grid points on sphere surface only to its surface ... what exactly you do not understand? ... the [conversion between Cartesian and Spherical](https://en.wikipedia.org/wiki/Spherical_coordinate_system#Coordinate_system_conversions) ? – Spektre Feb 11 '22 at 17:19
  • 1 and 2 seem very algebraic, how do I put that into code? I don't understand how to get a or b. – Shiv-iwnl Feb 11 '22 at 21:39
  • @Shiv-iwnl You just write the equation into code ... not coding in C# but in C/C++ (which is very similar) it would be for example like this: `r=sqrt(x*x + y*y +z*z); a=atan2(y,x); b=asin(z/r);` where `x,y,z` is your `point - center_of_grid` then you align `a,b,r` using the `round` function and then convert back to cartesian `x=r*cos(a)*sin(b); y=r*sin(a)*sin(b); z=r*cos(b);` then just add the center of grid back and you got your snapped point position ... – Spektre Feb 11 '22 at 22:45
  • I tried that method today but it didn't seem to work, but I've thought of another way of doing it using a "derivative" type of function, but I need some help with it as well. – Shiv-iwnl Feb 12 '22 at 00:16
  • @Spktre It seems pretty easy to turn that functionality into a "derivative", all I gotta do is give the function an index, and number, and index < number must be true. But there is a problem, this will only work for 2d and after even after we turn it to 3D there will be pinched position at the top. – Shiv-iwnl Feb 12 '22 at 00:49
  • @Spktre https://stackoverflow.com/questions/50606756/creating-a-2d-circular-mesh-in-unity Take a look at the generating vertices section, It seems pretty easy to turn that functionality into a "derivative", all I gotta do is give the function i, and n, and it'll give me a point in a circle. But there is a problem, this is for a circle and even after we turn it to a sphere there will be pinched points at the top, because that's how this type of sphere works, so I need to derive an Icosphere's vertices. To derive their positions I can use round(pointX) as the i parameter, b idk how to start. – Shiv-iwnl Feb 12 '22 at 01:00
  • @Shiv-iwnl if you want uniformly distributed snap points on sphere then you need to use either [spiral approach](https://stackoverflow.com/a/57240140/2521214) or [mesh subdivision](https://stackoverflow.com/a/29139125/2521214) or [adaptive number of points per sphere layer based on radius](https://stackoverflow.com/a/25031737/2521214) so chose approach first ... however turn these into analytic equation will not be easy (except for the last one) – Spektre Feb 12 '22 at 08:05
  • @Shiv-iwnl Also you should better describe the snap point topology ... so will be the concentric spheres be displaced with the same distance? will they have the same number of points? how are they placed (same spherical angles?) ... btw. I see absolutely no point on using derivates what for? point has no tangent and sphere surface point tangent is a plane unless some align direction is present and they will not help you with the snapping anyway... – Spektre Feb 12 '22 at 08:10