I want to assign values (in this case, vector3s) to points on a unit sphere/directions. I want to then be able to pass in a direction/point on the unit sphere, and get back the associated value.
I dont mean like a dictionary. Any direction vector passed in should return a value. It needs to interpolate between the known values closest to it.
How can I do this? The values associated with the directions are force vectors, so they need to be slerped together instead of lerped, but I dont know how to do that with 3 vectors. Im assuming the points I would need to blend would be the 3 points enclosing the given point.
Like most things in programming, this boils down to having a collection of values, and enumerating them until you find your desired result. This might involve enumerating the whole or part of the collection.
Sometimes you may have the option of calculating the exact or close index in some form, but not always the option.
I’m not entirely confident that I fully understand your question correctly, but are you asking about a calculation similar to blending the normals of three vertices to determine the normal at a specific point on a polygon of a spherical mesh?
If so, barycentric coordinates can be useful. You can look up “Barycentric Coordinates on a Triangle” to find the calculation method.
When blending vectors, consider direction and magnitude separately.
First, you can use the three weights obtained from the barycentric coordinate calculation as coefficients to blend the three vertex normals, which will give you the direction.
Next, applying the same blending process to the magnitudes of the three vertex normals will give you the final length.
Im trying to create an aerodynamic model where i procompute the force generated at specific local velocity directions, and then when the velocity is some given direction , it interpolates the force vector based on the precomputed directions.
I tried to phrase it in a more generic way but thats what im trying to do.
I want to be able to calculate even just a single direction and have any direction return that value. If 2 opposite directions are calculated it should blend between the 2 etc. I want to be able to scale up the “resolution” as much as I want and have it accurately interpolate what the force should be given any local velocity direction. I want it to work regardless of the distribution of these points as well. For example, I may want to calculate every 2.4 degrees change of angle of attack when the rigidbody is moving forward, but only every 15 degrees after 20 degrees of rotation etc.
I did think about barrycentric coordinates, but because a triangle is flat, I thought it would be an accurate approach. It also wouldn’t work with a single data point etc.
I see, I have a general understanding of what you want to achieve.
For this purpose, it would be best to interpolate using two elements of Euler angles (in other words, the angles in spherical coordinates).
For example, by setting the positive Z-axis as the reference direction, you can precompute values by varying the two rotation angles around the X and Y axes.
These values can then be stored in a two-dimensional array, where the indices correspond to the rotation angles around the X and Y axes.
When blending, you first convert the input vector into Euler angles and retrieve the four surrounding points A,B,C,D from the two-dimensional array. Let t0 and t1 be the interpolation parameters for the horizontal and vertical directions, respectively. The final result can be obtained using the formula:
Slerp(Slerp(A, B, t0), Slerp(C, D, t0), t1)
This method tends to cause sample points to cluster inefficiently near the Y-axis.
However, this can be easily avoided by preparing an additional dataset with the positive Y-axis as the reference direction and switching to this dataset when searching for points near the Y-axis.
What you need to first define is how the blending operator works. Even on a 2D plane, it’s a bit ambiguous how to blend between marked points evenly. For example, you can use barycentric coordinates of the nearest three enclosing points, but that won’t necessarily be smooth. Imagine just outside the triangle, there is a tiny sliver triangle with a point with +infinity. That would cause a very steep change in the output, even if the point moved only a very small amount.
So there are several ways to blend, depending on your use case. If you’re in aerodynamics, you might want to think about this more as an minimization problem. In the most advanced cases you could look into things like heat diffusion (energy diffusion) which takes the surface shape into account (here it’s just a sphere so not a big deal). The reason for this is you can think of blending as imagining spots of (differently colored) heat on a surface and seeing the colored equilibrium that eventually forms.
And what happens when both spots are on opposite ends of the sphere? It should probably pick a blended point on the equator, but which point? There’s an infinite number of valid points on the equator.
Hopefully that also starts to explain why this is not so simple, even on a sphere. There’s no ‘simple solution’.
If your goal is to use this for efficient precomputing, I’d recommend a different approach, honestly. Or use Arithmetica’s solution of interpolating in the 2D space of Euler Angles, which will have issues, but would be fixed by simply pre-calculating a lot of points to counteract any stretching.