I have an SDL2 program which uses mouse motion events event.motion.xrel
and event.motion.yrel
to rotate the camera. When I use it on the desktop it is perfectly fine. However, when I compile for emscripten it is very sensitive. Later, I noticed that xrel
and yrel
were the distance of the mouse pointer from the centre of the window every frame. So it sent the distance of the pointer from the centre of the window to relative movement, every time I moved the mouse.
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
. . .
case SDL_MOUSEMOTION:
printf("%d\n", event.motion.xrel);
SDL_WarpMouseInWindow(window, width/2, height/2);
break;
}
At first, I though it was a bug in SDL so I started looking in the source code. I couldn't find a problem, but started to write an issue. I also wrote a minimal example to show the problem, but when I tested the example it worked without any bugs. I then figured that SDL_WarpMouseInWindow()
created a problem because it was not actually moving the mouse on the web (which I knew but didn't think of it making a mouse motion event).
The SDL_WarpMouseInWindow
shouldn't be used like that and instead there may be an #ifdef
for emscripten or (which I haven't tested) SDL_SetRelativeMouseMode()
.
Calling SDL_WarpMouseInWindow() with emscripten doesn't do anything but setting .motion.xrel and .motion.yrel and because the effects don't cancel out the movement is dependent on the distance from the center of the window. Also, this isn't immediately obvious because it still only calls it when there is a SDL_MOUSEMOTION event.
Use SDL_RelativeMouseMode() instead