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

Started by
10 comments, last by MARS_999 11 years, 11 months ago
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);
}
}

Advertisement
What's wrong with the code you already have? Conceptually at least that's how it works...

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

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...
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.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

I will post back later tonight...
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].
Professional C++ and .NET developer trying to break into indie game development.
Follow my progress: http://blog.nuclex-games.com/ or Twitter - Topics: Ogre3D, Blender, game architecture tips & code snippets.
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...
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.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

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....
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.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

This topic is closed to new replies.

Advertisement