say if I have an object at a certain position and i want to go to a certain position in another object by going around the object instead, how do i do that?
i mean situation like this is simple
i can just rotate the point around the pivot object center position (blue) and uses (0, 1, 0) as my axis as it only rotates around the Y axis.
my problem is say the original position is somewhere with x, y and z values, that means i need to rotate it in all axis. such as this drawing
Now i need to figure out the axis to rotate to, and i am confuse on how to compute this rotate axis to get the new position.
one idea i have is to get the difference between each axis, and use it as an angle and rotate them individualy using each axis.
angleX = targetX - origX
angleY = targetY - origY
angleZ = targetZ - origZ
new X position = rotate in axis (1,0,0) with angleX
new Y position = rotate in axis (0,1,0) with angleY
new Z position = rotate in axis (0,0,1) with angleZ
Anyone got better idea or something to help?
You can also orbit things around other things using quaternions. To just make the orbiting object jump to a target position:
- Create the “planet” to orbit around.
- Create a group and place it at the same position as the “planet”.
- Create the “moon” object and add it to the group - then move the “moon” within the group away from the origin point to create the orbit radius.
- Use Object3D.lookAt to point the group from point 2 towards the target position - effectively placing the “moon” at the target position.
Alternatively, for a smooth movement:
- Create the “planet”.
- Create a group and place it at the same position as the “planet”.
- Create and add the “moon” object to the group - then move the “moon” within the group away from the origin point to create the orbit radius.
- Create a second group / empty Object3D - an invisible mock - and place it at the same location as the “planet”.
- Use Object3D.lookAt to point the mock towards the target point / position you want the “moon” to be at next. That will set the quaternion of the mock to point at the target position.
- Use Quaternion.slerp to smoothly transition quaternion of the group from point 2 to the target position.
Example
4 Likes