Jump to content
  • Advertisement
Sign in to follow this  
gbMike

How to smooth animation with matrixes

This topic is 2307 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

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

Share this post


Link to post
Share on other sites
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. Edited by rnlf

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
I'm using glm already. I can't imagine being without it. I haven't used it for quaternions yet though. Edited by gbMike

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!