- Home /
Rotate Vector3 array around a point
Hello everyone! I'am trying to rotate a Vector3 array around. The array represents vertices with positions, I can move them around in the scene using Handles.PositionHandle. Now what I want is to add rotation support. I tried to use plain Q * V and Matrix4x4 but they keep rotating around object's pivot instead of RotationHandle pivot. Here is the code:
Quaternion rotation = Handles.RotationHandle(Quaternion.identity, vertices[0]);
Vector3 delta = vertices[0] - Handles.PositionHandle(vertices[0], Quaternion.identity);
for (int i = 0; i < vertices.Length; i++)
{
vertices[i] = rotation * vertices[i] - delta;
}
Answer by Bunny83 · Jun 29, 2020 at 11:59 AM
Rotations always happens around (0,0,0) in the respective space your coordinates are in. If you want to rotate around a different point you first have to make that point the origin, rotate and then shift the coordinates back.
Keep in mind that if your "vertices" array actually contains vertices of a mesh, those are already given in local space of the object, You place a rotation and position handle at the local space position. However the handles expect world space positions. As long as your object is located at the world origin this would work. However moving the object elsewhere would not affect the position of your handles since the local space positions would not change.
Since you want to rotate around the vertex at index 0 you can simply change your for loop to:
for (int i = 0; i < vertices.Length; i++)
{
vertices[i] = vertices[0] + rotation * (vertices[i] - vertices[0]) - delta;
}
Sorry for not answering before. This works like a charm, thanks!
Your answer
Follow this Question
Related Questions
How would I rotate an object relative to another without parenting? 2 Answers
The bullet only moves in one direction and does not rotate with object. Code: 1 Answer
Calculate Vector3 for rotation or just change pivot; 0 Answers
Rotate a gameobject like car tire 2 Answers
Checking vector3 gameObject is about to move to (pathfinding collision detection) 1 Answer