SDL_SetRelativeMouseMode problem

Started by
4 comments, last by Palidine 9 years, 2 months ago

Hey,

I've been googling around for the better part of an hour and still have the same problem.

I am trying to hide the mouse on right-click and get continuous mouse movement data (to orbit a 3rd person camera continuously). SDL_SetRelativeMouseMode seems like definitely the right tool for the job. The problem is that it's still acting like the mouse is constrained by the window and when it "gets to the edge" of the window, my x and y deltas go to zero. IIRC that's not expected behavior. I've tried also using SDL_SetWindowGrab( SDL_TRUE ) which should have the additional feature of continuing to give me x/y delta information even if the mouse is pinned to the window edge, but that's not happening either.

I've also tried SDL_WarpMouseInWindow, but that has odd behavior as the call invokes another SDL_MOUSEMOTION event that undoes the relative mouse motion that just happened. If I try to fix that by wrapping the call with SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE); and then re-enabling that just makes the whole mouse event system take a big dump and give me really crappy choppy data...

Maybe a problem with: mouse driver, dual monitor setup, SDL_WINDOW_BORDERLESS??

If anyone has any ideas this is a pretty basic feature and it's suuuuper weird that I can't get it to work. I mean it's the basis of getting an FPS working correctly on PC, so should be pretty simple to get working

[EDIT: oh, if it matters, I'm using SDL2-2.0.1]

Advertisement

The problem is that it's still acting like the mouse is constrained by the window and when it "gets to the edge" of the window, my x and y deltas go to zero. IIRC that's not expected behavior.

It does seem like expected behavior: https://wiki.libsdl.org/SDL_SetWindowGrab states "When input is grabbed the mouse is confined to the window."

Not sure as to which SDL method would help. I'll keep looking for you.

It does seem like expected behavior: https://wiki.libsdl.org/SDL_SetWindowGrab states "When input is grabbed the mouse is confined to the window."

Sorry for the very late reply...

It is expected that it's confined to the window, but SDL_SetWindowGrab is supposed to keep generating mouse movement deltas even when it is confined along the edge of the window. So the user wouldn't see the mouse moving to the right outside of the window but your code would still get a deltaX in a rightwards direction. Anyway, this is still bogging me down so if you or anyone else has additional ideas, let me know!

I'll post if/when I eventually do find a solution, but as I said it's weird that I'm having problems here. I must be missing something kind of obvious...

Have you checked to make sure SDL_SetRelativeMouseMode() isn't returning -1?

ok, well I solved it but it's jenky. There's got to be a better "built in" way.

First I hide the mouse and call SDL_SetWindowGrab so that the mouse will not move outside of the window.


void LockMouse( bool lock )
{
	SDL_ShowCursor( (lock)? SDL_DISABLE : SDL_ENABLE );

	SDL_bool b = (lock)? SDL_TRUE: SDL_FALSE;
	SDL_SetWindowGrab( mainWindow, b );

	if ( lock )
	{
		mouseLock[0] = mouseInfo.xCur;
		mouseLock[1] = mouseInfo.yCur;
	}
}

Then, whenever I receive the mouse movement events I do the following to ignore the movement event caused by calling SDL_WarpMouseInWindow:


int32_t deltaW = mouseInfo.xCur;
deltaW -= mouseInfo.xPrev;
deltaW -= mouseDeltaAccum[0];

int32_t deltaH = mouseInfo.yCur;
deltaH -= mouseInfo.yPrev;
deltaH -= mouseDeltaAccum[1];

HandleMouseMovement( deltaW, deltaH );

//warp back to the position where the mouse is "locked"
//and add that movement into the accumulator so it can be ignored later
SDL_WarpMouseInWindow( mainWindow, mouseLock[0], mouseLock[1] );
mouseDeltaAccum[0] = mouseLock[0] - mouseInfo.xCur;
mouseDeltaAccum[1] = mouseLock[1] - mouseInfo.yCur;

Have you checked to make sure SDL_SetRelativeMouseMode() isn't returning -1?

It's returning zero

This topic is closed to new replies.

Advertisement