XNA Creators Club Online
Page 1 of 1 (5 items)
Sort Posts: Previous Next

Matrices Sux.

Last post 11/21/2009 9:32 PM by Ben Andersen. 4 replies.
  • 11/19/2009 11:24 AM

    Matrices Sux.

    Right now I am using matrices to rotate vectors, but they seem to not be working as I would understand them to.
    Here is some code.

                            Matrix transform = Matrix.CreateRotationZ((float)Math.Atan2(endToBall.Y, endToBall.X));
                            Vector2 newSpeed = Vector2.Transform(ballSpeed, Matrix.Invert(transform));

    If endToBall X = 0 and Y = -1 then the matrix = CreateRotationZ(Whatever negative half of pi is).
    In this example ballSpeed X = 0 and Y = 0.766666blah blah blah.
    Then I transform the speed (so I can do a bit of advanced reflection) and newSpeed X ends up to = negative ballSpeed.Y and newSpeed Y = -3.351213E-07. WTF!!?!?

    Can some explain what is happening?
  • 11/19/2009 11:54 AM In reply to

    Re: Matrices Sux.


    Firstly, I think you should be more polite.
    Secondly, your example could use easier numbers.
    Thirdly, you have not explained what bit is wrong.

    Teh1337Bix:
    formula
    Matrix transform = Matrix.CreateRotationZ((float)Math.Atan2(endToBall.Y, endToBall.X));
    Vector2 newSpeed = Vector2.Transform(ballSpeed, Matrix.Invert(transform));
    newSpeed X ends up to = negative ballSpeed.Y and newSpeed Y = -3.351213E-07.


    I have looked at that calculation and without spending too long checking, it looks correct.

    Atan2...  with your numbers gives a -90 deg rotation, as you have stated (-half PI.)
    You create a matrix that rotates -90 deg about the Z axis.

    You invert it, so that's plus 90 deg about the Z axis, (I think.)

    So you rotate a vector facing upwards (your ballSpeed), 90 deg about the plane that faces in to the screen, giving you a vector lying horizontal.  Y = 0 (ignoring floating point error) and X = what your Y value was (ignoring the sign.)

    The only thing I can see that could make a difference is if you swap the X and Y values in the Atan2 function, you'd get a completely different result.
    **

    Well on the way to creating a 3D First person controls shooter with Over the Shoulder view... Another few YEARS and it'll be done!

    http://games.discoverthat.co.uk/ - Skinning Sample Dude for Blender and XNA Parallel Spilt Shadow Maps plus other stuff...

    My game development blog - Well a few notes from time to time...
  • 11/19/2009 3:07 PM In reply to

    Re: Matrices Sux.

    Teh1337Bix:
                            Matrix transform = Matrix.CreateRotationZ((float)Math.Atan2(endToBall.Y, endToBall.X));
                            Vector2 newSpeed = Vector2.Transform(ballSpeed, Matrix.Invert(transform));

    If endToBall X = 0 and Y = -1 then the matrix = CreateRotationZ(Whatever negative half of pi is).
    In this example ballSpeed X = 0 and Y = 0.766666blah blah blah.
    Then I transform the speed (so I can do a bit of advanced reflection) and newSpeed X ends up to = negative ballSpeed.Y and newSpeed Y = -3.351213E-07. WTF!!?!?

    Can some explain what is happening?
    Given endToBall = {0, -1}, your matrix should be this (barring floating point errors):
    0 -1  0  0 
    1  0  0  0 
    0  0  1  0 
    0  0  0  1 

    With ballSpeed = {0, 0.7666...}, newSpeed should be {-0.7666..., 0}, barring floating point errors.

    Your result has newSpeed.Y = -3.351213E-07, which is -3.351213 x 10-7 = -0.0000003351213. This is the result of floating point errors, because computers are incapable of storing floating point values perfectly (there's an infinite number of values between 0 and 1, and the computer has finite memory). Floating point errors often result in very small numbers close to 0, as is the case in your code. The computer is not capable of representing PI perfectly, nor 0.7666... perfectly. Thus, your result won't be perfect.

    Your results are correct, you just shouldn't do something such as if (newSpeed.Y == 0), because as you've seen, it might be close-but-not-quite zero.
  • 11/20/2009 11:15 PM In reply to

    Re: Matrices Sux.

    I've worked what was wrong. I thought that a vector pointing straight up ( x = 0 y = -0) would be an angle of zero, so you'd get the same value, but an angle of 0 in atan2 is x = 1 y = 0. So I was  changing the wrong component in newSpeed. Anyway I have other problems, I'm trying to make my own Vector2.Reflect method but its failing, can some one help me please?
  • 11/21/2009 9:32 PM In reply to

    Re: Matrices Sux.

    The problem you are probably talking about is called gimble lock. The best alternative to matrices are using Quaternions, and then take the resulting quaternion, and transform it into a matrix for the purpose of making the transform matrix.

    The Quaternion is simply a way to represent a 3D angle, combining multiple quaternions to get a resulting angle doesn't result in gimble lock.

    Edit: jumped the gun.
Page 1 of 1 (5 items) Previous Next