Search code examples
xnarotation

XNA rotation not working correctly


So, I'm trying to rotate 5 vectors2s to get target(Vector2) as a normal for them. But when I rotate it isn't rotating properly. When target is pointing up and verticies' normal right or left(default: 0,-1 0,0 0,1) I have to rotate them 90 degrees but when I do result isn't -1,0 0,0 1,0 what it should be. Instead it is something like: -1,vs 0,0 1,-vs vs=very small number. Why is this? Is there a way to correct this? Code:

Vector2 target = new Vector2(0, 1); //Create target

Vector2[] vecs = new Vector2[3] { new Vector2(0, -1), Vector2.Zero, new Vector2(0, 1) }; //Create verticies to be rotated

Matrix matrix = Matrix.CreateRotationZ(MathHelper.ToRadians(90)); //Should be: (float)Math.Atan2(target.Y, target.X) instead of 90 but wanted to simplify this

Vector2.Transform(vecs, ref matrix, vecs); // Rotate

I even tried rotating with 360 degrees for full turn but that didn't give me the starting vectors, which is weird to me.


Solution

  • Why is this?

    Its because of the precision of floating point variables.

    Is there a way to correct this?

    I dont know a way, but I would just ignore it. Because it is nearly equal to zero.

    Protip

    With the given vector (x,y), (-y,x) or (y,-x) are the normals of this vector.