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

Author Topic: Please help me understanding views  (Read 3466 times)

0 Members and 1 Guest are viewing this topic.

Jeffrey

  • Newbie
  • *
  • Posts: 36
    • View Profile
Please help me understanding views
« on: July 29, 2012, 02:39:09 pm »
I can't understand how views works. I've read all the tutorial on the internet of the first 2 pages of google searching for it. I've also read a view tutorial that someone posted in another question of mine. Particularly I can't get how the coordinates works when a view is there. And when there are multiple views?

When we set setPosition() of a generic object (say sf::Sprite for example) what are we setting? Are we dealing with the renderwindow x and y, the view x and y or something else? Is there a way to find out how to convert from a type of coordinates to another? What is the function convertCoords supposed to convert?

The documentation and tutorials on this website are really poor about that. Could you please explain that to me referring to SFML 2.0 specifically?

Thanks in advance.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Please help me understanding views
« Reply #1 on: July 29, 2012, 03:04:29 pm »
Hmmm and I though I've covered your first question in full detail in my tutorial...

Anyways the position of a sprite does relate to a view's coordinate system, but no one in particular, it will choose the view that is set on the render target at the point where the draw call is executed.

convertCoords takes a pixel coordinate point directly on the window or exactly from the pixels you see on the window. If you now have changed the view somehow the coordinates of the view and the window do not match anymore, but you can call convertCoords you can convert a point (e.g. mouse position) into the view coordinate system.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Jeffrey

  • Newbie
  • *
  • Posts: 36
    • View Profile
Re: Please help me understanding views
« Reply #2 on: July 29, 2012, 03:15:03 pm »
How can I covert them? convertCoords accept an integer 2d vector while the position is usually dealt with a float 2d vector. All I ask is an example, please...

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Please help me understanding views
« Reply #3 on: July 29, 2012, 03:21:22 pm »
How can I covert them? convertCoords accept an integer 2d vector while the position is usually dealt with a float 2d vector. All I ask is an example, please...
Depends the mouse position is also sf::Vector2i. ;)
window.convertCoords(static_cast<sf::Vector2i>(sf::Vector2f(10.f, 10.f)));
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Jeffrey

  • Newbie
  • *
  • Posts: 36
    • View Profile
Re: Please help me understanding views
« Reply #4 on: July 29, 2012, 03:32:13 pm »
I'm really confused. I mean, when I set setPosition I'm setting the x and y offset from the View we set when we draw the sprite. Now getPosition() will return the same value right? Then If I have a fixed view (that doesn't move) with a sprite (which is linked to the mouse movement) and a movable view with other sprite and I want to move a sprite from the movable view towards the sprite in the fixed view I should do convertCoords(<position sprite from fixed view >, <movable view>). Is that correct?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Please help me understanding views
« Reply #5 on: July 29, 2012, 03:42:47 pm »
You could do some work with sf::Transform but an easier solution would be to convert the position of the mouse/fixed view sprite to the moved view and then use that coordinate to set the sprite within the moved view.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Jeffrey

  • Newbie
  • *
  • Posts: 36
    • View Profile
Re: Please help me understanding views
« Reply #6 on: July 29, 2012, 03:49:33 pm »
What? Could you provide a pseudo-code please?  :-X

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Please help me understanding views
« Reply #7 on: July 29, 2012, 04:38:32 pm »
What? Could you provide a pseudo-code please?  :-X
Because it looks like you really tried to understand it, I'll provid some more code:
sf::RenderWindow window(...);
sf::Sprite sprite;
sf::Sprite cursor;
sf::View guiView = window.getDefaultView();
sf::View movableView = guiView;

// Main loop

// Change view
movableView.move( 10.f, 5.f );

// Set cursor position
cursor.setPosition( sf::Mouse::getPosition( window ) );
// Set sprite position to the location of the mouse/the cursor
sprite.setPosition( window.convertCoords( sf::Mouse::getPosition( window ), movableView ) );

// Draw with diffrent views
window.clear();
window.setView( movableView );
window.draw( sprite );
window.setView( guiView );
window.draw( cursor );
window.display();
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Please help me understanding views
« Reply #8 on: July 29, 2012, 04:46:54 pm »
Forget about views. The position of sprites and other entities are relative to the 2D world coordinate system, which can also be called the "global" coordinate system. All entites use this coordinate system.

Now, the view just selects a sub-area of the 2D world and displays it in your render window. That's all. The view is the user's eyes.

When you draw something which belongs to the global 2D world, it is transformed (translated, scaled, rotated) to match the current view, and rasterized (transformed to pixels) to the render window.

The convertCoords function has a very specific role: it provides the inverse transform, ie. from pixels (which are always integer coordinates) to the 2D world. Pixels are usually the mouse position; if you're trying to use convertCoords with float coordinates it means that you're doing something wrong, because these coordinates already belong to the 2D world.
Laurent Gomila - SFML developer

Jeffrey

  • Newbie
  • *
  • Posts: 36
    • View Profile
Re: Please help me understanding views
« Reply #9 on: July 30, 2012, 05:44:32 am »
Now I'm beginning to understand. But for example, if I set a sprite:

sprite.setPosition(window.convertCoords( sf::Mouse::getPosition( window ), movableView ));

and then I do:

anotherSprite.setPosition(sprite.getPosition()); // sprite that is in the movable view

I should get two sprites in the same position right?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Please help me understanding views
« Reply #10 on: July 30, 2012, 08:21:40 am »
Quote
I should get two sprites in the same position right?
Yes, since they are all using the same coordinate system.
Laurent Gomila - SFML developer