How do I adjust the origin of rotation for a group of sprites?
Posted
by
Jon
on Game Development
See other posts from Game Development
or by Jon
Published on 2011-11-29T18:37:55Z
Indexed on
2011/11/30
18:17 UTC
Read the original article
Hit count: 312
I am currently grouping sprites together, then applying a rotation transformation on draw:
private void UpdateMatrix(ref Vector2 origin, float radians)
{
Vector3 matrixorigin = new Vector3(origin, 0);
_rotationMatrix = Matrix.CreateTranslation(-matrixorigin) * Matrix.CreateRotationZ(radians) * Matrix.CreateTranslation(matrixorigin);
}
Where the origin is the Centermost point of my group of sprites. I apply this transformation to each sprite in the group.
My problem is that when I adjust the point of origin, my entire sprite group will re-position itself on screen.
How could I differentiate the point of rotation used in the transformation, from the position of the sprite group? Is there a better way of creating this transformation matrix?
EDIT
Here is the relevant part of the Draw() function:
Matrix allTransforms = _rotationMatrix * camera.GetTransformation();
spriteBatch.Begin(SpriteSortMode.BackToFront, null, null, null, null, null, allTransforms);
for (int i = 0; i < _map.AllParts.Count; i++)
{
for (int j = 0; j < _map.AllParts[0].Count; j++)
{
spriteBatch.Draw(_map.AllParts[i][j].Texture, _map.AllParts[i][j].Position, null, Color.White, 0, _map.AllParts[i][j].Origin, 1.0f, SpriteEffects.None, 0f);
}
}
This all works fine, again, the problem is that when a rotation is set and the point of origin is changed, the sprite group's position is offset on screen.
I am trying to figure out a way to adjust the point of origin without causing a shift in position.
EDIT 2 At this point, I'm looking for workarounds as this is not working. Does anyone know of a better way to rotate a group of sprites in XNA? I need a method that will allow me to modify the point of rotation (origin) without affecting the position of the sprite group on screen.
© Game Development or respective owner