# Camera orientation "flickers" when following object using dot product

This topic is 3807 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Ok, heres the basics of my situation. I have a 4x4 matrix representing a camera's orientation and another matrix representing a freely rotating/moving object. My goal is to have the camera update and face the object after each frame of movement. Namely, I want the Z-vector of the camera matrix to point towards the position of object while having the matrix remain orthonormal. Currently, the method im trying looks something like this: I find the angle between the camera's current Z-vector and the new Z-vector using the arc-cos of the dot product. I then find the cross product of the current and new Z-vectors to get my axis of rotation. Finally, I rotate my camera's matrix along this axis by the previously found angle. To clarify, this is sorta what my code looks like:
void FaceObject(MATRIX & camera, MATRIX & object)
{
//get current camera z vector, and new vector facing object
VECTOR current_z_vector = camera.Get_Z_Vector()
VECTOR new_z_vector = object.Get_Position_Vector()- camera.Get_Position_Vector()
new_z_vector.Normalize()

//find angle
float dot_product = DotProduct(current_z_vector, new_z_vector)
float angle_between_vectors = acos(dot_product)

//rotate the camera matrix
VECTOR axis_of_rotation = CrossProduct(current_z_vector, new_z_vector)
camera.Rotate(angle_between_vectors,
axis_of_rotation.x, axis_of_rotation.y, axis_of_rotation.z)
}

As it is, my current method produces some extremely odd behavior and faces the object (what looks like) every other frame, and the rest of the time it points in any random direction. Is there something wrong with what Im trying to do mathematically or is the problem somewhere in my code?

1. 1
2. 2
Rutin
19
3. 3
khawk
15
4. 4
A4L
13
5. 5

• 13
• 26
• 10
• 11
• 44
• ### Forum Statistics

• Total Topics
633744
• Total Posts
3013657
×