Rotating a 4x4 Matrix Causes Scaling Over Time

无人久伴 提交于 2020-01-06 03:26:11

问题


I am using glm::rotate to rotate a transformation matrix for a cube in a scene.

"cube->t = glm::rotate(cube->t, stepTime * 50.f, glm::vec3(0.f, 1.f, 0.f));" is called once per frame, where cube->t is the matrix in question.

The strange thing is that over the course of 20 minutes (or two minutes if I rotate by stepTime * 5000.f instead of stepTime * 50.f), the cube scales noticeably on the X and Z axes, where the scaling on those two axes is the same at all times (the height of the cube never changes, but the width and depth change by exactly the same amount). In the case of 5000.f, becoming smaller, and with the normal 50.f or 100.f, becoming larger.

I thought this might be a question of rounding error, but other than that I have no idea what could be causing it. Is this rounding error? Can I solve it by normalizing the matrix on a regular basis? Does glm have a function for normalizing matrices, or do I have to write one myself?


回答1:


If it is an incremental, cumulative, loss of precision: instead of in-place updating the cube, keep an original un-rotated cube and keep track of the total rotational angle. Then on each step create a cube from the original un-rotated cube, transformed by the total accumulated rotational angle. In this way your cube will not suffer from cumulative rounding problems.

Edit: In response to discussion with Miles, a more appropriate answer:

If you can separate the rotational and translation components of the transformation matrix, and the problem is primarily with the accumulation of rotation introducing shear or scaling, you can rectify the problem by extracting the angle from the rotational component and re-creating the rotation matrix with that angle but unit length.




回答2:


Yes, it is a rounding error. A reasonable solution is to store orientation as a single number and in every frame build the matrix from scratch.



来源:https://stackoverflow.com/questions/11269050/rotating-a-4x4-matrix-causes-scaling-over-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!