Find direction on a sphere?
I want objects to rotate around the point in space. Imagine how the vertices move when you rotate a sphere mesh. They rotate using a matrix. I tried to do the same way and it works but super slow. For 40 objects in FixedUpdated it destroys my FPS on mobile.
Is there a faster way to get a direction on a sphere?
Img: red vectors - point direction, green dir - sphereMovementDir
/// <summary>
///
/// </summary>
/// <param name="sphereMovementDir">direction the sphere moves in space</param>
/// <param name="sphereCenter">sphere center in world coordinates</param>
/// <param name="point">point on a sphere in world coordinates</param>
/// <returns></returns>
public Vector3 GetDirOnASphere(Vector3 sphereMovementDir, Vector3 sphereCenter, Vector3 point)
{
//transform sphereMovementDir to the rotation direction. Like a rotation of roll
Quaternion lookRot = Quaternion.Euler(sphereMovementDir.z, 0f, -sphereMovementDir.x);
//matrix that rotate smth using lookRot
Matrix4x4 m = Matrix4x4.Rotate(lookRot);
//calculate a new position for point after we applied rotation matrix
Vector3 newPointPos = sphereCenter + m.MultiplyPoint3x4(point - sphereCenter);
//get direction of movement on a point of a sphere
Vector3 forceDir = (newPointPos - point).normalized;
//return the dir so then I can apply it to the point...velocity
return forceDir;
}
screenshot-2020-11-21-102407.png
(28.0 kB)
Comment
Best Answer
Answer by Melnikovv · Nov 21, 2020 at 01:49 PM
I have found the mistake. I should compile a matrix out of the method and pass it as an argument. Because for each vertex rotation matrix is the same and no need to calculate it in the method
Your answer
![](https://koobas.hobune.stream/wayback/20220613020229im_/https://answers.unity.com/themes/thub/images/avi.jpg)