Sign in to follow this  

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

This topic is 2049 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?

[code]
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);
}
}

[/code]

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

[code]
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;
[/code]

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
I have no idea why this code works... but it works but not as smooth as I would like? And it seems like a very bad hack....

[code]
if(mouseMove)
{
const int BUFFERZONE = 35;
float x = static_cast<float>(sf::Mouse::getPosition().x);
float y = static_cast<float>(sf::Mouse::getPosition().y);
sf::Vector2i screenCenter(static_cast<int>(sf::VideoMode::getDesktopMode().width * .5f),
static_cast<int>(sf::VideoMode::getDesktopMode().height * .5f));
float deltaX = x - prevX;
float deltaY = y - prevY;
prevX = x;
prevY = y;
if(x > (screenCenter.x + BUFFERZONE) || x < (screenCenter.x - BUFFERZONE) ||
y > (screenCenter.y + BUFFERZONE) || y < (screenCenter.y - BUFFERZONE))
{
xRot += deltaY;
yRot += deltaX;
MaxCameraRotationX();
sf::Mouse::setPosition(screenCenter);
}
}
[/code] Edited by MARS_999

Share this post


Link to post
Share on other sites
FINALLY!!! This code works like a million dollars!!!

Thanks! for all the suggestions...

[code]
inline void MouseMovement(void)
{
if(mouseMove)
{
static sf::Vector2i screenCenter(static_cast<int>(sf::VideoMode::getDesktopMode().width * .5f),
static_cast<int>(sf::VideoMode::getDesktopMode().height * .5f));
float x = static_cast<float>(sf::Mouse::getPosition().x);
float y = static_cast<float>(sf::Mouse::getPosition().y);
if(prevX != x || prevY != y)
{
float deltaX = x - screenCenter.x;
float deltaY = y - screenCenter.y;
xRot += deltaY;
yRot += deltaX;
MaxCameraRotationX();
sf::Mouse::setPosition(screenCenter);
prevX = x;
prevY = y;
}
}
}
[/code]

Share this post


Link to post
Share on other sites

This topic is 2049 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this