Sunday, January 9, 2011

Why isn't my world coordinate mapping back to the proper local coordinate?

I'm using XNA and am having a simple issue which I can't figure out. I'm just messing around and want to get a firm grasp of local/world space coordinates.

        Matrix rotateCharacter= Matrix.CreateRotationZ(MathHelper.ToRadians(45.0f));

        Matrix moveCharacter = Matrix.CreateTranslation(new Vector3(10,10,0));

        Matrix multMat =Matrix.Identity * rotateCharacter * moveCharacter ;

        Matrix WorldToB = Matrix.Invert(multMat);

        Vector2 worldCoord = new Vector2(10, 10);
        Vector2 worldToB = Vector2.Transform(worldCoord, WorldToB);

So all I'm doing here in my example is initially rotating my sprite's local axis around the world origin and then translating it out into world space. Thus, a world space coordinate of something like (10,10) would be 0,0 (my pixel's starting coordinate) in the local space (worldToB should be (0,0)). However, this is not occurring I guess as my inverted matrix is incorrect. Can anyone give me an answer of what I'm doing wrong exactly?

  • (worldToB should be (0,0)). However, this is not occurring

    But the vector2 worldToB does end up being 0,0 in your code above..., within the realm of floating point error anyways.

    The actual value of worldToB.X is -0.0000002843... on my computer which for all practical purposes is 0.0f.

    Ilya : Wow, never noticed the E when I hovered over the variable, thanks haha
    Steve H : You're welcome, glad to help. I've made that mistake many times. Hey, it looks like your understanding of matrices has grown alot in this last week. good going.
    From Steve H

0 comments:

Post a Comment