[java] Relative vs. Absolute Mouse Motion

Started by
9 comments, last by Palidine 17 years, 10 months ago
Hey everyone. I'm writing a game which uses the standard Java listeners for keyboard & mouse input. I got the keyboard working fine with KeyListener, and the mouse works alright too, but it appears that Java is only capable of giving me absolute coordinates within the component that has the MouseMotionListener attached. This works fine for most games, but for first-person-shooter-like games that would benefit from a mouselook mode it seems like relative coordinates (where the system only reports the change in location) would be necessary. Does anyone know of a way to get relative coordinates in Java without using another library like JInput? I'll learn JInput if I must, it just seems like overkill to use it just for getting relative mouse coords (I don't need to use joysticks or gamepads or anything fancy in my game). Thanks for the help.
Advertisement
You can do something simple like this

//each update
relativePosition = currentPosition - previousPosition;
previousPosition = currentPosition;
return relativePosition;
deathkrushPS3/Xbox360 Graphics Programmer, Mass Media.Completed Projects: Stuntman Ignition (PS3), Saints Row 2 (PS3), Darksiders(PS3, 360)
Thanks for the reply. That snippet works fine the majority of the time, however, it doesn't work when the mouse cursor is at one edge of the screen (let's assume a full-screen window here) because Java always seems to clamp the coordinates to the max or min width or height of the component that's listening to the mouse.

Think about a first-person game with mouselook enabled. No matter how much the player moves the mouse to the left or right, thier view will keep rotating accordingly, even after the "real" mouse cursor has hit the edge of the screen. Is there a way to get this functionality in Java?
Not sure if this is possible with Java. However, under x11 (c++), XWindows reports the mouse the same way. And, to give the appearance of infinite movement, you have to warp the mouse back to center when it reaches the edge (or within a few pixels or the edge). This works well there, perhaps you can do the same with java?
Thanks, that sounds like it would work. However, I looked through the Java APIs but didn't see anything that would let me change the position of the cursor. Of course, the API is more than gigantic, so there's a very good chance that there is a way and I just didn't find it. Anyone know of a way in Java to let the programmer mess with the mouse position?
Look at the java.awt.Robot class. There is a function mouseMove(int x, int y) in it. You could use this along with what deathkrush said to position the mouse to the center of the screen after the calcuations. Then you will avoid reaching the screen bounds.
Hi,

The java.awt.Robot class allows you to physically move the mouse cursor to any location, so you could probably use that to move the cursor back to the center when it hits the edge.

Doesn't sound like a good solution to me though really as depending on screen resolution and mouse sensitivity you'd probably be moving the mouse cursor alot.

You would also need to keep track of the 'warp' as minusing the previous from the current would no longer be giving the correct relative movement.

Anyhoo might be worth checking out.


- Chris
"More computing sins are committed in the name of efficiency than for any other single reason - including blind stupidity" - William Wulf

"Any fool can write code that a computer can understand. Good programmers write code that a human can understand"

Thanks guys! The Robot class is exacly what I need!

Chris, you said it didn't seem like a good solution. I can see needing to move the mouse cursor a lot, but is this really a problem? Is it an inefficent operation? If there are better solutions, I'm open to them too.

Thanks.
I have used the Robot class once for relative mouse motion and as far as I can remeber you have to be carefull because Robot.mouseMove(int,int) causes events, just like the user moving the mouse.
Hi,

I've only used the Robot class for messing around so I can't really give you a good answer.

It just seemed to me if you were running at an average resolution of 800x600 and the user was able to edit the mouse sensitivity you could literally hit the edge of the screen in one movement. Meaning you'd be forced to move cursor everytime the player turned.

Really the best way is just to try and see how it works out, no matter how crappy something may seem if you get the FPS then theres no reason not to use it (assuming it's crappy for performance reasons and not design reasons ;)).


- Chris
"More computing sins are committed in the name of efficiency than for any other single reason - including blind stupidity" - William Wulf

"Any fool can write code that a computer can understand. Good programmers write code that a human can understand"

This topic is closed to new replies.

Advertisement