Sign in to follow this  

SDL mouse warp and relative state

This topic is 2791 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

hey guys, im having some problems with my mouse behavior. im building a little airhockey game and im trying to control the player bat with the mouse. this is working fine, however as soon as the mouse leaves the window then the position is at 0, 0 and it doesnt help me very much. im not too keen on locking the mouse to my game window, so i've decided to go with a hide mouse cursor & warp to the center of the screen method, however my codes now playing up and im having trouble trying to debug it. my code is: void update(float delta) { // ..... SDL_GetRelativeMouseState(&mouseX, &mouseY); sprites[i]->setVel((Vector(mouseX, mouseY) / delta) * 1000); // moves my sprite at the same velocity as the mouse SDL_WarpMouse(400, 300); // reposition mouse at the center of the screen // ..... } now the problem is that when i warp the mouse, the sprite moves in the same direction. so eg, the mouse is in the center, i move it quickly to the far right, the sprite will move to the far right, the mouse warps back to center, the sprite moves quickly back to center. i know it should be an easy solution, but i've never really done anything like this...anyone have any ideas? cheers

Share this post


Link to post
Share on other sites
for any who are interested, the method for doing this without locking the mouse to the screen and hiding the cursor is to get the actual mouse position and subtract the warping position from it to get the displacement, change the velocity, warp the mouse back to the warping position.

SDL_GetMouseState(&mouseX, &mouseY);
sprites[i]->setVel((Vector(mouseX - (WINDOW_WIDTH / 2), mouseY - (WINDOW_HEIGHT / 2)) / delta) * 1000); // Get displacement from warp position
SDL_WarpMouse(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2);

/thread

Share this post


Link to post
Share on other sites

This topic is 2791 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