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

Author Topic: Mouse::setPosition problem with View  (Read 6432 times)

0 Members and 1 Guest are viewing this topic.

monkey3

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Mouse::setPosition problem with View
« on: October 04, 2015, 03:07:51 pm »
Hi again brothers and sisters.

I have a problem while using Mouse::setPosition method in View.
My screen's height is 1080 pixel. And 0 to 1080 there is no problem. But -0 and 1080+ it stucks and doesn't change.

Im making a topdown shooter game and using mouse pointer as taking aim facility. So when player moves, then mouse pointer move too. And view following player's position.

Thanks for your help already.

edit: I tried using mapCoordsToPixel fucntion but I couldn't figure out it doesn't work right.
« Last Edit: October 04, 2015, 03:32:41 pm by monkey3 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Mouse::setPosition problem with View
« Reply #1 on: October 04, 2015, 03:32:47 pm »
Not exactly sure what you're asking, but if you make a shooter, you should work with delta positions, meaning you measure the difference between position A and B and apply that to your view movement. Next you can reset the mouse cursor to the center of the screen/window. That way you won't end up getting "stuck" somewhere.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

SpectreNectar

  • Newbie
  • *
  • Posts: 39
    • View Profile
Re: Mouse::setPosition problem with View
« Reply #2 on: October 04, 2015, 05:13:14 pm »
I don't know if you're already aware but without the relativeTo argument passed in this function works relative to the desktop coordinates and not the windows size. Hope this helps.

monkey3

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Re: Mouse::setPosition problem with View
« Reply #3 on: October 05, 2015, 06:37:46 pm »
@SpectreNectar Thanks, I know that.

@eXpl0it3r Can you explain detailed?

monkey3

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Re: Mouse::setPosition problem with View
« Reply #4 on: October 05, 2015, 06:49:45 pm »
The center of view is player's position. Player moves and then aim facility moves and mousecursor moves.
But when the player move out of Screen width or height, mousecursor overflows out of window.

I am confused about object's positions in window, view and screen.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Mouse::setPosition problem with View
« Reply #5 on: October 05, 2015, 08:45:51 pm »
Your problem description is still not clear to me...

Have you read and understood the view tutorial?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

AFS

  • Full Member
  • ***
  • Posts: 115
    • View Profile
Re: Mouse::setPosition problem with View
« Reply #6 on: October 05, 2015, 09:02:39 pm »
I am confused about object's positions in window, view and screen.

Your player can have any position, like 0x0 or 999999x999999, doesn't matter. Same case with the view, they use "world coordinates".

Your mouse, however, can only be inside your screen, that is, your resolution (1280x1080 or whatever). So, if you use your player position in Mouse::setPosition(), you are using numbers way beyond your screen resolution, and that's bad.

So, you need to transform the coordinates, either using mapCoordsToPixel() or manually:

sf::Vector2f transformedPlayerPosition = window.mapCoordsToPixel( player.getPosition() , view );
sf::Mouse::setPosition( transformedPlayerPosition );
 

(Or something like that. I've never used mapCoordsToPixel() :P)

Anyway, is it really necesary to move your mouse cursor? That seems annoying for the player. You could use another sprite or something instead of the mouse.

monkey3

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Re: Mouse::setPosition problem with View
« Reply #7 on: October 06, 2015, 02:02:46 pm »
Im trying to use mappixeltocoords function but there is promlwm still. Player's position is according to window's left-top corner. But when Im using mappixeltocoords function it gives me coords irrevelant from left-top corner. And I cant calculate righly.

How can I meet these two in same coordinate plane?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Mouse::setPosition problem with View
« Reply #8 on: October 06, 2015, 02:10:45 pm »
player position relative to view + view position = world position
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

monkey3

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Re: Mouse::setPosition problem with View
« Reply #9 on: October 06, 2015, 02:42:58 pm »
Difference between view and view position?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Mouse::setPosition problem with View
« Reply #10 on: October 06, 2015, 03:20:55 pm »
If you're asking questions like these, you should probably spend some more time reading the tutorials and the documentation. I can't tell you more than what's written there...

In case you mean "world position", that's the absolute position of the entity in global world space. That's what you set with sf::Sprite::setPosition(), for example.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

monkey3

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Re: Mouse::setPosition problem with View
« Reply #11 on: October 06, 2015, 04:22:52 pm »
Thank you for trying to help and sorry if I cant explain my problem clear enough.
Now my only need is to know mouse cursor's world position.
Ive read tutorials about views but I have an idea that can do this and it doesnt work right. I have nothing else now.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Mouse::setPosition problem with View
« Reply #12 on: October 06, 2015, 04:31:42 pm »
sf::Vector2f mouse_world_coord = window.mapPixelToCoords(sf::Mouse::getPosition(window));
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

monkey3

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Re: Mouse::setPosition problem with View
« Reply #13 on: October 06, 2015, 04:49:36 pm »
I dont know why but that doesnt work right for me.

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Mouse::setPosition problem with View
« Reply #14 on: October 06, 2015, 05:09:56 pm »
Tried with the view as the second parameter of mapPixelToCoords?