Jump to content

  • Log In with Google      Sign In   
  • Create Account

How to smooth animation with matrixes


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 gbMike   Members   -  Reputation: 152

Like
0Likes
Like

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

Sponsor:

#2 rnlf   Members   -  Reputation: 1123

Like
1Likes
Like

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.

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

my blog (German)


#3 rnlf   Members   -  Reputation: 1123

Like
2Likes
Like

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.

my blog (German)


#4 gbMike   Members   -  Reputation: 152

Like
0Likes
Like

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.

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 Posted Image

#5 larspensjo   Members   -  Reputation: 1540

Like
2Likes
Like

Posted 24 August 2012 - 05:12 AM

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.
Current project: Ephenation.
Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/

#6 rnlf   Members   -  Reputation: 1123

Like
0Likes
Like

Posted 24 August 2012 - 05:14 AM

I'd second that. I'm using glm, too, and it works very well...

my blog (German)


#7 gbMike   Members   -  Reputation: 152

Like
0Likes
Like

Posted 24 August 2012 - 10:38 AM

I'm using glm already. I can't imagine being without it. I haven't used it for quaternions yet though.

Edited by gbMike, 24 August 2012 - 11:43 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS