Using Transform in SFML

I want to explain how to use transform in SFML.NET.  I think, this is a good way to manipulate the sprites invidually. Also, this way solved my problem in my prototype. Firstly, I created a new project. I should save a template, because no need to setup sfml project again and again. You can find tutorial about creating SFML tutorial from this link.

I just created a texture as an example like this:


I want to draw it on the screen at first:

using System;
using SFML.Graphics;
using SFML.Window;

namespace transformUsage
{
    class Program
    {
        ...

        static void Main(string[] args)
        {
            ...
            Sprite entity = new Sprite(new Texture("textures/texture.png"));
            entity.Position = new SFML.System.Vector2f(0,0);

            ...

            while (window.IsOpen)
            {
                ...
                entity.Draw(window, new RenderStates(Transform.Identity));
                window.Display();
            }
        }
        
    }
}
Now, I want to rotate this from specific points of the entity. For this, I will create a new object as RenderStates. I will pass entity.Transform as argument, and then called this Rotate method from transform of our renderstate. We can define angle at first argument. According to this angle the object will be rotated. The other two arguments defined origin of the rotation:

namespace transformUsage
{
    class Program
    {
        ...

        static void Main(string[] args)
        {
            ...
            Sprite entity = new Sprite(new Texture("textures/texture.png"));
            entity.Position = new SFML.System.Vector2f(0,0);

            RenderStates rs = new RenderStates(entity.Transform);
            rs.Transform.Rotate(45, 0, 0);
            ...

            while (window.IsOpen)
            {
                ...
                entity.Draw(window, rs);
                window.Display();
            }
        }
        
    }
}
Let's look at the result:
using transform in sfml

This transform is actuall a matrix. You probably studied this subject from math in high school. There are a lot of sources about usage of matrix for transformation, one of these sources is OpenGL - Transformations.

My texture of sprite's dimension is 128x128. I can use it for define origin of the rotation. But, firstly, I want to centered my sprite on the screen.After that, Let's define the origin of the sprite for rotation:
        static void Main(string[] args)
        {
            RenderWindow window = new RenderWindow(new VideoMode(WIDTH, HEIGHT), TITLE);
            Sprite entity = new Sprite(new Texture("textures/texture.png"));
            entity.Origin = new SFML.System.Vector2f(64,64);
            entity.Position = new SFML.System.Vector2f(WIDTH/2, HEIGHT/2);

            RenderStates rs = new RenderStates(Transform.Identity);
            int rotation = 0;
            window.SetFramerateLimit(60);

            window.Closed += (sender, args) => window.Close();


            while (window.IsOpen)
            {
                window.DispatchEvents();

                rotation = 1;
                rs.Transform.Rotate(rotation, entity.Position);

                window.Clear(Color.Black);
                entity.Draw(window, rs);
                window.Display();
            }
        }
I changed entity.Transform as Transform.Identity, because that causes false positioning on the screen. I think we should use Transform.Identity at first all the time. This Identity represents of unit matris. Let's look at the result:
Now, we can rotate this entity according to the origin of the entity. Also, we can use scale and translate methods for this sprite like we did with rotate method.

No comments:

Post a Comment