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

Rotating a matrix

Last post 5/30/2008 4:11 PM by LiquidAsh. 6 replies.
  • 5/29/2008 7:51 PM

    Rotating a matrix

    ok so I have a 2D shape made from lines, I have a Vertex position color for each point on the shape, I want to rotate the shape, the problem is at the moment I am trying to rotate it using a rotation matrix but the thing just get stretched vertically, any help to how I can solve this?

     

    The lines of code that rotate it are as so...

    mOffsets[i].Position.X = (float)((Math.Cos(angle) * mOffsets[i].Position.X) + (-(Math.Sin(angle)) * mOffsets[i].Position.X));

    mOffsets[i].Position.Y = (float)((Math.Sin(angle) * mOffsets[i].Position.Y) + (Math.Cos(angle) * mOffsets[i].Position.Y));

     

    mOffsets is a VertexPositionColor array, I need to rotate these offsets around the origin...

     

    Any help is appreciated.

  • 5/29/2008 8:40 PM In reply to

    Re: Rotating a matrix

    Is this what you're looking for?

    Matrix rotation = Matrix.CreateRotationZ(angle);

  • 5/29/2008 9:17 PM In reply to

    Re: Rotating a matrix

    no thats not it, the problem is that the points I store are in VertexPositionColor format not in a matrix format so I cant perform matrix multiplication hence my messy peice of code wich is just mimicing a matrix multiplication, if this helps: the shape compresses in the following way: (not angles are in degrees, but are converted to radians in the actual program)

    0 - 45 squashes the shape on the horizontal plane

    45 - 90 uncompressesthe shape on the horizontal axis

    80 - 135 compresses the shape on the vertical axis

    135 - 180 uncompresses the shape on the horizontal axis the shape is now flipped upside down.

  • 5/29/2008 9:40 PM In reply to

    Re: Rotating a matrix

    The multiplying your position through the rotation matrix suggested above will do this, but if you want to write it out your way, try: 

     

    float newx =  (float)((Math.Cos(angle) * mOffsets[i].Position.X) + (-(Math.Sin(angle)) * mOffsets[i].Position.Y));

    float newy =  (float)((Math.Sin(angle) * mOffsets[i].Position.X) + (Math.Cos(angle) * mOffsets[i].Position.Y));

    mOffsets[i].Position.X = newx;

    mOffsets[i].Position.Y = newy;

     

  • 5/30/2008 9:04 AM In reply to

    Re: Rotating a matrix

    now I'm not sure what the coding difference is there but that fixed it,

     

    ty :D

  • 5/30/2008 3:46 PM In reply to

    Re: Rotating a matrix

    Just so you know, you can rotate individual points using Matrix rotations.

     

    /// <summary>  
    /// Translates a point around an origin  
    /// </summary>  
    /// <param name="point">Point that is going to be translated</param>  
    /// <param name="originPoint">Origin of rotation</param>  
    /// <param name="rotationAxis">Axis to rotate around, this Vector should be a unit vector (normalized)</param>  
    /// <param name="radiansToRotate">Radians to rotate</param>  
    /// <returns>Translated point</returns>  
    public Vector3 RotateAroundPoint(Vector3 point, Vector3 originPoint, Vector3 rotationAxis, float radiansToRotate)  
    {  
        Vector3 diffVect = point - originPoint;  
       
        Vector3 rotatedVect = Vector3.Transform(diffVect, Matrix.CreateFromAxisAngle(rotationAxis, radiansToRotate));  
       
        rotatedVect += originPoint;  
       
        return rotatedVect;  
    XNA QuickStart Engine (3D Game Engine for XNA) | My site
    "I'll be whatever I want to do!", Philip J. Fry
  • 5/30/2008 4:11 PM In reply to

    Re: Rotating a matrix

    Your rotated X position depends on mOffsets[i].Position.X and mOffsets[i].Position.Y, instead of mOffsets[i].Position.X and mOffsets[i].Position.X. Likewise for the Y coordinate.

     

Page 1 of 1 (7 items) Previous Next
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/
DCSIMG