开发者

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 qu开发者_如何学Goestion 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 = 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 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.

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