# GLUT - Get Mouse Coordinates (w/ movement)

This topic is 2461 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi. I'm making a tank game where you can move your mouse around the tank and the barrel rotates to point to wherever your mouse is. The only problem is, the tank can move around and thus the barrel needs to reposition itself to point to wherever the cursor NOW is relative to the tanks position. In other words, I can leave the mouse stationary, drive the tank around the mouse pointer, and the barrel should rotate to always point at it. However, it will only readjust whenever I move the mouse since I'm using glutMotionFunc and glutPassiveMotionFunc. How can I get the mouse coordinates at any time without the need for a callback function so that I can have real-time mouse coordinates?

##### Share on other sites

Hi. I'm making a tank game where you can move your mouse around the tank and the barrel rotates to point to wherever your mouse is. The only problem is, the tank can move around and thus the barrel needs to reposition itself to point to wherever the cursor NOW is relative to the tanks position. In other words, I can leave the mouse stationary, drive the tank around the mouse pointer, and the barrel should rotate to always point at it. However, it will only readjust whenever I move the mouse since I'm using glutMotionFunc and glutPassiveMotionFunc. How can I get the mouse coordinates at any time without the need for a callback function so that I can have real-time mouse coordinates?

I don't use glut but from what I can tell glutPassiveMotionFunc receives the mouse position whenever the mouse moves, so you can simple store it there to a global variable. The changes to this global variable will be in real time. I don't think there will be any threading issues if you only access the variable from the main thread. If you need it in other threads then you need to set and get it in a thread safe manner so it would be neater to wrap it into a class like:
 class MouseCoords { void set(const Vector2i& newPos) { ScopedLock lockThisFunctionScope(lock); pos = newPos; } Vector2i get() const { ScopedLock lockThisFunctionScope(lock); return pos; } private: Vector2i pos; Mutex lock; }