# Problem Scaling Tiled Sprites (XNA)

This topic is 3494 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm trying to implement something similar to the tiled sprites tutorial found at http://creators.xna.com/en-US/sample/tiledsprites. The tutorial uses the centre of the screen as the draw position of each sprite, and then has the sprite position as the offset, in order to get rotation around the centre of the screen. The trouble is, when I do this in my game, it rotates properly, but causes the scaling to be a bit off (gaps start appearing between sprites when zooming out). Any help with this would be very much appreciated, as I think the answer is probably pretty obvious but I can't see it for the life of me. My code:
public void UpdateCamera(Vector2 position, float scale, float rotation)
{
_offset = new Vector2(
position.X % _sourceRectangle.Width,
position.Y % _sourceRectangle.Height);
_scale = scale;
_rotation = rotation;
}

public void Draw(SpriteBatch spriteBatch)
{
Vector2 position = -_offset * _scale;
position -= _screenCentre;
int width = Conver.ToInt16(Math.Ceiling(_width / _scale));
int height =  Conver.ToInt16(Math.Ceiling(_height / _scale));

float sourceRectangleWidth = _sourceRectangle.Width * _scale;
float sourceRectangleHeight = _sourceRectangle.Height * _scale;

for (int x = -width; x <= width; x++)
{
for (int y = -height; y <= height; y++)
{
Vector2 drawPosition = new Vector2(
position.Y + (sourceRectangleHeight * y) );
spriteBatch.Draw(_texture, _screenCentre , _sourceRectangle, Color.White, _rotation, -drawPosition, _scale, SpriteEffects.None, 1);
}
}
}


##### Share on other sites
Fixed it, don't worry :)

##### Share on other sites
Well.. you gonna share, so us n00bs can learn??? :)

##### Share on other sites
Haha, since you asked so nicely :) The problem was with the fact that the offset scales along with the texture, which in retrospect was kinda obvious. On the plus side it simplified it quite a lot.

        public void UpdateCamera(Vector2 position, float scale, float rotation)        {            _offset = new Vector2(                position.X % _sourceRectangle.Width,                position.Y % _sourceRectangle.Height);            _scale = scale;            _rotation = rotation;        }        public void Draw(SpriteBatch spriteBatch)        {            int width = Convert.ToInt16(Math.Ceiling(_width / _scale));            int height = Convert.ToInt16(Math.Ceiling(_height / _scale));             for (int x = -width; x <= width; x++)            {                for (int y = -height; y <= height; y++)                {                    Vector2 offset = _offset + new Vector2(_sourceRectangle.Width * x, _sourceRectangle.Height * y);                    spriteBatch.Draw(_texture, _screenCentre, _sourceRectangle, Color.White, _rotation, offset, _scale, SpriteEffects.None, 1);                }            }        }

##### Share on other sites
Ah, gotcha! That makes sense, now that you point it out.

And thank you very much for sharing. :D

1. 1
2. 2
3. 3
Rutin
14
4. 4
5. 5

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633672
• Total Posts
3013272
×