How to smooth animation with matrixes

Started by
5 comments, last by gbMike 11 years, 8 months ago
I'm attempting to add a slight smoothing to the 'camera' in my 3d engine. It locks onto the player, and follows the model, exactly. However, I would like it to, instead animate slightly, as the camera catches up to the player, just so it doesn't look so stiff. Here was my semi successful fix:

[source lang="cpp"]
mat4 targetMatrix = glm::inverse( currentCamera->camTarget->getCoreTransform().getMatrix() );

//camera matrix is the camera's offset
targetMatrix = currentCamera->camMatrix * targetMatrix;

//get the difference between the new target matrix, and the current view matrix
mat4 difMatrix = targetMatrix - viewMatrix;

//only move a percentage of the total distance.
//this will automatically ease out the movement
difMatrix /= 4;
viewMatrix += difMatrix;

iGLEngine->viewMatrix->setMatrix( viewMatrix );
[/source]

The problem is, when the object rotates very quickly the scene starts to warp and stretch. The amount of stretching is directly connected to the rotation speed. Anyone know why? Or, even better, another way to animate a transition from one matrix (the current view matrix) to another (the target view matrix) ?
Advertisement
You cannot interpolate between transformation matrices without adding such funny effects because this will stop the resulting coordinate system's from being orthonormal, effectively resulting in your scene to be skewed, streched and more.

You might consider splitting your camera motion into its position and rotation. If you represent your rotation as a quaternion, you may use http://en.wikipedia.org/wiki/Slerp to interpolate nicely between your cameras' quaternions. After interpolation of rotation and translation you construct a new combined matrix by converting the quaternion to a rotation matrix and multiplying it with your translation matrix (be advised though, that there are some nice tricks to speed up multiplying a (pure) rotation matrix with a (pure) translation matrix).

Hope that helps.
By the way, your simple approach to smoothing out the camera by just going fixed quarter-of-the-total-distance steps is depending on your framerate. What you're doing every frame is new_position = interpolate(old_position, target_position, 0.25). What you might actually want to do is new_position = interpolate(old_position, target_position, 0.25*time_since_last_frame). This way your camera will always move with the same speed, and not faster wehen the framerate is high and slower when the framerate is low.
Hm. I had considered pulling the position and rotation values from the matrix, then calculating it, but I would have thought there would be some way to do it without needing to convert the data twice. But then again, I'm still a bit lacking in my matrix math. I'll let you know how the update goes.


By the way, your simple approach to smoothing out the camera by just going fixed quarter-of-the-total-distance steps is depending on your framerate.


True. The updateViewMatrix function (that contains the above code) takes in a frameTime variable, but I had yet to integrate it here, since the current code still needs fixed. Definitely good advice smile.png
I would recommend having a look at glm. It supports fast inline matrix math, including quaternions. It is designed to look like GLSL programming as close as possible, but in a C++ environment.
[size=2]Current project: Ephenation.
[size=2]Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/
I'd second that. I'm using glm, too, and it works very well...
I'm using glm already. I can't imagine being without it. I haven't used it for quaternions yet though.

This topic is closed to new replies.

Advertisement