# glfwSetMousePos() weirdness?

**URL:** <https://discourse.glfw.org/t/glfwsetmousepos-weirdness/459>\
**Category:** support\
**Created:** [January 15, 2007, 3:21pm UTC](https://discourse.glfw.org/t/glfwsetmousepos-weirdness/459 "2007-01-15T15:21:50Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/glfw/original/1X/b8cdefe6b61b8d5aee6c43b2c56e9cf5eabecfe4.svg) [@system](https://discourse.glfw.org/u/system)\
**Post date:** [January 15, 2007, 3:21pm UTC](https://discourse.glfw.org/t/glfwsetmousepos-weirdness/459/1 "2007-01-15T15:21:50Z")

</div>

**[sporkstorms](https://sourceforge.net/u/sporkstorms/)** wrote on [Monday, January 15, 2007](https://sourceforge.net/p/glfw/discussion/247562/thread/a9446063/#d49c):

Let’s consider the following simplifed pseudo-code. The purpose is to detect relative movements of the mouse - ie, I’m interested in knowing delta movements.

void called\_with\_each\_draw() {  
mid\_x = screen\_width / 2;  
mid\_y = screen\_height / 2;  
glfwGetMousePos(&x, &y);  
// Don’t do anything if mouse hasn’t moved  
if (x == mid\_x && y == mid\_y) { return; }

```
delta\_x = mid\_x + x;
delta\_y = mid\_y - y;

move\_camera\_or\_do\_whatever\(delta\_x, delta\_y\);

// Now move the mouse back to the middle, because
// we don't care where it really is, just how much 
// it moves.
glfwSetMousePos\(mid\_x, mid\_y\);

```

}

Now I expect this to give me a small value for delta\_x if i move the mouse slowly and slightly to the side, and a large value if I jerk the mouse quickly.

It does not work like this, hower. You can move it and initially get something like my expectations, and then (also as I’d expect) while keeping the mouse still you get 0s for the deltas.  
The weirdness comes in when you move the mouse again. At this point is seems to remember what the last position was. Rather than moving from the mid\_x and mid\_y with which we called glfwSetMousePos(), it moves from the value that gave us those first deltas.

Whenever the mouse isn’t moving you get, as expected, zeros for the deltas (ie, getMousePos is giving the thing you last set with setMousePos). But as soon as you move the mouse, its position is suddenly tracking from its old location before you called setMousePos.

Why does this happen? Is it intentional, and if so, how can I get the behavior I’m looking for?

If it isn’t intentional, here is some further info in case it’s helpful: This behavior appears to happen in fullscreen mode whether you disabled GLFW\_MOUSE\_CURSOR or not. In windowed mode it happens if you disable the cursor. In windowed mode with the cursor enabled, I’m not entirely sure how it behaves (i still have to work out how to test it because the mouse coordinates appear to be different and the setMousePos is throwing my cursor out of the window before i can figure out what’s going on).

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/glfw/original/1X/b8cdefe6b61b8d5aee6c43b2c56e9cf5eabecfe4.svg) [@system](https://discourse.glfw.org/u/system)\
**Post date:** [January 15, 2007, 3:31pm UTC](https://discourse.glfw.org/t/glfwsetmousepos-weirdness/459/2 "2007-01-15T15:31:06Z")

</div>

**[sporkstorms](https://sourceforge.net/u/sporkstorms/)** wrote on [Monday, January 15, 2007](https://sourceforge.net/p/glfw/discussion/247562/thread/a9446063/#b847):

This is on OS X, in case that’s relevant (since I’d guess carbon is used for mouse stuff)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/glfw/original/1X/b8cdefe6b61b8d5aee6c43b2c56e9cf5eabecfe4.svg) [@system](https://discourse.glfw.org/u/system)\
**Post date:** [March 22, 2007, 11:37am UTC](https://discourse.glfw.org/t/glfwsetmousepos-weirdness/459/3 "2007-03-22T11:37:23Z")

</div>

**[elmindreda](https://sourceforge.net/u/elmindreda/)** wrote on [Thursday, March 22, 2007](https://sourceforge.net/p/glfw/discussion/247562/thread/a9446063/#8343):

Bug confirmed and being worked on.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/glfw/original/1X/b8cdefe6b61b8d5aee6c43b2c56e9cf5eabecfe4.svg) [@system](https://discourse.glfw.org/u/system)\
**Post date:** [March 23, 2007, 11:20pm UTC](https://discourse.glfw.org/t/glfwsetmousepos-weirdness/459/4 "2007-03-23T23:20:45Z")

</div>

**[elmindreda](https://sourceforge.net/u/elmindreda/)** wrote on [Friday, March 23, 2007](https://sourceforge.net/p/glfw/discussion/247562/thread/a9446063/#8343/f912):

This bug is now fixed. Thank you.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/glfw/original/1X/b8cdefe6b61b8d5aee6c43b2c56e9cf5eabecfe4.svg) [@system](https://discourse.glfw.org/u/system)\
**Post date:** [April 25, 2007, 1:25pm UTC](https://discourse.glfw.org/t/glfwsetmousepos-weirdness/459/5 "2007-04-25T13:25:18Z")

</div>

**nobody** wrote on [Wednesday, April 25, 2007](https://sourceforge.net/p/glfw/discussion/247562/thread/a9446063/#7889):

Which version was this fixed in?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/glfw/original/1X/b8cdefe6b61b8d5aee6c43b2c56e9cf5eabecfe4.svg) [@system](https://discourse.glfw.org/u/system)\
**Post date:** [April 25, 2007, 8:41pm UTC](https://discourse.glfw.org/t/glfwsetmousepos-weirdness/459/6 "2007-04-25T20:41:11Z")

</div>

**[elmindreda](https://sourceforge.net/u/elmindreda/)** wrote on [Wednesday, April 25, 2007](https://sourceforge.net/p/glfw/discussion/247562/thread/a9446063/#c0b5):

It is fixed in CVS but not in any release (yet).

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/glfw/original/1X/b8cdefe6b61b8d5aee6c43b2c56e9cf5eabecfe4.svg) [@system](https://discourse.glfw.org/u/system)\
**Post date:** [March 21, 2008, 1:04pm UTC](https://discourse.glfw.org/t/glfwsetmousepos-weirdness/459/7 "2008-03-21T13:04:27Z")

</div>

**[ttk-bandit](https://sourceforge.net/u/ttk-bandit/)** wrote on [Friday, March 21, 2008](https://sourceforge.net/p/glfw/discussion/247562/thread/a9446063/#6790):

I get similar problems on windows.  
using mouseposcallback, but it gives me very strange values.  
for example:  
After I drag the mouse to the left a few times, my cam angle spins up very fast to the left even though I only move the mouse slowly.  
And when I then try to move right, it keeps turning left, getting slower over time until then finally turning right and speeding up again.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/glfw/original/1X/b8cdefe6b61b8d5aee6c43b2c56e9cf5eabecfe4.svg) [@system](https://discourse.glfw.org/u/system)\
**Post date:** [March 21, 2008, 6:47pm UTC](https://discourse.glfw.org/t/glfwsetmousepos-weirdness/459/8 "2008-03-21T18:47:28Z")

</div>

**[ttk-bandit](https://sourceforge.net/u/ttk-bandit/)** wrote on [Friday, March 21, 2008](https://sourceforge.net/p/glfw/discussion/247562/thread/a9446063/#d49c/979b):

got it fixed, here is what I changed (based on version 2.6):

starting changes in the middle of \_glfwWindowCallback:  
…  
// Did the mouse move?  
case WM\_MOUSEMOVE:  
{  
if ( \_glfwWin.Active ) // add this… (so there wont be any mouse events on an inactive window when the mouse hovers over your opengl window)  
…

#if 0 // change this  
else  
{  
\_glfwInput.MousePosX = NewMouseX;  
\_glfwInput.MousePosY = NewMouseY;  
}  
\_glfwInput.OldMouseX = NewMouseX;  
\_glfwInput.OldMouseY = NewMouseY;  
#else // to this:  
else  
{  
\_glfwInput.OldMouseX = \_glfwInput.MousePosX = NewMouseX;  
\_glfwInput.OldMouseY = \_glfwInput.MousePosY = NewMouseY;  
}  
#endif

… // at the end of \_glfwCreateWindow, add this:

```
\_glfwInput.MouseMoved = GL\_FALSE;

```

… // at the beginning of \_glfwPlatformPollEvents, remove these 3 lines:

```
// Flag: mouse was not moved \(will be changed by \_glfwGetNextEvent if
// there was a mouse move event\)
\_glfwInput.MouseMoved = GL\_FALSE;

```

… // and later in this function:

```
// Did we have mouse movement in locked cursor mode?
if\( \_glfwInput.MouseMoved &amp;&amp; \_glfwWin.MouseLock \)
\{
    \_glfwPlatformSetMouseCursorPos\( \_glfwWin.Width / 2,
                                    \_glfwWin.Height / 2 \);
    \_glfwInput.MouseMoved = GL\_FALSE; // Add this\!
\}
```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/glfw/original/1X/b8cdefe6b61b8d5aee6c43b2c56e9cf5eabecfe4.svg) [@system](https://discourse.glfw.org/u/system)\
**Post date:** [March 21, 2008, 10:30pm UTC](https://discourse.glfw.org/t/glfwsetmousepos-weirdness/459/9 "2008-03-21T22:30:15Z")

</div>

**[elmindreda](https://sourceforge.net/u/elmindreda/)** wrote on [Friday, March 21, 2008](https://sourceforge.net/p/glfw/discussion/247562/thread/a9446063/#d49c/979b/993e):

Please submit a patch for this to the patch tracker and I’ll review it.
