1
\$\begingroup\$

Currently, when I rotate the image using SpriteBatch.Draw() the image is rotated after it is scaled. Resulting in:

Rotation after scaling

Which is using the code

public override void Draw(GameTime gameTime) {
    sprite_batch.Draw(
        texture, 
        parent.position,
        new Rectangle(0, 0, (int) 25, (int) 25),
        Color.White,
        rotation,
        new Vector2(texture.Width/2, texture.Height/2),
        new Vector2(2, 2),
        SpriteEffects.None,
        0f);
}

However what I would like to see is more on the grounds of

Rotation before scaling

I've tried to directly edit the texture itself instead of rotating using spritebatch, only I have found that it was incredibly inefficient.

Edit 1:

I am currently looking around at RenderTarget2D and such. Possible solution to my problem.

\$\endgroup\$
  • \$\begingroup\$ Use code tags to mark source code. Yours is hardly readable. \$\endgroup\$ – Doctor Niklas Jul 1 '15 at 7:47
  • \$\begingroup\$ I thought I was using code tags? I can read it fine. \$\endgroup\$ – Aceeri Jul 1 '15 at 21:34
  • \$\begingroup\$ Rotate before you scale? You will need to use transform matrices for that. \$\endgroup\$ – Andrew Wilson Aug 7 '15 at 7:37
1
\$\begingroup\$

An easy way to do this would be to use transformation matrices. So you need to define a transformation matrix like so.

Matrix transform = Matrix.Identity;

Then to create the transformations do something like this but with your values in there.

transform = Matrix.CreateTranslation(X, Y, 0f) * Matrix.CreateRotationZ(rotationInRadians), Matrix.CreateScale(X, Y, 0f)

Now matrices are non-commutative meaning A * B != B * A and with transformation matrices changing the order you multiply them in will change which one happens first. For you I suggest Rotation * Scale * Translation or Rotation * Translation * Scale whichever you are looking for.

Then apply the transform in the spriteBatch. It will apply it for every sprite in that batch.

spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.None, RasterizerState.CullCounterClockwise, null, transform);

Lastly, note that in that overload for spriteBatch.Begin most of those are defaults except transform and samplerstate. A point clamp will prevent your objects from being sampled smoothly that way you can retain the rough edges you are looking for.

More information on transformation matrices can be shown by this picture. Although you don't need to fully understand them to utilize them. enter image description here

\$\endgroup\$
0
\$\begingroup\$

Using a rendertarget may be a solution if you only have that one sprite. If your game requires a lot of those rotated, scaled sprites, you might want to look into a pixelshader. You might calculate the occurence of a color within your intended pixel size (your spritepixels may be 3x3 screen pixels) and color the resulting screen pixels accordingly.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.