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

Author Topic: Views setcenter vs rotation problem  (Read 2601 times)

0 Members and 1 Guest are viewing this topic.

rogeriodec

  • Newbie
  • *
  • Posts: 42
    • View Profile
Views setcenter vs rotation problem
« on: April 26, 2018, 06:22:03 am »
  • I have a view with same dimensions of original window (500,300)


  • I apply view.zoom(2) to leave the view at half the size.


  • Now the view is centered. I want to move the view to the upper left corner of the original window. So I put view.setCenter(500,300);


  • The view is now correctly positioned in the upper corner of the original window. But now I want to rotate the view, making the center of the view its own top left corner, ie (0,0): view.setRotation(5);


As you can see, the center of the axis of rotation should be 0.0 but not respected.
The problem is that if I do view.setcenter (0,0), the whole view returns to the middle of the original window.
How to solve this?
« Last Edit: April 26, 2018, 06:23:35 am by rogeriodec »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: Views setcenter vs rotation problem
« Reply #1 on: April 26, 2018, 08:39:38 am »
Maybe my tutorial on sf::View will be more helpful: https://github.com/SFML/SFML/wiki/Tutorial%3A-Using-View
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Geheim

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Email
Re: Views setcenter vs rotation problem
« Reply #2 on: April 26, 2018, 11:59:16 am »
I am not sure if you intent to make a split-screen, in which case following the great tutorial will give you something like this:
(click to show/hide)

Otherwise there is also the option to draw your stuff onto a RenderTexture, rotating and drawing the resulting sprite onto your window, which will lead to something like this:
(click to show/hide)

Maybe that helps.
« Last Edit: April 26, 2018, 12:03:35 pm by Geheim »
Failing to succeed does not mean failing to progress!

rogeriodec

  • Newbie
  • *
  • Posts: 42
    • View Profile
Re: Views setcenter vs rotation problem
« Reply #3 on: April 27, 2018, 01:09:28 am »
I think the work you have in getting me to study, it would be much easier to write 2 or 3 lines of code and I would have the solution.
Didatically, learning from examples is much more efficient than just learning from theory (even more so with limited documentation).
Could you just tell me what to do?
« Last Edit: April 27, 2018, 01:19:27 am by rogeriodec »

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Views setcenter vs rotation problem
« Reply #4 on: April 28, 2018, 09:13:49 pm »
Although I have provided an answer on your stackexchange question, I'll relay it here too for the benefit of others who may have the same or similar question or need.

SFML views always rotate around their centre. This, however, does not mean that we can't effectively rotate them around any desired point; it just takes a little extra mathematics...

The basic idea is that you take the offset of the desired target point from the actual centre and rotate this vector (from the centre to the target point) around the view's centre by the amount of rotation you wish to rotate the view. You then need to calculate the difference between those points. You can now rotate the view as normal around the centre but you follow that with a simple move by the difference you just calculated. This makes the point match up to where it was before!

I've added a free function to the SFML wiki that does all this in a little encapsulated function. It takes the point around which you want to rotate the view, the view itself, and the amount of rotation in degrees - all as parameters to the function. The view is manipulated to rotate around that point by your required amount.
Note that a custom view object is required for this approach. If you require applying directly to the window's view, you could modify the function to create a temporary view internally and the set the window's view automatically. Also, you'd need to pass the window instead of a view. However, created your own view (temporary or not) is probably easier :P

Anyway, the function itself is simply just this:
void rotateViewAt(sf::Vector2f coord, sf::View& view, float rotation)
{
    const sf::Vector2f offset{ coord - view.getCenter() };
    const float rotationInRadians{ rotation * 3.141592653f / 180.f };
    const float sine{ std::sin(rotationInRadians) }; // remember to "#include <cmath>" for this
    const float cosine{ std::cos(rotationInRadians) }; // remember to "#include <cmath>" for this
    const sf::Vector2f rotatedOffset{ cosine * offset.x - sine * offset.y, sine * offset.x + cosine * offset.y };
    view.rotate(rotation);
    view.move(offset - rotatedOffset);
}

For more information, see the Wiki page, Source: Rotate View At (specified co‐ordinate)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

rogeriodec

  • Newbie
  • *
  • Posts: 42
    • View Profile
Re: Views setcenter vs rotation problem
« Reply #5 on: April 28, 2018, 09:39:55 pm »
Thank you all for your attention and clarification.
In fact I admit that for a beginner in SFML there are missing some important bases, which after learning, everything becomes easier.
With your approaches, now I can better glimpse the solution.
Thank you very much.

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Views setcenter vs rotation problem
« Reply #6 on: April 28, 2018, 09:45:55 pm »
You are welcome.

I have no qualms saying that I do indeed believe that views are hard.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything