Game Development Community

Mouse-based game?

by Vern Jensen · in Torque Game Builder · 06/21/2006 (1:01 pm) · 8 replies

I'm starting to prototype a space game where a gun is at the bottom center of the screen, and its rotation is controlled by the mouse. As you move the mouse left, the gun rotates left, and as you move the mouse right, it rotates right.

This all works great so far, using the onMouseMove() and onMouseDragged() callbacks, with just one problem: if the player moves the mouse outside of the window, these callbacks are NOT called. I would like them to continue being called, and I simply detect that the mouse is outside of my window and convert any position returned by the callbacks into the nearest on-screen position. Is that possible?

Or perhaps it's impossible to make the type of game I'm making be able to be run from within a window -- perhaps it MUST be fullscreen? Because I also want to hide the mouse cursor AND make it so that if the user clicks while outside of the window, nothing happens... They'd have to push Esc to show the cursor and be able to click outside the game's window. I know that's kinda non-standard for a windowed game, but I do want to try to give the player a Windowed option that works if they want it.

I do know of one method that would probably work: use setMousePosition every time the mouse is moved, to move the mouse back to the center of the game's window. Then I use the offset, rather than its location, to determine how to rotate my ship. This would probably work fine, except I'm wary of using setMousePosition in a way that's so crucial to how the game runs like this, since I think on some OSes it's kind of a hack to move the mouse's physical position, and is often discouraged by the OS's documentation.

Anyway, just wondering if anyone has any advice one way or another...

#1
06/21/2006 (1:18 pm)
Try:
t2dSceneWindow.setLockMouse(true);
#2
06/21/2006 (2:04 pm)
Thanks for the tip. I changed that to:

sceneWindow2D.setLockMouse(true);

That gets rid of run-time errors, but it still doesn't work. I even added:

echo( sceneWindow2D.getLockMouse() );

which echos 1 to the console. But when I move the mouse outside the window, my ship stops rotating. It freezes until the mouse is back inside the window.

Perhaps it's a Mac-specific bug? I'll report it in the bugs forum.

-Vern
#3
06/21/2006 (2:21 pm)
When you move the mouse outside of your application window, the operating system no longer sends mouse events to that application, but to other applications instead. In short, the best you could do would be to lock the firing position at the last known mouse position when the cursor exited the window, and wait for it to come back in so that your application starts getting mouse events again.
#4
06/21/2006 (3:12 pm)
In TGE, doesn't locking the mouse keep it inside the window? I presume it would set the position to the middle of the screen after each frame, which would cause problems for getMousePosition. The thing that I think needs to happen is to fix (or provide a working example if it's not broken) of the actionMap.bind(mouse, blah, blah) stuff.

I came up with a way to manually get this functionality using sceneWindow2D.setMousePosition(), which is documented on TDN, but I'll bet several people will be confused when they find out this isn't standard functionality.
#5
06/22/2006 (1:14 pm)
Unfortunately, I get a delay of about half a second after most calls to sceneWindow2D.setMousePosition(), and ALL mouse movement during that delay gets "lost". (See a thread about this in the Report Bugs forum.) Otherwise, I would be able to use a onMouseLeave() callback to change the mouse position any time the mouse leaves the window. This works great, except for the delay when setMousePosition() is called (and resulting lost mouse movement during that delay), making the solution inadequate.
#6
06/22/2006 (1:39 pm)
Guess what? I found the solution, thanks to helpful posts from the other forum. The solution is to use

lockMouse(true);

It's as simple as that! This keeps the mouse inside the window, and doesn't have any delays associated with it. Beautiful!
#7
06/22/2006 (1:41 pm)
I would just like to know, are you also able to use the onMouseMove stuff with this? If so, then this is a decent solution.
#8
06/22/2006 (7:03 pm)
Yes. I use onMouseMove() and onMouseDragged(), since the button makes my bullet shoot. Everything is now working great. :-)