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

Author Topic: How the heck does origin work? Extremely confused.....  (Read 8860 times)

0 Members and 1 Guest are viewing this topic.

Offam1992

  • Newbie
  • *
  • Posts: 4
    • View Profile
How the heck does origin work? Extremely confused.....
« on: February 27, 2019, 05:10:48 pm »
So I'm trying to work with the origin, and it makes no sense to me at all. Here's some simple code I wrote to play around with the origin:


        sf::RenderWindow window(sf::VideoMode(1200, 800), "Window");

        sf::RectangleShape square(sf::Vector2f(200, 200));
        square.setPosition(500, 600);

        auto smallSquare = square;
        smallSquare.move(0, -square.getSize().y);
        smallSquare.setOrigin(0, square.getSize().y);
        smallSquare.scale(0.5, 0.5);

        while (window.isOpen())
        {

                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                }
                window.draw(square);
                window.draw(smallSquare);
                window.display();
        }

What I expect this to do is
1) Draw a 200x200 square at the center bottom of the screen
2) Make a copy of that square called smallSquare
3) Move the smallSquare upwards by the height of the square
4) Set smallSquare's origin to it's bottom left corner, instead of top left.
5) Scale the smallSquare to 0.5 around it's new origin

So the result should be a 200x200 square at the center bottom, with a 100x100 square on top of it on it's left side. Sort of like a blocky lowercase b shape.

Instead what I get is

(click to show/hide)

Why is this happening? What's wrong with my assumptions of how the origin works?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: How the heck does origin work? Extremely confused.....
« Reply #1 on: February 27, 2019, 05:46:35 pm »
The move function is a shortcut for setPosition, and position is also based on the local origin. And remember that transformations are not applied immediately, but rather in a fixed order when the entity is drawn. So I think your assumptions about the final position are wrong.
« Last Edit: February 27, 2019, 05:48:21 pm by Laurent »
Laurent Gomila - SFML developer

Offam1992

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: How the heck does origin work? Extremely confused.....
« Reply #2 on: February 27, 2019, 06:59:43 pm »
The move function is a shortcut for setPosition, and position is also based on the local origin. And remember that transformations are not applied immediately, but rather in a fixed order when the entity is drawn. So I think your assumptions about the final position are wrong.

I see. So to solve the first problem, always do moves and setPositions before changing the origin?

How could I solve the second problem? Is there a way to force apply the transformations immediately?

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How the heck does origin work? Extremely confused.....
« Reply #3 on: February 27, 2019, 07:27:03 pm »
When you set the origin, you choose which part of the local object should be used as the origin; for example, if you set the origin of a rectangle to half of its size, its origin will be in the centre of that rectangle.

When you set the position, you are choosing where to put the origin of that object. So, for the previous example of the rectangle and its origin at its centre, you will be placing its centre at the co-ordinate that you supply.

It doesn't matter which order you set them as long as you set them both (if you need to change them both).

To expand slightly on the above example, if you set that rectangle's position to the centre of the window, the centre of the rectangle would be in the centre of the window i.e. the rectangle is centred within the window. However, if you set the position to the top-left corner of the window, only the bottom-right quarter of the rectangle would be visible (in the top-left corner) as the centre of the rectangle is in that corner.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Offam1992

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: How the heck does origin work? Extremely confused.....
« Reply #4 on: February 27, 2019, 07:31:21 pm »
I see. Then what is wrong with the coded I posted above? Is it just the issue of transformations not being applied until it's drawn?

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How the heck does origin work? Extremely confused.....
« Reply #5 on: February 27, 2019, 07:36:51 pm »
If you're setting the origin to the bottom of the smaller rectangle and want both origins to be in the same place (i.e. the bottom of the small one touching the top of the larger one) then both rectangles should have the same position.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: How the heck does origin work? Extremely confused.....
« Reply #6 on: February 27, 2019, 08:12:26 pm »
Quote
I see. So to solve the first problem, always do moves and setPositions before changing the origin?
I don't know what "first" and "second" problems you refer to, but as I said these functions are just setters for position/rotation/scale variables, which are then combined at draw time, always in the same order. So chaning the origin after or before won't make any difference.

Quote
Is there a way to force apply the transformations immediately?
Yes. Bypass the entity's transform with your own transform (sf::Transform). sf::Transform is lower level, it's actually a thin wrapper on top of a 4x4 matrix so you can build whatever you want with it.

sf::Transform transform;
transform.translate(x, y); // first translate
transform.rotate(a); // then rotate
// ...
window.draw(entity, transform);

Then, remember that everything based on the entity's transform (local/global bounds for example) won't be right since it will use the entity's transform and not yours.
« Last Edit: February 27, 2019, 08:14:05 pm by Laurent »
Laurent Gomila - SFML developer

Offam1992

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: How the heck does origin work? Extremely confused.....
« Reply #7 on: February 27, 2019, 08:40:19 pm »
If you're setting the origin to the bottom of the smaller rectangle and want both origins to be in the same place (i.e. the bottom of the small one touching the top of the larger one) then both rectangles should have the same position.

I see. So let's say I want the same result, but the starting square has it's origin in the center. How would I go about doing it?


Quote
Is there a way to force apply the transformations immediately?
Yes. Bypass the entity's transform with your own transform (sf::Transform). sf::Transform is lower level, it's actually a thin wrapper on top of a 4x4 matrix so you can build whatever you want with it.

sf::Transform transform;
transform.translate(x, y); // first translate
transform.rotate(a); // then rotate
// ...
window.draw(entity, transform);

Then, remember that everything based on the entity's transform (local/global bounds for example) won't be right since it will use the entity's transform and not yours.

Thanks. Unfortunately I won't be able to use that method as I need to be able to use the entity's transform.
« Last Edit: February 27, 2019, 08:42:12 pm by Offam1992 »

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How the heck does origin work? Extremely confused.....
« Reply #8 on: March 02, 2019, 04:48:10 pm »
So let's say I want the same result, but the starting square has it's origin in the center. How would I go about doing it?
Set its position to where you want its centre/origin to be.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Wake

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: How the heck does origin work? Extremely confused.....
« Reply #9 on: January 17, 2021, 11:53:28 pm »
OMG this thread saved my life!

I included screen captures of the way to return sf::Text with sf::Font from a class to the main window.draw() function!  8)

#1 Rule!

YOU MUST make sf::Font a MEMBER VARIABLE OF THE CLASS and load the .ttf to the sf::Text Member Variable in the constructor... Make sf::Text another member variable and set the font using the sf::Font member variable in the constructor.

« Last Edit: January 17, 2021, 11:56:02 pm by Wake »

 

anything