如何圍繞其中心旋轉 3D 模型? (How to rotate a 3D model around its center?)


問題描述

如何圍繞其中心旋轉 3D 模型? (How to rotate a 3D model around its center?)

I am making a 3d car game and I have a problem with rotation. 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;

參考解法

方法 1:

I have a few tips to get you started:

  1. Matrix multiplication order in DirectX/Xna is differrent than you learned in school. 

In school v = A B y meant: v = A (B y). So when chaining matrices, B is applied first. 

If you want to combine matrix A and B, you multiply them like C = A B

In Directx/XNA, the order is reversed. To combine matrix B and A, you write var C = B * A;

To stop me from making mistakes, I adopt a naming convention: each matrix (or transform) is called AtoBWorldToViewModelToWorld, or ModelToRotatedModel.

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.

P.S.  I hope the origin of your car model is in the center of the car, otherwise the it will still move around strangely. 

方法 2:

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

Identity

Scale

Rotation

Orientation

Translation

You work right to left, so you always end your statement with Translation.

(by hakamuser180326Neil Knight)

參考文件

  1. How to rotate a 3D model around its center? (CC BY-SA 3.0/4.0)

#3d #xna #C#






相關問題

OpenGL ES 或 Direct3D 中的表面究竟是什麼? (What exactly is a surface in OpenGL ES or Direct3D?)

為什麼我們需要維護自己的矩陣來轉換遊戲對象? (Why we need to maintain our own matrices to transform Game objects?)

將兩個 4x4 矩陣連接在一起? (Concat two 4x4 matrices together?)

使用 matplotlib 保存散點圖動畫 (Saving scatterplot animations with matplotlib)

如何在 Matlab 中為這個 3D 繪圖設置動畫? (How to animate this 3D plot in Matlab?)

用java加載最簡單的靜態模型格式? (easiest static model format to load with java?)

在 GLSL 中混合多個紋理 (Blending multiple textures in GLSL)

在 3D 中可視化 10,000 個陰影框的最簡單方法 (Easiest way to visualize 10,000 shaded boxes in 3D)

如何圍繞其中心旋轉 3D 模型? (How to rotate a 3D model around its center?)

如何計算一個長方體與其相鄰長方體的接觸面積 (How to calculate area of contact of one rectangular cuboid to its adjacent cuboids)

在多視圖幾何中最接近另一個 3D 點的線上查找 3D 點? (To find 3D point on a line closest to another 3D point in Multi-view Geometry?)

GLUT 環面與相機相撞 (GLUT torus colliding with camera)







留言討論