简体   繁体   English

如何围绕其中心旋转3D模型?

[英]How to rotate a 3D model around its center?

I am making a 3d car game and I have a problem with rotation. 我正在制作3D汽车游戏,但旋转有问题。 I want to rotate a model around itself but when I move, it starts to move around the world ! 我想围绕自身旋转模型,但是当我移动时,它开始在世界范围内移动!

The question is: How do I make a center for the model to move around? 问题是:如何为模型移动提供中心?

I tried to change the code like this : 我试图像这样更改代码:

 effect.World = Matrix.CreateRotationZ(modelRotation) *  effect.World = Matrix.CreateTranslation(position); 

now instead of moving forward relative to the model, orientation it moves in a set direction ! 现在,它相对于模型向前移动,而不是相对于模型向前移动! & this is my code: 这是我的代码:

 effect.World = Matrix.CreateTranslation(position) * Matrix.CreateRotationZ(modelRotation); 
                effect.View = camera.View; 
                effect.Projection = camera.Projection;

I have a few tips to get you started: 我有一些提示可帮助您入门:

  1. Matrix multiplication order in DirectX/Xna is differrent than you learned in school. DirectX / Xna中的矩阵乘法顺序不同于您在学校学习的顺序。

In school v = AB y meant: v = A (B y) . 在学校里v = AB y表示: v = A (B y) So when chaining matrices, B is applied first. 因此,在链接矩阵时,将首先应用B。

If you want to combine matrix A and B, you multiply them like C = AB 如果要合并矩阵A和B,则将它们相乘,就像C = AB

In Directx/XNA, the order is reversed. 在Directx / XNA中,顺序相反。 To combine matrix B and A, you write var C = B * A; 为了合并矩阵B和A,可写var C = B * A;

To stop me from making mistakes, I adopt a naming convention: each matrix (or transform) is called AtoB : WorldToView , ModelToWorld , or ModelToRotatedModel . 为了避免出错,我采用了一种命名约定:每个矩阵(或变换)都称为AtoBWorldToViewModelToWorldModelToRotatedModel

This reminds you that the output of the first matrix must match the input of the right matrix : 这提醒您第一个矩阵的输出必须与右矩阵的输入匹配:

  var modelToView = modelToWorld * worldToView; 

and not: 并不是:

  var nowhereToNowhere = worldToView * modelToWorld; 

This helped me a lot, I hope it helps you sort out your matrix problems. 这对我有很大帮助,希望它能帮助您解决矩阵问题。

PS I hope the origin of your car model is in the center of the car, otherwise the it will still move around strangely. PS我希望您的汽车模型的原点位于汽车的中心,否则它仍会奇怪地运动。

Try switching these values around: 尝试切换以下值:

effect.World = Matrix.CreateTranslation(position) * Matrix.CreateRotationZ(modelRotation);

so it becomes: 变成:

effect.World = Matrix.CreateRotationZ(modelRotation) * Matrix.CreateTranslation(position);

I follow a simple acronym thats called ISROT 我遵循一个叫做ISROT的简单缩写

Identity 身分识别

Scale 规模

Rotation 回转

Orientation 取向

Translation 翻译

You work right to left, so you always end your statement with Translation . 您的工作从右到左,所以您始终以Translation结尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM