Edit
Finally managed to get this to work, after two separate questions! Here's a video demonstration:
http://www.youtube.com/watch?v=nZ7e0jegvs0
Will write an article about it later when I have a chance since there's a lot of little tricks involved. Thanks to everyone who helped figure this out. Meanwhile, the solution for the rendering part is described on this question, while for the physics part of the solution, check this other question.
Disclaimer
I'm looking for intelligent solutions that encapsulate this behavior in a way that is transparent to the user. Don't simply answer "draw the sprite four times". For instance, something that could be packed up in a class like:
class Sprite
{
Vector2 Position;
float Rotation;
float Scale;
Texture2D Texture;
Rectangle? WrapRegion;
void Draw(SpriteBatch spriteBatch);
bool IsColliding(Sprite other);
}
By the way, there's no need to post any code, just a description of how to solve each of the problems will do.
Problem
1 - By wrapped I mean that when they leave the borders of that region, they continuously appear on the opposite side, i.e:
So simply changing the sprite's position clearly is not enough. The sprite might have to be drawn either 2 or 4 times depending on its position in relation to the region.
And despite needing to draw it several times, I should still be able to do, for instance:
sprite.Position += Vector2.One; // Every frame
And the sprite's Position would automatically wrap within the area.
2 - Another problem would be how to handle collisions between wrapped sprites properly. For instance:
This should trigger only one collision (okay, consider that they are overlapping a bit although the drawing might not suggest it).
Checking for the collision manually shouldn't be too hard.
But the bigger problem arises when using this together with a physics engine (which is what I tried to do before and failed). How would I coerce the physics engine into handling this problem correctly, since there's usually a 1 to 1 correspondence between the physics body and the sprite representation?
Edit
You can see the solution to this particular point here. I create 9 bodies attached to each other using the joint described in that question, like so:
3 - Finally, and being more specific about XNA in this case, if the wrap region happens to be smaller than the Viewport, what would be the best way to clip the sprite so that it does't show outside the region anywhere?
Take into consideration that the sprite may be rotated or scaled too.