Jump to content
  • Advertisement
Sign in to follow this  
MossStone

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!