Sign in to follow this  
Master Jake

GLUT - Get Mouse Coordinates (w/ movement)

Recommended Posts

Master Jake    100
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 this post


Link to post
Share on other sites
bluntman    255
[quote name='Master Jake' timestamp='1306505776' post='4816433']
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?
[/quote]

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:
[code]

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;
}
[/code]

Share this post


Link to post
Share on other sites

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