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]

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.
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!