Skip to main content
added 25 characters in body
Source Link
David Gouveia
  • 25k
  • 5
  • 89
  • 127

I've got this implemented on my world editor. I can group sprites together and rotate them or scale them as a group around an arbitrary origin (which in my case is also the center of the group). I use something like this:

// Origin should be supplied in World-Space
void RotateGroup(IEnumerable<Sprite> sprites, Vector2 origin, float angle)
{
    Matrix transform =  Matrix.CreateTranslation(-origin.X, -origin.Y, 0f) *
                        Matrix.CreateRotationZ(angle) *
                        Matrix.CreateTranslation(origin.X, origin.Y, 0f);
                        
    foreach(Sprite sprite in sprites)
    {
        sprite.Position = Vector2.Transform(sprite.Position, transform);
        sprite.Rotation += angle;
    }
}
// Origin should be supplied in World-Space
void RotateGroup(IEnumerable<Sprite> sprites, Vector2 origin, float angle)
{
    Matrix transform =  Matrix.CreateTranslation(-origin.X, -origin.Y, 0f) *
                        Matrix.CreateRotationZ(angle) *
                        Matrix.CreateTranslation(origin.X, origin.Y, 0f);
                        
    foreach(Sprite sprite in sprites)
    {
        sprite.Position = Vector2.Transform(sprite.Position, transform);
        sprite.Rotation += angle;
    }
}

I simply call this method when I want to rotate my group as a whole, and the method calculates a new position and rotation for each sprite in order to give the illusion that they are changing as a group.

Most importantly, I don't pass any special matrix to my drawing function. Each sprite still has its own individual position, rotation and origin which are independent from them being in a group or not.

This means that the group's origin is conceptual, and simply changing that value won't mess up with your sprites. The value is only used when rotating the group in order to specify where the pivot should be, but not when drawing. So

In sum, the only matrix you should be passing to SpriteBatch.Begin is your viewView matrix, nothing else, and each. Each sprite should be drawn according to its usualindividual properties, and if you'd like to rotate them as a group, play around those properties, not around the drawing code.

I've got this implemented on my world editor. I can group sprites together and rotate them or scale them as a group around an arbitrary origin (which in my case is also the center of the group). I use something like this:

// Origin should be supplied in World-Space
void RotateGroup(IEnumerable<Sprite> sprites, Vector2 origin, float angle)
{
    Matrix transform =  Matrix.CreateTranslation(-origin.X, -origin.Y, 0f) *
                        Matrix.CreateRotationZ(angle) *
                        Matrix.CreateTranslation(origin.X, origin.Y, 0f);
                        
    foreach(Sprite sprite in sprites)
    {
        sprite.Position = Vector2.Transform(sprite.Position, transform);
        sprite.Rotation += angle;
    }
}

I simply call this method when I want to rotate my group as a whole, and the method calculates a new position and rotation for each sprite in order to give the illusion that they are changing as a group.

Most importantly, I don't pass any special matrix to my drawing function. Each sprite still has its own individual position, rotation and origin which are independent from them being in a group or not.

This means that the group's origin is conceptual, and simply changing that value won't mess up with your sprites. The value is only used when rotating the group in order to specify where the pivot should be, but not when drawing. So the only matrix you should be passing to SpriteBatch.Begin is your view matrix, nothing else, and each sprite should be drawn according to its usual properties.

I've got this implemented on my world editor. I can group sprites together and rotate them or scale them as a group around an arbitrary origin (which in my case is also the center of the group). I use something like this:

// Origin should be supplied in World-Space
void RotateGroup(IEnumerable<Sprite> sprites, Vector2 origin, float angle)
{
    Matrix transform =  Matrix.CreateTranslation(-origin.X, -origin.Y, 0f) *
                        Matrix.CreateRotationZ(angle) *
                        Matrix.CreateTranslation(origin.X, origin.Y, 0f);
                        
    foreach(Sprite sprite in sprites)
    {
        sprite.Position = Vector2.Transform(sprite.Position, transform);
        sprite.Rotation += angle;
    }
}

I simply call this method when I want to rotate my group as a whole, and the method calculates a new position and rotation for each sprite in order to give the illusion that they are changing as a group.

Most importantly, I don't pass any special matrix to my drawing function. Each sprite still has its own individual position, rotation and origin which are independent from them being in a group or not.

This means that the group's origin is conceptual, and simply changing that value won't mess up with your sprites. The value is only used when rotating the group in order to specify where the pivot should be, but not when drawing.

In sum, the only matrix you should be passing to SpriteBatch.Begin is your View matrix, nothing else. Each sprite should be drawn according to its individual properties, and if you'd like to rotate them as a group, play around those properties, not around the drawing code.

trying to make it clearer
Source Link
David Gouveia
  • 25k
  • 5
  • 89
  • 127

