Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Text doesn't rotate around origin [bug?]  (Read 4815 times)

0 Members and 1 Guest are viewing this topic.

SuperV1234

  • SFML Team
  • Full Member
  • *****
  • Posts: 190
    • View Profile
Text doesn't rotate around origin [bug?]
« on: September 23, 2012, 10:36:57 am »
Using last SFML.NET (from GitHub).



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();
    }          
}
« Last Edit: September 23, 2012, 11:41:06 am by SuperV1234 »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Text doesn't rotate around origin [bug?]
« Reply #1 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?
Laurent Gomila - SFML developer

SuperV1234

  • SFML Team
  • Full Member
  • *****
  • Posts: 190
    • View Profile
Re: Text doesn't rotate around origin [bug?]
« Reply #2 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-
« Last Edit: September 23, 2012, 11:40:55 am by SuperV1234 »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Text doesn't rotate around origin [bug?]
« Reply #3 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
Laurent Gomila - SFML developer

SuperV1234

  • SFML Team
  • Full Member
  • *****
  • Posts: 190
    • View Profile
Re: Text doesn't rotate around origin [bug?]
« Reply #4 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();
    }          
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Text doesn't rotate around origin [bug?]
« Reply #5 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.
Laurent Gomila - SFML developer

SuperV1234

  • SFML Team
  • Full Member
  • *****
  • Posts: 190
    • View Profile
Re: Text doesn't rotate around origin [bug?]
« Reply #6 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:



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?

Marcus

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Text doesn't rotate around origin [bug?]
« Reply #7 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.

SuperV1234

  • SFML Team
  • Full Member
  • *****
  • Posts: 190
    • View Profile
Re: Text doesn't rotate around origin [bug?]
« Reply #8 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

 

anything