I like using transformation matrices for this, because it allows me to have all sorts of complex father-child relationship between sprites such has having different relative positions, rotations, scales and origins relative to the parent, but in the end the only thing I need to do is multiply all of their matrices together and render.
I could repeat everything again but I've already written about this before on the two following questions, so look into them for more detail:
Is there a simple way to group two or more sprites, so all of them will be dependent on each other?
How to "swing" bounding box and update collision for sprite swinging a weapon?
I'll briefly describe the concept here though. The basic idea behind everything is that for each sprite in your game you'll have a single matrix encapsulating all of its transformations relative to the parent. In the case that there is no parent, then the transformation is simply treated as being in world space already.
Here's the basic interface for your sprite:
class Sprite
{
Matrix RelativeTransform;
Sprite Parent;
List<Sprite> Children;
}
Then for any sprite, you can find the actual absolute transform with the following (somewhat recursive) expression:
public Matrix AbsoluteTransform
{
get
{
if (Parent == null)
return RelativeTransform;
else return
RelativeTransform * Parent.AbsoluteTransform;
}
}
In other words, multiply your matrices together, starting from the current sprite and going all the way up to the root. This will get the absolute transform of that sprite in world space, which you can use to draw it.
As for drawing it, the first question I linked above has a method called DecomposeMatrix
which you can use to take a Matrix
and get some values back from it that you can use with SpriteBatch
.