Keeping mouse cursor stationary

HI guys.

I recently started programming a FPS shooter using JOGL. I've already managed to hide the mouse cursor (otherwise aiming will move the cursor all over the screen). Now in order to register the amount moved by the mouse (must also be able to exceed the desktop),

I need a constant reference point.

In C++ it was easy to constantly set the mouse's position, how can I keep the mouse centered?

Thanx!

[440 byte] By [Climaxa] at [2007-10-2 17:00:22]
# 1
You can easily move or center he mouse (how be it, why would you want to control the mouse and not the mouse control your game) by using Robot. It is an Object included in Java since 1.4
morgalra at 2007-7-13 18:13:52 > top of Java-index,Other Topics,Java Game Development...
# 2

Cool, that might work! thanx. But i've looked at my direct input stuff from c++ and noticed that my mouse cursor doesn't need to stay stationary. The only thing is that when i get the current mouse "state" it returns the amount moved on the axis and not the coordinates.

Any functions/api's in java that can give me the relative movement on the Axi?

Thanx

Climaxa at 2007-7-13 18:13:52 > top of Java-index,Other Topics,Java Game Development...
# 3

there isn't any built in function like that except basic math. the problem is, like you've already said, the desktop size. the way Maya does it (a 3D modeling program) is that it wraps the mouse around the screen while you move it. Kind of like in Asteroids when your ship leaves one side, it shows up on the other. you can do that part with the Robot class, but you need to keep a variable of how many times the mouse has "wrapped" around the screen on both the x and y axis.

for example, if the player is getting frantic and keeps moving the mouse left so as to rotate the player around several times, you're going to be wrapping around the x-axis for sure. so have a variable, say `xwrap` which keeps track of how many times the user has looped around the x axis (and is initially set to 0). if the player keeps moving his mouse towards the left, when it comes to the very left, you need to subtract 1 from `xwrap` and place the mouse with Robot to the RIGHT side. so, using pseudo code...

if (mouse_x <= 0) {

xwrap--;

mouse_x = SCREEN_WIDTH+mouse_x;

}

if the situation is the opposite, and the mouse is moving towards the right, you need to increase xwrap when the mouse hits SCREEN_WIDTH

if (mouse_x >= SCREEN_WIDTH) {

xwrap++;

mouse_x = SCREEN_WIDTH-mouse_x;

}

of course, all of this applies to the y-axis in the very same way. up is negative, down is positive.

now this brings us back to the original point: the offset of the mouse. with your `wrap` variables in place, simple math will save you: (note: CENTER_X is the center x point on the screen. you should manually set the mouse to the center of the screen with the Robot class when the game starts - this will be the reference point)

offset_x = CENTER_X+SCREEN_WIDTH*xwrap+mouse_x;

offset_y = CENTER_Y+SCREEN_HEIGHT*ywrap+mouse_y;

this code will work fine as long as your player isn't REALLY looping around the screen as often as I was saying (in an average game you probably won't leave the screen more than 3 or 4 times since you typically move the mouse BACK after you move it FORWARD [e.g. he will always want to shoot straight anyway]). if you seriously left the screen 5, 6, or 10 times or whatever, your offset will be HUGE, although it will still be accurate.

one final note.. this code will only work for ABSOLUTE positioning. what I mean by absolute is that "the reason the character is facing this direction is because this is where the mouse moved from the original point." it won't work for games that constantly keep rotating the player little by little based on how far away the mouse is from the original point.

Woogleya at 2007-7-13 18:13:52 > top of Java-index,Other Topics,Java Game Development...
# 4
I found time to write up a quick example. run the example here: http://woogley.net/misc/MouseWrap(press any key to exit)
Woogleya at 2007-7-13 18:13:52 > top of Java-index,Other Topics,Java Game Development...
# 5

Thanx alot! This helped me immensely! it's a bit of cpu overhead, but hey, it works! I also got a reference to the JInput api. It implements a method which directly gives absolute movements on the axi. So no wrapping is required nor any form of stationary mouse pointers!

I'm also experimenting with the lwjgl api, which includes implementations of OpenGL, OpenAL, DevIL(OpenIL), JInput and more...

Thanx alot!

Climaxa at 2007-7-13 18:13:52 > top of Java-index,Other Topics,Java Game Development...
# 6
Thanx for the example! It's exactly what I need! Thanx for taking the time!
Climaxa at 2007-7-13 18:13:52 > top of Java-index,Other Topics,Java Game Development...
# 7
Doesn't work on multi-monitor systems ;)You are assuming the origin of a maximized Frame is 0,0 - which is not always the case when you have a virtual desktop spanning several monitors.
Abusea at 2007-7-13 18:13:52 > top of Java-index,Other Topics,Java Game Development...
# 8
of course, Abuse with a technicality that doesnt need to be applied to such tiny examples! (without offering a solution, also. but hey..)anyway, Abuse's mentionings are easily remedied with a call to getLocation() isntead of assuming 0,0
Woogleya at 2007-7-13 18:13:52 > top of Java-index,Other Topics,Java Game Development...
# 9
calm down, calm down! =DI wasn't criticizing your code, just highlighting that the poster shouldn't copy your example line for line, as it won't (in its current state) work in all situations.
Abusea at 2007-7-13 18:13:52 > top of Java-index,Other Topics,Java Game Development...
# 10
oh, it's cool dude, i wasn't taking it as a stab. (i've known you long enough). i guess I lacked emoticons in my last message ;)
Woogleya at 2007-7-13 18:13:52 > top of Java-index,Other Topics,Java Game Development...