I've got this implemented on my world editor. I can group sprites together and rotate them or scale them as a group around an arbitrary origin (which in my case is also the center of the group). I use something like this:

// Origin should be supplied in World-Space
void RotateGroup(IEnumerable<Sprite> sprites, Vector2 origin, float angle)
{
    Matrix transform =  Matrix.CreateTranslation(-origin.X, -origin.Y, 0f) *
                        Matrix.CreateRotationZ(angle) *
                        Matrix.CreateTranslation(origin.X, origin.Y, 0f);
                        
    foreach(Sprite sprite in sprites)
    {
        sprite.Position = Vector2.Transform(sprite.Position, transform);
        sprite.Rotation += angle;
    }
}

I simply call this method when I want to rotate my group as a whole, and the method calculates a new position and rotation for each sprite in order to give the illusion that they are changing as a group.

Most importantly, I don't usepass any special matrix whento my drawing function. Each sprite simplystill has its own individual position and rotation, rotation and the origin which is always at the sprite's centerare independent from them being in a group or not.

This means that the "group's origin" doesn't directly change where the sprites are drawngroup's origin is conceptual, so you can change it without the spritesand simply changing at allthat value won't mess up with your sprites. ItThe value is only affectsused when rotating the act of rotation itselfgroup in order to specify where the pivot should be, but not when drawing. So the only matrix you should be passing to SpriteBatch.Begin is your view matrix, nothing else, and each sprite should be drawn according to its usual properties.

I've got this implemented on my world editor. I can group sprites together and rotate them or scale them as a group around an arbitrary origin (which in my case is also the center of the group). I use something like this:

// Origin should be supplied in World-Space
void RotateGroup(IEnumerable<Sprite> sprites, Vector2 origin, float angle)
{
    Matrix transform =  Matrix.CreateTranslation(-origin.X, -origin.Y, 0f) *
                        Matrix.CreateRotationZ(angle) *
                        Matrix.CreateTranslation(origin.X, origin.Y, 0f);
                        
    foreach(Sprite sprite in sprites)
    {
        sprite.Position = Vector2.Transform(sprite.Position, transform);
        sprite.Rotation += angle;
    }
}

I don't use any special matrix when drawing. Each sprite simply has its own position and rotation, and the origin which is always at the sprite's center.

This means that the "group's origin" doesn't directly change where the sprites are drawn, so you can change it without the sprites changing at all. It only affects the act of rotation itself.

I've got this implemented on my world editor. I can group sprites together and rotate them or scale them as a group around an arbitrary origin (which in my case is also the center of the group). I use something like this:

// Origin should be supplied in World-Space
void RotateGroup(IEnumerable<Sprite> sprites, Vector2 origin, float angle)
{
    Matrix transform =  Matrix.CreateTranslation(-origin.X, -origin.Y, 0f) *
                        Matrix.CreateRotationZ(angle) *
                        Matrix.CreateTranslation(origin.X, origin.Y, 0f);
                        
    foreach(Sprite sprite in sprites)
    {
        sprite.Position = Vector2.Transform(sprite.Position, transform);
        sprite.Rotation += angle;
    }
}

I simply call this method when I want to rotate my group as a whole, and the method calculates a new position and rotation for each sprite in order to give the illusion that they are changing as a group.

Most importantly, I don't pass any special matrix to my drawing function. Each sprite still has its own individual position, rotation and origin which are independent from them being in a group or not.

This means that the group's origin is conceptual, and simply changing that value won't mess up with your sprites. The value is only used when rotating the group in order to specify where the pivot should be, but not when drawing. So the only matrix you should be passing to SpriteBatch.Begin is your view matrix, nothing else, and each sprite should be drawn according to its usual properties.

Source Link
David Gouveia
  • 25k
  • 5
  • 89
  • 127

I've got this implemented on my world editor. I can group sprites together and rotate them or scale them as a group around an arbitrary origin (which in my case is also the center of the group). I use something like this:

// Origin should be supplied in World-Space
void RotateGroup(IEnumerable<Sprite> sprites, Vector2 origin, float angle)
{
    Matrix transform =  Matrix.CreateTranslation(-origin.X, -origin.Y, 0f) *
                        Matrix.CreateRotationZ(angle) *
                        Matrix.CreateTranslation(origin.X, origin.Y, 0f);
                        
    foreach(Sprite sprite in sprites)
    {
        sprite.Position = Vector2.Transform(sprite.Position, transform);
        sprite.Rotation += angle;
    }
}

I don't use any special matrix when drawing. Each sprite simply has its own position and rotation, and the origin which is always at the sprite's center.

This means that the "group's origin" doesn't directly change where the sprites are drawn, so you can change it without the sprites changing at all. It only affects the act of rotation itself.