2010-12-03 34 views
2

我很愚蠢到3D,就像我的問題可能是:WPF AxisAngleRotation3D與XNA CreateFromAxisAngle

我做了WPF中「觀衆」節目呈現在屏幕上的東西,有什麼可以旋轉爲好。我用這種旋轉對我的口味是什麼在起作用:

代碼:WPF

Transform3DGroup tg = new Transform3DGroup(); 
tg.Children.Add(new ScaleTransform3D(sx, sy, 1)); 
tg.Children.Add(new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(1, 0, 0), rx))); 
tg.Children.Add(new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 1, 0), ry))); 
tg.Children.Add(new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 0, 1), rz))); 
tg.Children.Add(new TranslateTransform3D(x, y, z)); 
darabka.Transform = tg; 
TeljesModell.Children.Add(darabka); 

我決定把這個程序XNA,因爲這似乎是有點快,但我不能使它到工作。

試過這種方式(代碼,XNA):

Pozició = Matrix.CreateScale(sx, sy, 1f);       //méretezés 
Pozició *= Matrix.CreateFromAxisAngle(Vector3.UnitX, rx);   //forgatás 
Pozició *= Matrix.CreateFromAxisAngle(Vector3.UnitY, ry); 
Pozició *= Matrix.CreateFromAxisAngle(Vector3.UnitZ, rz); 
Pozició *= Matrix.CreateTranslation(x, y, z);      //középpont 

我試過甚至CreateFromYawPitchRoll,CreateRotationX(YZ)過,但沒有運氣的東西drawed在屏幕上總是不同的旋轉。所以我猜我會問這裏的大腦如果他們知道什麼數學我要把兩種技術之間。

在此先感謝。

編輯:我在其他論壇詢問過我的細節。我複製/他們在這裏貼過 的XNA代碼如下:

主要

... 
protected override void LoadContent() 
{ 
t3 = new Airplane(); //this is a "zone" object, having zone regions and zone objects in it. all of them together give the zone itself as a static object, where player walks in 
Kamera.Példány.Pozició = new Vector3(1362, 627, -757); //starting pozition in the zone - this is camera position so the player's starting pozition. (camera is FPS) 
... 
} 
... 
protected override void Update(GameTime gameTime) 
{ 
... 
Kameramozgatása();                //kamera mozgatását vezérlő billentyűk 
//this moves the FPS camera around 
} 
... 
protected override void Draw(GameTime gameTime) 
{ 
... 
ÁltalánosGrafikaiObjektumok.Effect.View = Kamera.Példány.Idenézünk;     //camera views this 
ÁltalánosGrafikaiObjektumok.Effect.Projection = projectionMatrix;     //... and camera views FROM is set in Kameramozgatása() 
... 
t3.Rajzolj(); //draw zone object 
... 
} 

區對象

constructor: set the effect and device to the same as main, set the Pozició (a matrix containing the current position and rotations) to origo 
... 
public virtual Alapmodel Inicializálás(float x, float y, float z, float rx, float ry, float rz, float sx, float sy) 
{ 
//this initializer starts when the coordinates are from a file and not set with Pozició = Matrix....., so we are to convert. this runs only one time. overrides set the vertex positions and textures 
VertexDeclaration = new VertexDeclaration(device, VertexPositionTexture.VertexElements); 
Pozició = Matrix.CreateScale(sx, sy, 1f);    //méretezés 
Pozició *= Matrix.CreateFromAxisAngle(Vector3.UnitX, rx);  //forgatás 
Pozició *= Matrix.CreateFromAxisAngle(Vector3.UnitY, ry); 
Pozició *= Matrix.CreateFromAxisAngle(Vector3.UnitZ, rz); 
Pozició *= Matrix.CreateTranslation(x, y, z);   //középpont 
//we set the starting position - nowhere else we do it, from here only the main loop can modify this,... could.. but does not. this is zone, and has only static objects - never moving 
} 
public void Rajzolj() 
{ 
//this draws the object (what can be a zone or a static object in it) 
foreach (EffectPass pass in effect.CurrentTechnique.Passes) 
{ 
    pass.Begin(); 
    foreach (Elem elem in Elemek) 
    { 
    //végigmegyünk az elemeken   
    effect.Texture = elem.Textúra; 

    //pozicionálás 
    effect.World = Pozició; //we take the initialized position 

    effect.CommitChanges(); //this is needed. if you do not use this, the textures will be screwed up 
    device.VertexDeclaration = VertexDeclaration; 
    device.DrawUserIndexedPrimitives<VertexPositionTexture>(PrimitiveType.TriangleList, Poziciók, 0, Poziciók.Length, elem.Indexek, 0, elem.Indexek.Length/3); 
    } 
    pass.End(); 
    } 
} 

就是這樣。我仍然認爲這是一些轉換,而不是繪圖代碼。沒有其他地方改變了立場。我猜想,矩陣就像WPF堆棧變換 - 我的問題是,我不知道兩者之間的數學轉換。 WPF代碼完美地工作,該區域中的模型顯示出良好。 XNA代碼不知何故,因爲Inicializálás()從x,y,z等轉換錯誤。在裏面。在這我需要幫助。

在此先感謝。

+0

忘記添加:XNA 3.1 - 但我猜在這種情況下,它不是一個biggy – 2010-12-03 11:21:25

回答

1

你也可以使用CreateWorld,但它沒有縮放比例,所以我決定一步步解決這個問題。首先是放到原點,然後縮放,然後旋轉縮放,然後放大縮放和旋轉。

如果您有學位,您將使用弧度而不是度數,像我一樣使用MathHelper.ToRadians()。

如果你來自WPF並且你的旋轉角度在那裏工作,但是這裏不是,試着將你的角度乘以360/512,然後再計算到弧度。你可以在評論中看到這個例子,你可以將它用於所有rx,ry和rz。

 Pozició = Matrix.CreateTranslation(Vector3.Zero);      //alappozíció az origó 
     if (sx != 1 || sy != 1) Pozició *= Matrix.CreateScale(sx, sy, 1f); //méretezés 
     if (rx != 0 || ry != 0 || rz != 0) Pozició *= Matrix.CreateFromYawPitchRoll(
           MathHelper.ToRadians(rx)      //WPFből: MathHelper.ToRadians(rx * 360f/512f) 
           , MathHelper.ToRadians(ry) 
           , MathHelper.ToRadians(rz) 
          ); 
     if (x != 0 || y != 0 || z != 0) Pozició *= Matrix.CreateTranslation(x, y, z);       //középpont 

如果你可以給更快的公式,然後* 360/512,或者看起來更好的東西,請評論。

1

我建議不要儲存您的方位角度的方式這樣...

但僅僅通過第一應用Z旋轉,然後X,則Y也有當您的問題可以在XNA版本來解決次

+0

試過,但沒有爲我工作。我不認爲這是旋轉的順序,因爲當我嘗試CreateFromYawPitchRoll時,所有旋轉一次,並沒有工作。這就是爲什麼我猜測它是一些數學運算,也許是XNA中的輸入和WPF中的輸入之間的一些轉換。 – 2010-12-03 13:26:30