**1**

# How to smooth animation with matrixes

Started by gbMike, Aug 23 2012 09:34 PM

6 replies to this topic

###
#1
Members - Reputation: **152**

Posted 23 August 2012 - 09:34 PM

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

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

###
#2
Members - Reputation: **1679**

Posted 24 August 2012 - 12:32 AM

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.

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.

**Edited by rnlf, 24 August 2012 - 03:02 AM.**

###
#3
Members - Reputation: **1679**

Posted 24 August 2012 - 03:08 AM

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.

###
#4
Members - Reputation: **152**

Posted 24 August 2012 - 04:12 AM

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.

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

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