[英]How to rotate a 3D model around its center?
我正在制作3D汽车游戏,但旋转有问题。 我想围绕自身旋转模型,但是当我移动时,它开始在世界范围内移动!
问题是:如何为模型移动提供中心?
我试图像这样更改代码:
effect.World = Matrix.CreateRotationZ(modelRotation) * effect.World = Matrix.CreateTranslation(position);
现在,它相对于模型向前移动,而不是相对于模型向前移动! 这是我的代码:
effect.World = Matrix.CreateTranslation(position) * Matrix.CreateRotationZ(modelRotation);
effect.View = camera.View;
effect.Projection = camera.Projection;
我有一些提示可帮助您入门:
在学校里v = AB y
表示: v = A (B y)
。 因此,在链接矩阵时,将首先应用B。
如果要合并矩阵A和B,则将它们相乘,就像C = AB
在Directx / XNA中,顺序相反。 为了合并矩阵B和A,可写var C = B * A;
为了避免出错,我采用了一种命名约定:每个矩阵(或变换)都称为AtoB
: WorldToView
, ModelToWorld
或ModelToRotatedModel
。
这提醒您第一个矩阵的输出必须与右矩阵的输入匹配:
var modelToView = modelToWorld * worldToView;
并不是:
var nowhereToNowhere = worldToView * modelToWorld;
这对我有很大帮助,希望它能帮助您解决矩阵问题。
PS我希望您的汽车模型的原点位于汽车的中心,否则它仍会奇怪地运动。
尝试切换以下值:
effect.World = Matrix.CreateTranslation(position) * Matrix.CreateRotationZ(modelRotation);
变成:
effect.World = Matrix.CreateRotationZ(modelRotation) * Matrix.CreateTranslation(position);
我遵循一个叫做ISROT
的简单缩写
身分识别
规模
回转
取向
翻译
您的工作从右到左,所以您始终以Translation
结尾。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.