1
\$\begingroup\$

I'm using the full Spritebatch.Draw function to draw my explosion sprite since I also need rotation. I am using the Scale value to resize my texture, but I can't get it to be accurate.

I want to be able to set the textures Size to a specific pixel size, width/height are exactly the same. Say the texture is 128x128, and I want to resize it to be 23x23.

Heres my current code:

  Vector2 Scale = new Vector2((float)sprite.Area.Width / (float)sprite.Texture.Width, (float)sprite.Area.Height / (float)sprite.Texture.Height);
            spriteBatch.Draw(sprite.Texture, sprite.Position, null, sprite.Color * sprite.Alpha, (float)Math.Atan2(sprite.Angle.Y, sprite.Angle.X), new Vector2(sprite.Area.Width / 2, sprite.Area.Height / 2), Scale, SpriteEffects.None, 0f);

I have the Area set to be the size I want it at, however the Draw function seems to not be drawing it to the exact size.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ I don't understand yet. What do you want and how does the output differ from that? Since we're talking about sprites here, a screenshot would be really useful. \$\endgroup\$
    – Anko
    Commented May 20, 2013 at 10:15

2 Answers 2

2
\$\begingroup\$

Since it looks like you are using the Scale parameter, you will need to calculate the correct scale value based on the original and target size. Since the Scale parameter is a float, and not a pixel size, you'll need to calculate the percentage your target is of your source.

If your resource is 128x128, and you draw at scale 1.0f, your output will be 128x128.

If your resource is 128x128, and you draw at scale 0.5f, your output will be 64x64.

If your resource is 128x128, and you draw at scale 0.179f, your output will be 23x23 (rounding may make this slightly different, adjust scale accordingly.

The formula for uniform scaling as you are asking, would be Scale = TargetSize/SourceSize.

For additional information, and alternate approaches (i.e. using a destination rectangle instead of a scaling factor) see this MSDN article.

\$\endgroup\$
1
\$\begingroup\$

In the destination variable of Draw() use a new Rectangle() so that you can specify the not just the position of the draw but the size also.

Rectangles properties

Height  Specifies the height of the rectangle.
Width   Specifies the width of the rectangle.
X       Specifies the x-coordinate of the rectangle.
Y       Specifies the y-coordinate of the rectangle. 

Use X,Y for the location and Height,Width for the size.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .