Sign in to follow this  

[java] Relative vs. Absolute Mouse Motion

This topic is 4195 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
You can do something simple like this

//each update
relativePosition = currentPosition - previousPosition;
previousPosition = currentPosition;
return relativePosition;

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Quote:
Original post by Aikavanak
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?


the "real" cursor never reaches the edge of the screen because after you check the relative movement you set the cursor to the center of the screen.

-me

Share this post


Link to post
Share on other sites

This topic is 4195 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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