

How can i use Matrix.CreateWorld?

Somewhere in my code, I wrote this:
Vector3 position = new Vector3(0.0f, 0.0f , 3000.0f); Vector3 forward = Vector3.Forward; Vector3 up = Vector3.Up; Matrix.CreateWorld(ref position, ref forward, ref up, out worldMatrix);
The resulting worldMatrix shows that
worldMatrix.Forward.ToString() => {0, 0, 1} worldMatrix.Up.ToString() => {0, 1, 0} worldMatrix.Right.ToString() => {1, 0, 0} worldMatrix.Translation.ToString() => {0, 0, 0}
Is the implementation right? I'm now very confused with the result because I expected the following result :
worldMatrix.Forward.ToString() => {0, 0, 1} worldMatrix.Up.ToString() => {0, 1, 0} worldMatrix.Right.ToString() => {1, 0, 0} worldMatrix.Translation.ToString() => {0, 0, 3000}
Is this due to my lack of Matrix understanding?




Re: How can i use Matrix.CreateWorld?

Why don't you use Quaternions, instead of Matrix.CreateWorld? I think it is much better. And you can use the interpolate functions so you don't need to keep a float to know the rotation angle at the moment.
So you do your world Matrix (which is necessary) like it: Matrix world=Matrix.CreateScale(myScale)*transforms[mesh.ParentBone.Index]*Matrix.CreateFromQuaternion(myQuaternion)*Matrix.CreateTranslation(myPosition);
(I'm not sure about the Matrix.CreateFromQuaternion function, but there is a Matrix function that does it).




Re: How can i use Matrix.CreateWorld?

Yeah, it doesn't do what you would think it would but I'm sure that MS did it this way for a good reason. I hope someone will explain it. Here is the reflected code that is used in that method: public static void CreateWorld(ref Vector3 position, ref Vector3 forward, ref Vector3 up, out Matrix result) { Vector3 vector = Vector3.Normalize(position  forward); Vector3 vector2 = Vector3.Normalize(Vector3.Cross(up, vector)); Vector3 vector3 = Vector3.Cross(vector, vector2); result.M11 = vector2.X; result.M12 = vector2.Y; result.M13 = vector2.Z; result.M21 = vector3.X; result.M22 = vector3.Y; result.M23 = vector3.Z; result.M31 = vector.X; result.M32 = vector.Y; result.M33 = vector.Z; result.M41 = 0f; result.M42 = 0f; result.M43 = 0f; result.M44 = 1f; result.M14 = Vector3.Dot(vector2, position); result.M24 = Vector3.Dot(vector3, position); result.M34 = Vector3.Dot(vector, position); }
You would think that M41,42,43 would have the position info.




 (21105)

XNA Team

Posts
11,763

Re: How can i use Matrix.CreateWorld?

You're right, it looks like the CreateWorld implementation is wrong. Oops, our bad!




Re: How can i use Matrix.CreateWorld?

Shawn Hargreaves: You're right, it looks like the CreateWorld implementation is wrong. Oops, our bad!
Sounds like it's time for a trip to Connect. ;)




Re: How can i use Matrix.CreateWorld?

No need, Shawn filed a bug. :)
Unfortunately we won't be able to fix it before we release.




Re: How can i use Matrix.CreateWorld?

Did you fix the bug in Matrix.CreateWorld implementation?
I dowloaded the new version of XNA Framework 2.0, but it seems that there is still the same problem in the above method.
So, I 've written the implementation myself as it is supposed to be. Please let me know if my implementation is worng or right.
public static void CreateWorld(ref Vector3 position, ref Vector3 forward, ref Vecctor3 up, out Matrix result) { Vector3 right; // Normalize forward vector Vector3.Normalize(ref forward, out forward); // Calculate right vector Vector3.Cross(ref forward, ref up, out right); Vector3.Normalize(ref right, out right); // Recalculate up vector Vector3.Cross(ref right, ref forward, out up); Vector3.Normalize(ref up, out up);
result.M11 = right.X; result.M12 = right.Y; result.M13 = right.Z; result.M21 = up.X; result.M22 = up.Y; result.M23 = up.Z; result.M31 = forward.X; result.M32 = forward.Y; result.M33 = forward.Z;
result.M41 = position.X; result.M42 = position.Y; result.M43 = position.Z; result.M14 = 0.0f; result.M24 = 0.0f; result.M34 = 0.0f; result.M44 = 1.0f; }
Thanks in advance.




 (21105)

XNA Team

Posts
11,763

Re: How can i use Matrix.CreateWorld?

palbul: Did you fix the bug in Matrix.CreateWorld implementation?
As Mitch said in the previous post: Mitch Walker: Unfortunately we won't be able to fix it before we release.




Re: How can i use Matrix.CreateWorld?

Did'nt you release the final XNA Frmamework 2.0?
I thought that the version, which was downloaded this morning, was the final of the 2.0.
Sorry for that.




 (16344)

Member
XNA Team

Posts
10,389

Re: How can i use Matrix.CreateWorld?

They did release the final XNA 2.0. But Mitch was saying it was too close to their release to fix it. I'm sure they'll have it solved when they either do 3.0 or a 2.0 Refresh (if they do one).




Re: How can i use Matrix.CreateWorld?

Sorry to necro this topic, but I think I'm still seeing this problem in 3.0 CTP.




 (16344)

Member
XNA Team

Posts
10,389

Re: How can i use Matrix.CreateWorld?

Indeed. I just used this code:
Matrix world;
Vector3 position = new Vector3(0.0f, 0.0f, 3000.0f);
Vector3 forward = Vector3.Forward;
Vector3 up = Vector3.Up;
Matrix.CreateWorld(ref position, ref forward, ref up, out world);
Console.WriteLine("Forward: {0}", world.Forward);
Console.WriteLine("Up: {0}", world.Up);
Console.WriteLine("Right: {0}", world.Right);
Console.WriteLine("Translation: {0}", world.Translation);
and the results it yielded:
Forward: {X:0 Y:0 Z:1}
Up: {X:0 Y:1 Z:0}
Right: {X:1 Y:0 Z:0}
Translation: {X:0 Y:0 Z:0}
So it would seem that the CreateWorld method still does not store the translation data inside of it like it should be.




 (21105)

XNA Team

Posts
11,763

Re: How can i use Matrix.CreateWorld?

I just checked the status of this bug: we fixed it in Milestone 2, which came after the CTP release.


