I want to calculate the orientation (quaternion) of the virtual 3d camera that is looking at some point in 3d space.
According to this explanation the quaternion be calculated from axis-angle:
qx = axis.x * sin(angle/2)
qy = axis.y * sin(angle/2)
qz = axis.z * sin(angle/2)
qw = cos(angle/2)
I calculate the axis angle this way (it's a pseudo code):
Point3D axis = normalize(target.position - camera.position); //direction
angle = 0;
But of course the quaternion will be [0, 0, 0, 1]
for any given target.position
and camera.position
(sin(0) = 0
). Which is wrong.
I guess that axis-angle to quaternion formula is ok.
Then maybe the axis angle should be calculated differently?
I thought about axis-angle as "the direction in which the object is pointing + the angle around that direction". Maybe it's more like "rotate the object around given direction by this angle"? But if so, axis-angle would not cover all possible 3d orientations.
How to calculate the axis angle having target.position
and camera.position
?
I work on left-handed coordinate system, but the solution should be quite analogical for right-handed one.
Edit, clarification based on @anon
request:
I'm not a mathematician (more a programmer) and some parts of quaternion math looks complicated to me. But I will try to explain what I've meant by "orientation of the camera".
In almost any 3d software (e.g. 3ds max, Blender) or game engine (e.g. Unity) you have two main components for each object: position (x,y,z
) and orientation (quaternion x,y,z,w
). To get the object in right place you rotate it first by orientation and then translate by position to place in the 3d scene before rendering.
By orientation of camera I mean that, common for 3d software, "object orientation" (on the illustration you can see that camera is rotated and translated from the center of the scene). Precisely speaking, in left-handed coordinate system, with y axis pointing "up".
Let's assume we remove the camera's target from an image for a moment. I should be able to restore the camera by: placing un-rotated camera object in [0,0,0]
, rotating it by my orientation (which I want to calculate) and then translating by position (which I already have).
[0, 0, 0, 1]
quaternion ("not rotated") for any given target and camera position (which is wrong). So either first or second formula must be wrong. Maybe I miss something in the concept of axis-angle. $\endgroup$