簡體   English   中英

如何圍繞其中心旋轉3D模型?

[英]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;

我有一些提示可幫助您入門:

  1. DirectX / Xna中的矩陣乘法順序不同於您在學校學習的順序。

在學校里v = AB y表示: v = A (B y) 因此,在鏈接矩陣時,將首先應用B。

如果要合並矩陣A和B,則將它們相乘,就像C = AB

在Directx / XNA中,順序相反。 為了合並矩陣B和A,可寫var C = B * A;

為了避免出錯,我采用了一種命名約定:每個矩陣(或變換)都稱為AtoBWorldToViewModelToWorldModelToRotatedModel

這提醒您第一個矩陣的輸出必須與右矩陣的輸入匹配:

  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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM