Jump to content
  • Advertisement
Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

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()

//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)
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?

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!