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'm having a bit of trouble getting my sprites in XNA to draw when near the edge of the screen.

Seemingly if you use SpriteBatch to draw then XNA will not draw it if for example (mPosition.X + mSpriteTexture.Width < 0) as it assumes it is offscreen.

However, it seems to make this decision before it applies a rotation. This rotation can mean that, even though (mPosition.X + mSpriteTexture.Width < 0), some of the sprite is still visible on screen.

EDIT: The rotation being a part of the problem is only a hypothesis but to clarify, as the sprite moves towards the edge of the screen at a steady rate (applying a small constant velocity) it will suddenly disappear when it is still partially (usually about half of the sprite) on screen.

My question is, is there a way to get it to draw further outside the viewport or temporarily disable sprite clipping during a certain spriteBatch.draw(...)?

sb.Draw(mSpriteTexture, mPosition,
                new Rectangle(0, 0, mSpriteTexture.Width, mSpriteTexture.Height),
                Color.White, Globals.VectorToAngle(mOrientation), new      Vector2(halfWidth, halfHeight), scale, SpriteEffects.None, 0);
share|improve this question
add comment

2 Answers 2

I don't think you assertion that the sprite is clipped is true. I've just tested the following code, which draws part of the sprite after rotation of 7/8's of a circle

   protected override void Draw(GameTime gameTime)
    {
        float rotation = (float)MathHelper.TwoPi * 7.0f / 8.0f;
        Vector2 mPosition = new Vector2(-(mSpriteTexture.Width + 1), 100);

        GraphicsDevice.Clear(Color.CornflowerBlue);
        spriteBatch.Begin();
        spriteBatch.Draw(mSpriteTexture, mPosition, null, Color.White, rotation, Vector2.Zero, 1, SpriteEffects.None, 0);
        spriteBatch.End();
        // TODO: Add your drawing code here

        base.Draw(gameTime);
    }

As you can see, mPosition.X + mSpriteTexture.Width < 0 is true in this case.

In your code, it looks like you are setting sprite origin to be;

new Vector2(mSpriteTexture.Width / 2, mSpriteTexture.Width/2)

Which has the effect of moving the sprite to the left of mPosition by mSpriteTexture.Width / 2, so maybe the sprite is a bit further 'offscreen' than you initially thought.

share|improve this answer
    
I'm with you :) –  Blau Nov 26 '12 at 16:31
    
I'm not certain if it's related to the rotation but to clarify, as the sprite moves towards the edge of the screen at a steady rate (applying a small constant velocity) it will suddenly disappear when it is still partially (usually about half of the sprite) on screen. –  Kevin B Nov 27 '12 at 12:06
    
@KevinB, I can't reproduce this behaviour. If I move my sprite (rotated by -45deg) towards the left, it just moves off the edge of the screen like one would expect. No sudden disappearances. –  Ken Nov 27 '12 at 14:04
    
@Ken I'm not sure if it will affect the result but you could try rotating it in the opposite direction. –  Kevin B Nov 27 '12 at 14:52
    
@KevinB, doesn't change anything –  Ken Nov 27 '12 at 14:59
add comment
up vote 0 down vote accepted

Turns out I was well off the mark, the mPosition variable I used was a part of a translation to do a wrap-around on-screen. When doing a wraparound with the sprites drawn at their centres it is necessary to include the halfWidth and halfHeight of the sprite when calculating wraparound. Final wraparound code looks as follows

            if (mTranslation.X > (Globals.Universe.X - halfWidth)) mTranslation.X -= Globals.Universe.X;
            else if (mTranslation.X < -halfWidth) mTranslation.X += Globals.Universe.X;
            if (mTranslation.Y > (Globals.Universe.Y - halfHeight)) mTranslation.Y -= Globals.Universe.Y;
            else if (mTranslation.Y < -halfHeight) mTranslation.Y += Globals.Universe.Y;
            mPosition = mTranslation;
share|improve this answer
add comment

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.