3
\$\begingroup\$

I also use Farseer Physics and Artemis ECS. Here is RenderingSystem Process method:

    public override void Process(Entity entity, TransformComponent transformComponent, RenderingComponent renderingComponent)
    {
        Vector2 dimension = transformComponent.Dimension * camera.ScalingFactor;

        Vector2 rotationOrigin = new Vector2(dimension.X,dimension.Y)/2;

        Vector2 renderingPosition = new Vector2(1, -1) * transformComponent.RenderingPosition * camera.ScalingFactor + camera.BitmapPosition;

        Rectangle rectangle = new Rectangle((int)(renderingPosition.X), (int)(renderingPosition.Y), (int)dimension.X, (int)dimension.Y);

        spriteBatch.Draw(renderingComponent.Texture, rectangle, 
            null, Color.Wheat, -transformComponent.Rotation,rotationOrigin, SpriteEffects.None, 0f);
    }

That's what i get: enter image description here

And that's what I get if

Vector2 rotationOrigin = new Vector2.Zero;

enter image description here

What's the reason for such a weird behavoir of rotationOrigin parameter and how to correctly rotate the sprite around its center?

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Turns out the origin parameter represents an offset in the sprite's original terxture, not the scaled one being showed on screen. So here is the correct version:

Vector2 dimension = transformComponent.Dimension * camera.ScalingFactor;

        Vector2 rotationOrigin = new Vector2(dimension.X/2,dimension.Y / 2);

        Vector2 renderingPosition = new Vector2(1, -1) * transformComponent.Position * camera.ScalingFactor + camera.BitmapPosition;

        spriteBatch.Draw(
             renderingComponent.Texture,
             renderingPosition,
             null,
             Color.White,
             -transformComponent.Rotation,
             new Vector2(
                 renderingComponent.Texture.Width / 2,
                 renderingComponent.Texture.Height / 2),
             new Vector2(dimension.X / renderingComponent.Texture.Width, dimension.Y / renderingComponent.Texture.Height),
             SpriteEffects.None,
             1.0f);
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .