SFML community forums

Help => General => Topic started by: rogeriodec on April 26, 2018, 06:22:03 am

Title: Views setcenter vs rotation problem
Post by: rogeriodec on April 26, 2018, 06:22:03 am

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?
Title: Re: Views setcenter vs rotation problem
Post by: eXpl0it3r 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
Title: Re: Views setcenter vs rotation problem
Post by: Geheim 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 (https://www.sfml-dev.org/documentation/2.4.2/classsf_1_1RenderTexture.php), rotating and drawing the resulting sprite onto your window, which will lead to something like this:
(click to show/hide)

Maybe that helps.
Title: Re: Views setcenter vs rotation problem
Post by: rogeriodec 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?
Title: Re: Views setcenter vs rotation problem
Post by: Hapax on April 28, 2018, 09:13:49 pm
Although I have provided an answer (https://stackoverflow.com/a/50080023/6158263) 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) (https://github.com/SFML/SFML/wiki/Source%3A-Rotate-View-At-%28specified-co%E2%80%90ordinate%29)
Title: Re: Views setcenter vs rotation problem
Post by: rogeriodec 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.
Title: Re: Views setcenter vs rotation problem
Post by: Hapax 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.