SFML community forums

Help => General => Topic started by: monkey3 on October 04, 2015, 03:07:51 pm

Title: Mouse::setPosition problem with View
Post by: monkey3 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.
Title: Re: Mouse::setPosition problem with View
Post by: eXpl0it3r 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.
Title: Re: Mouse::setPosition problem with View
Post by: SpectreNectar 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.
Title: Re: Mouse::setPosition problem with View
Post by: monkey3 on October 05, 2015, 06:37:46 pm
@SpectreNectar Thanks, I know that.

@eXpl0it3r Can you explain detailed?
Title: Re: Mouse::setPosition problem with View
Post by: monkey3 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.
Title: Re: Mouse::setPosition problem with View
Post by: eXpl0it3r 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?
Title: Re: Mouse::setPosition problem with View
Post by: AFS 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.
Title: Re: Mouse::setPosition problem with View
Post by: monkey3 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?
Title: Re: Mouse::setPosition problem with View
Post by: Nexus on October 06, 2015, 02:10:45 pm
player position relative to view + view position = world position
Title: Re: Mouse::setPosition problem with View
Post by: monkey3 on October 06, 2015, 02:42:58 pm
Difference between view and view position?
Title: Re: Mouse::setPosition problem with View
Post by: Nexus 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.
Title: Re: Mouse::setPosition problem with View
Post by: monkey3 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.
Title: Re: Mouse::setPosition problem with View
Post by: eXpl0it3r on October 06, 2015, 04:31:42 pm
sf::Vector2f mouse_world_coord = window.mapPixelToCoords(sf::Mouse::getPosition(window));
Title: Re: Mouse::setPosition problem with View
Post by: monkey3 on October 06, 2015, 04:49:36 pm
I dont know why but that doesnt work right for me.
Title: Re: Mouse::setPosition problem with View
Post by: G. on October 06, 2015, 05:09:56 pm
Tried with the view as the second parameter of mapPixelToCoords (http://www.sfml-dev.org/documentation/2.3.2/classsf_1_1RenderTarget.php#a46eb08f775dd1420d6207ea87dde6e54)?
Title: Re: Mouse::setPosition problem with View
Post by: monkey3 on October 06, 2015, 05:20:35 pm
Yes I did.
Title: Re: Mouse::setPosition problem with View
Post by: monkey3 on October 07, 2015, 02:30:57 pm
Hi again.
Ive prepeared a visual for problem.

(http://oi60.tinypic.com/24o64b7.jpg)

The problem is object.position != Window.mapPixelToCoords(Mouse::getPosition(Window),Window.getView())

Or is this a problem?

How can I make this equality true?
Title: Re: Mouse::setPosition problem with View
Post by: zsbzsb on October 07, 2015, 03:06:55 pm
Write a complete and minimal (http://en.sfml-dev.org/forums/index.php?topic=5559.msg36368#msg36368) code example that shows the problem so we can all test it.
Title: Re: Mouse::setPosition problem with View
Post by: monkey3 on October 07, 2015, 07:29:31 pm
Very good idea. I write a minimal code and it worked fine. And then I looked my main code confidently and I noticed problem's source. Maybe. Actually I dont understand exactly why it did not work but I make little changes then it worked.

Thanks for everyone's attention.