Jump to content
  • Advertisement
Sign in to follow this  
MARS_999

Warp mouse to screen center and take input to allow camera movement

This topic is 2264 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am not sure how to get my mouse cursor to stay at the center of the screen while moving the mouse up and down and taking the input to get the amount moved?


inline void MouseMovement(int x, int y)
{
if(mouseMove)
{
float diffX = static_cast<float>(x - prevX); //check the difference between the current x and the last x position
float diffY = static_cast<float>(y - prevY); //check the difference between the current y and the last y position
prevX = static_cast<float>(x); //set prevX to the current x position
prevY = static_cast<float>(y); //set prevY to the current y position
xRot += diffY; //set the xrot to xrot with the addition of the difference in the y position
yRot += diffX; //set the xrot to yrot with the addition of the difference in the x position
MaxCameraRotationX();
sf::Vector2i screenCenter(sf::VideoMode::getDesktopMode().width * .5f,
sf::VideoMode::getDesktopMode().height * .5f);
sf::Mouse::setPosition(screenCenter);
}
}

Share this post


Link to post
Share on other sites
Advertisement
What's wrong with the code you already have? Conceptually at least that's how it works...

Share this post


Link to post
Share on other sites
Because when I right click and move the mouse it jumps to the center as it should but the scene doesn't move and after I release it jumps to some location depending on what the last value was I am assuming... I am looking for a real time smooth side to side up and down mouse movement that rotates the the scene when the right button is hit...

Share this post


Link to post
Share on other sites
What determines the value of the mouseMove variable? How do you call the function in the first place? It sounds like you have a trivial logic bug someplace in how you invoke the code that was posted.

Share this post


Link to post
Share on other sites
I used another approach in my game because I wasn't happy with the put-mouse-in-center trick (it has some theoretical drawbacks: mouse movements between you reading the cursor position and resetting the mouse to the center are discarded; if the mouse is moved fast enough to exit the window, a click would minimize the game).

In my case, I restricted the mouse cursor's movement to the client area of the game window via [font=courier new,courier,monospace]ClipCursor()[/font] and signed up for [font=courier new,courier,monospace]WM_INPUT[/font] via [font=courier new,courier,monospace]RegisterRawInputDevices()[/font] to receive true mouse deltas.

The code you posted may be forgetting to take the screen center into account. Before calling [font=courier new,courier,monospace]sf::Mouse::setPosition(screenCenter[/font][font=courier new,courier,monospace])[/font], you should probably set [font=courier new,courier,monospace]prevX[/font] and [font=courier new,courier,monospace]prevY[/font] to [font=courier new,courier,monospace]screenCenter.X[/font] and [font=courier new,courier,monospace]screenCenter.Y[/font].

Share this post


Link to post
Share on other sites
Cygon that didn't work either.... Thanks though.

ApochPiQ
In the event loop on MouseMoved event call the MouseMovement() and send in the x,y coordinates from that event. Also I set the mouseMove to true on mouse right click in the same event loop and when mouse button is released I set to false.


sf::Event::MouseMoved:
camera.MouseMovement(evt.mouseMove.x, evt.mouseMove.y);
break;
case sf::Event::MouseButtonPressed:
{
switch(evt.mouseButton.button)
{
case sf::Mouse::Left:
break;
case sf::Mouse::Right:
{
sf::Vector2i screenCenter(static_cast<int>(sf::VideoMode::getDesktopMode().width * .5f),
static_cast<int>(sf::VideoMode::getDesktopMode().height * .5f));
sf::Mouse::setPosition(screenCenter);
camera.SetMouseMove(true);
}
break;
case sf::Mouse::Middle:
break;
default:
break;
}
}
break;


I personally don't see anything wrong with that code...

What I am seeing on the variables in MouseMovement() some are zero for values that have been calculated... Not sure why as they are float and should have some value at least...

Share this post


Link to post
Share on other sites
Well, try simplifying things first. Start by resetting the mouse to the center of the screen every frame, regardless of mouse buttons being pressed. This should result in the cursor rubber-banding back to the center constantly when you try to move it.

Once you get that figured out, you can start computing deltas each frame, and print those out somehow to verify that you're getting it correct. Finally, you can turn back on the mouse-button toggle.

Reducing things to simpler components should hopefully help you track down the bug and get it fixed, and then you can re-add functionality incrementally, giving you the chance to fix any other bugs that might creep in along the way.

Share this post


Link to post
Share on other sites
Yeah it rubberbands back if I do just call the screenCenter and setPosition()....

The deltas are very small like -1 to -7 or 1 to 7 but I have scaled it makes no difference... I am about to scream.... I had code to do this crap a long time ago and can't find it and for some reason I remember some big if/else was needed.... Edited by MARS_999

Share this post


Link to post
Share on other sites
Well, that's progress! That means the bug is centered around your button handling implementation somehow. See if you can trace what's up with that, and you should be on the right track.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!