简体   繁体   中英

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;

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 = AB 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 = AB

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 AtoB : WorldToView , ModelToWorld , 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.

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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