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

2 different draw calls with different results

Last post 5/1/2009 7:47 PM by DrSmooth. 5 replies.
  • 5/1/2009 3:43 PM

    2 different draw calls with different results

    ok so im trying to draw a sprite in the correct spot, but as soon as i specify  enough parameters to make it rotate it gets drawn in a completely different spot:
    bunny is the texture, dest is a rectangle rot is the rotation value...

    the sprite is still wrong even if the rot value is 0.

                spriteBatch.Draw(bunny, dest, Color.Yellow);   // this draws it correctly
                spriteBatch.Draw(                                       //this is the one i need but the image is always offset a little
                    bunny
                    , dest
                    , new Rectangle(0,0,bunny.Width,bunny.Height)
                    , Color.White
                    , rot
                    , new Vector2(bunny.Width/2f, bunny.Height/2f)
                    , SpriteEffects.None
                    , .5f);         

    is there something wierd going on here, i feel like ive done this before and not had this problem.


    thanks,
    ken
  • 5/1/2009 3:53 PM In reply to

    Re: 2 different draw calls with different results

    The first call assumes the sprite origin offset is Vector2.Zero (top-left corner), while in the second one you provide a non-zero offset (the center of the texture). If you change new Vector2(bunny.Width/2f, bunny.Height/2f) to Vector2.Zero the results should be identical.
  • 5/1/2009 4:06 PM In reply to

    Re: 2 different draw calls with different results

    well that worked when the rot was zero, but once it starts rotating, i need it to be around the center. and now it rotates around the top corner


    thanks,
    ken
  • 5/1/2009 4:31 PM In reply to

    Re: 2 different draw calls with different results

    You can't have it both ways. If you want to rotate around the center of the sprite, you need to account for that offset when positioning the sprite. You can modify your dest variable to move it up and to the left by the same amount as your center is offset.

    // get the offset amount
    Vector2 offset = new Vector2(bunny.Width/2f, bunny.Height/2f);

    // adjust the destination by that amount (assuming dest is a Rectangle)
    dest.Top -= offset.Y;
    dest.Left -= offset.X;

    // draw
    spriteBatch.Draw(
                    bunny
                    , dest
                    , new Rectangle(0,0,bunny.Width,bunny.Height)
                    , Color.White
                    , rot
                    , offset
                    , SpriteEffects.None
                    , .5f);       

    If dest is a Vector2 you can simply subtract offset from it instead.
  • 5/1/2009 5:46 PM In reply to

    Re: 2 different draw calls with different results

    Your different result is that the texture look a little "out of focus"?

    If yes, this is due to the bilinear or trilinear filtering of the texture done by the GPU, in this case you can add 1.0f / (float)bunny.Width and 1.0f / (float)bunny.Height to the Vector2 that you supply as origin.

    Please be more more specific.
    Marco
  • 5/1/2009 7:47 PM In reply to

    Re: 2 different draw calls with different results

    ok i got it, its weird i must have fixed this problem intuitively before because i never noticed it as an issue, for any onlookers here is what fixed it

     Vector2 off = new Vector2(bunny.Width, bunny.Height);
                dest.X += (int)off.X;
                dest.Y += (int)off.Y;
                spriteBatch.Draw(
                    bunny
                    , dest
                    , new Rectangle(0,0,bunny.Width,bunny.Height)
                    , Color.White
                    , rot
                    , new Vector2(bunny.Width/2, bunny.Height/2)
                    , SpriteEffects.None
                    , .5f);                

    thanks for the help
Page 1 of 1 (6 items) Previous Next