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

Author Topic: How to change view based on mouse position?  (Read 3274 times)

0 Members and 1 Guest are viewing this topic.

Coakie

  • Newbie
  • *
  • Posts: 5
    • View Profile
How to change view based on mouse position?
« on: December 19, 2016, 10:41:12 pm »
I want to make a circle always in the middle of the window, but I want the view to change based on the mouse position. For example, if I move the cursor above the circle, the view will change, but the circle will still be fixed in the middle of the window.

                sf::Vector2i pixelPos = sf::Mouse::getPosition(window);
                sf::Vector2f worldPos = window.mapPixelToCoords(pixelPos);

                playerCircle.setPosition(view.getCenter());
                view.setCenter(worldPos);
 
I tried this, but it doesn't work

Hapax

  • Hero Member
  • *****
  • Posts: 3381
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How to change view based on mouse position?
« Reply #1 on: December 20, 2016, 12:10:24 am »
Try setting the circle's position to the view's centre after you've correctly set the view's centre. ;)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Coakie

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: How to change view based on mouse position?
« Reply #2 on: December 20, 2016, 01:16:35 am »
Try setting the circle's position to the view's centre after you've correctly set the view's centre. ;)
I tried this, but now it does the same thing as before, just moving the view around super quick. Is there a way to fix this? Here is a copy of my messy code so you can test it out for yourself; if you press the mouse then it will show the coordinates.
(click to show/hide)

Hapax

  • Hero Member
  • *****
  • Posts: 3381
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How to change view based on mouse position?
« Reply #3 on: December 20, 2016, 03:23:16 pm »
I want the view to change based on the mouse position.
is unclear.

How do you intend the view to move based on the mouse movement?
Do you want it to centre on the mouse when you click or something?
Or, do you want it to move at a speed based on the mouse's offset from the centre.

If the former, the view's centre should only be updated when there has been a click.

If the latter, you get the difference between the current mouse position and the centre, then you can use that amount to offset the current view centre (move the view). This difference can be multiplied to scale the amount you want it to affect the speed. You could also use a specific view for this - one with a lower range. Then use this view to find the mouse position. This can reduce the need for scaling the result. If this view's centre is at zero, there would also be no need for any "difference" calculations.

Maybe you want something completely different but we can't know the exact thing you want without enough information.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Coakie

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: How to change view based on mouse position?
« Reply #4 on: December 20, 2016, 05:29:21 pm »
If the latter, you get the difference between the current mouse position and the centre, then you can use that amount to offset the current view centre (move the view). This difference can be multiplied to scale the amount you want it to affect the speed. You could also use a specific view for this - one with a lower range. Then use this view to find the mouse position. This can reduce the need for scaling the result. If this view's centre is at zero, there would also be no need for any "difference" calculations.
I wanted it to move similar to the way you explained it, except I want the view to move at the same speed regardless of the amount of distance between the cursor and the circle, similar to a game like "agar.io". Can you maybe give to me an example of this to get started? :'(

Hapax

  • Hero Member
  • *****
  • Posts: 3381
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How to change view based on mouse position?
« Reply #5 on: December 23, 2016, 12:19:52 pm »
The second way I explained?

I think I would try to set up a view to use just for this, probably size of 2x2 and centre at (0,0). This means that the positions of the window are (-1, -1) to (1, 1). If you map your mouse position to that view, you can see which direction the mouse is from the centre of the window.
To move in that direction by a constant speed, you find the unit vector of the mouse's mapped position and then multiply that by the required speed.

Note that you may want to consider some distance from the centre as still in the centre otherwise it'll be difficult to stop the motion.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything