Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I use a 1x1 pixel 2d texture as source for SpriteBatch.Draw. The texture is scaled and rotated around an origin. The left image is only scaling. The right one scaling + rotation around the origin at the bottom center. I need the right image as result. i.e. scaled to 5x10 and rotated around 2.5x10

enter image description here

The problem is with the 1x1 texture the draw starts to act strange if the origin parameter is outside the source texture. And as long as the origin is limited to a max of 1x1 its not possible to rotate around the bottom center of the scaled texture.

Why does the origin behave strangely if it is outside the source texture? With strange I mean the texture moves much more then the origin value set. i.e. if its origin x=10 the texture moves much more then 10 pixels.

share|improve this question
    
Are you scaling first, then rotating? It might be just a case of changing the order in the transform. Change the order, and see if it fixes itself –  Jason Coombes Feb 15 '13 at 9:20

3 Answers 3

I think you are misinterpretting the result of setting the origin to (0.5f, 1). I did the same thing, and I got the expected results. Here's a modified version of the code I ran at this answer:

Color[] colors = new Color[] { Color.White };
texture = new Texture2D(GraphicsDevice, 1, 1);
texture.SetData<Color>(colors);

spriteBatch.Draw(texture, new Rectangle(100, 50, 100, 100), null, Color.Green);
spriteBatch.Draw(texture, new Rectangle(100, 225, 100, 100), null, Color.Yellow, 0, new Vector2(0, 0), SpriteEffects.None, 0);
spriteBatch.Draw(texture, new Rectangle(100, 400, 100, 100), null, Color.Pink, 0, new Vector2(0.5f, 1f), SpriteEffects.None, 0);

spriteBatch.Draw(texture, new Rectangle(400, 50, 100, 100), null, Color.Red, MathHelper.PiOver4, Vector2.Zero, SpriteEffects.None, 0);
spriteBatch.Draw(texture, new Rectangle(400, 225, 100, 100), null, Color.Orange, MathHelper.PiOver4, new Vector2(0, 0), SpriteEffects.None, 0);
spriteBatch.Draw(texture, new Rectangle(400, 400, 100, 100), null, Color.Blue, MathHelper.PiOver4, new Vector2(0.5f, 1f), SpriteEffects.None, 0);

And as you can see, the blue square rotates around the bottom-center, exactly as intended. The other squares rotate around (0,0).

enter image description here

share|improve this answer

I think you should divide origin by the scale...

because spritebatch apply origin translation before the scale operation, so you have to invert the scale operation to avoid origin being scaled

share|improve this answer
2  
This should probably be a comment. –  Vaughan Hilts Apr 15 '13 at 18:52
    
you are right... but was the solution... I've added more explanations –  Blau Apr 15 '13 at 22:44

Set origin in relation to source rectangle. If your rectangle is 0:0:1:1, set 0.5:1 origin (for bottom center).

share|improve this answer
    
Yes that's how I would expect it to work. But when doing that it rotates the 5x10 texture around the 0.5:1 origin (top left). The order seems to be wrong. Xna scales first and rotates afterwards with the unscaled origin. –  user26297 Feb 14 '13 at 13:53
    
@user26297 This method is correct. I demonstrated it in my screenshot. –  Seth Battin May 16 '13 at 2:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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