SFML community forums

Help => Graphics => Topic started by: SuperV1234 on September 23, 2012, 10:36:57 am

Title: Text doesn't rotate around origin [bug?]
Post by: SuperV1234 on September 23, 2012, 10:36:57 am
Using last SFML.NET (from GitHub).

(https://dl.dropbox.com/u/3724424/sfmlbug.png)

This picture shows what happens. On the left you can see the text, without any rotation applied. The red square is the origin.

On the right, you see the text after a few complete rotations (and not clearing the render target). It doesn't rotate around the origin, but around the top of the "A" instead.

Here's the interesting code:

private static void Main()
{
    var renderWindow = new RenderWindow(new VideoMode(1024, 768), "Text Bug");

    var font = new Font("akz.ttf");
    var text = new Text("A", font, 60);
    text.Position = new Vector2f(1024 / 2f, 768 / 2f);
    text.Color = Color.Black;
    text.Origin = new Vector2f(text.GetGlobalBounds().Width / 2f, text.GetGlobalBounds().Height / 2f);

    while(true)
    {
        renderWindow.Clear(Color.White);
        text.Rotation++;
        renderWindow.Draw(text);
        renderWindow.Display();
    }          
}
Title: Re: Text doesn't rotate around origin [bug?]
Post by: Laurent on September 23, 2012, 10:51:04 am
Can you please post a minimal example that can be copied and compiled directly, and that contains only what's required to reproduce the problem?
Title: Re: Text doesn't rotate around origin [bug?]
Post by: SuperV1234 on September 23, 2012, 10:56:06 am
Can you please post a minimal example that can be copied and compiled directly, and that contains only what's required to reproduce the problem?

Here you go.
This makes the "A" spin around the top, not the center.

-snip-
Title: Re: Text doesn't rotate around origin [bug?]
Post by: Laurent on September 23, 2012, 11:35:03 am
This code is not complete nor minimal, I can't test it directly. If it's a problem with the Text class, I don't need all the stuff that you add on top of it (Game, GameWindow, ...).

Please read this carefully:
http://en.sfml-dev.org/forums/index.php?topic=5559.msg36368#msg36368
Title: Re: Text doesn't rotate around origin [bug?]
Post by: SuperV1234 on September 23, 2012, 11:39:33 am
This code is not complete nor minimal, I can't test it directly. If it's a problem with the Text class, I don't need all the stuff that you add on top of it (Game, GameWindow, ...).

Please read this carefully:
http://en.sfml-dev.org/forums/index.php?topic=5559.msg36368#msg36368

Sorry, that code basically translated to this.

private static void Main()
{
    var renderWindow = new RenderWindow(new VideoMode(1024, 768), "Text Bug");

    var font = new Font("akz.ttf");
    var text = new Text("A", font, 60);
    text.Position = new Vector2f(1024 / 2f, 768 / 2f);
    text.Color = Color.Black;
    text.Origin = new Vector2f(text.GetGlobalBounds().Width / 2f, text.GetGlobalBounds().Height / 2f);

    while(true)
    {
        renderWindow.Clear(Color.White);
        text.Rotation++;
        renderWindow.Draw(text);
        renderWindow.Display();
    }          
}
Title: Re: Text doesn't rotate around origin [bug?]
Post by: Laurent on September 23, 2012, 12:13:24 pm
Your code works for me, the "A" rotates around its origin. What's "wrong" is that the origin is not at the center of the letter, but this is expected because text has special alignment rules and the bounding box of a Text object is usually larger than what you see.
Title: Re: Text doesn't rotate around origin [bug?]
Post by: SuperV1234 on September 23, 2012, 12:21:50 pm
Your code works for me, the "A" rotates around its origin. What's "wrong" is that the origin is not at the center of the letter, but this is expected because text has special alignment rules and the bounding box of a Text object is usually larger than what you see.

Makes sense, but drawing the bounding box as a polygon is really not intuitive, look:

(https://dl.dropbox.com/u/3724424/bug2.png)

The following code displays that image. The origin is displayed in the center of the letter - the user expects the letter to rotate around its origin.

private static void Main()
{
    var renderWindow = new RenderWindow(new VideoMode(1024, 768), "Text Bug");

    var font = new Font("akz.ttf");
    var text = new Text("A", font, 60);
    text.Position = new Vector2f(1024 / 2f, 768 / 2f);
    text.Color = Color.Black;
    text.Origin = new Vector2f(text.GetGlobalBounds().Width / 2f, text.GetGlobalBounds().Height / 2f);

    while (true)
    {
        renderWindow.Clear(Color.White);
        text.Rotation += 0.01f;
        renderWindow.Draw(text);

        var x = text.GetGlobalBounds().Left;
        var y = text.GetGlobalBounds().Top;
        var width = text.GetGlobalBounds().Width;
        var height = text.GetGlobalBounds().Height;
        var origin = new Vector2f(width / 2f, height / 2f);
        var originS = new Vector2f(x, y) + origin - new Vector2f(3, 3);
        var originE = new Vector2f(x, y) + origin + new Vector2f(3, 3);

        var originColor = new Color(255, 0, 0, 255);
        var originVertices = new Vertex[4];
        originVertices[0] = new Vertex(new Vector2f(originS.X, originS.Y), originColor);
        originVertices[1] = new Vertex(new Vector2f(originE.X, originS.Y), originColor);
        originVertices[2] = new Vertex(new Vector2f(originE.X, originE.Y), originColor);
        originVertices[3] = new Vertex(new Vector2f(originS.X, originE.Y), originColor);

        var boundsColor = new Color(0, 255, 0, 75);
        var boundsVertices = new Vertex[4];
        boundsVertices[0] = new Vertex(new Vector2f(x, y), boundsColor);
        boundsVertices[1] = new Vertex(new Vector2f(x + width, y), boundsColor);
        boundsVertices[2] = new Vertex(new Vector2f(x + width, y + height), boundsColor);
        boundsVertices[3] = new Vertex(new Vector2f(x, y + height), boundsColor);

        renderWindow.Draw(originVertices, PrimitiveType.Quads);
        renderWindow.Draw(boundsVertices, PrimitiveType.Quads);

        renderWindow.Display();
    }      
}



Also, for my project, I actually need letters to rotate around their center. How can I achieve that?
Title: Re: Text doesn't rotate around origin [bug?]
Post by: Marcus on September 28, 2012, 04:17:14 pm
For any other sprite, the origin is considered the top left corner. The same rule seems to apply to  text. You have to manually set the origin to be the center of the text.
Title: Re: Text doesn't rotate around origin [bug?]
Post by: SuperV1234 on September 29, 2012, 05:01:38 pm
For any other sprite, the origin is considered the top left corner. The same rule seems to apply to  text. You have to manually set the origin to be the center of the text.

I'm doing that, but the rotation isn't happening at the origin