Reading mouse deltas instead of absolute positions

Almost every contemporary game using a 3D environment uses the mouse as a controller for the view direction. Now I finally have my 3D environment, but can't control the view properly, since MouseMotionEvents only give info about the absolute position of the mouse. As long as the mouse pointer is in the center area of the screen, there is no problem with calculating the necessary deltas myself, but as soon as the pointer reaches the right edge of the screen, moving the mouse to the right does not have any effect (same is true for the other edges, of course).

I already tried a little hack that involved a java.awt.Robot, that moved the pointer from one edge to the opposite edge and vice versa, but that is far from optimal.

Any ideas on how to obtain mouse deltas regardless of the actual position of the pointer on screen? Preferrably something that does not involve JNI... =)

[903 byte] By [Fleischer] at [2007-9-27 21:48:13]
# 1

// somewhere in your class

int lastMouseX, lastMouseY;

// when processing your mouseEvent for movement

deltaX = e.getX() - lastMouseX;

deltaY = e.getY() - lastMouseY;

handleMouseDeltas(deltaX, deltaY);

lastMouseX = e.getX();

lastMouseY = e.getY();

markuskidd at 2007-7-7 3:47:26 > top of Java-index,Other Topics,Java Game Development...
# 2

Thanks for your reply, but that solution does not solve my problem. Imagine the following case in a fullscreen application with a resolution of 1024x768:

-move mouse from position [1004, 302] to [1024, 317]

deltaX: 20

deltaY: 15

everything is fine...

-move mouse (physically) the same distance into the same direction

deltaX: 0 (since the pointer clings to the right edge now)

deltaY: 15 (correct again, but only until the pointer hits the lower edge)

So, if you imagine a game like Quake, Halflife, Unreal, whatever... you would stop turning to the right just because you ran out of pixels on the right side.

This case will get even more complicated for windowed applications, since the mouse pointer has to hover over your application window in order to trigger MouseMotionEvents.

Fleischer at 2007-7-7 3:47:26 > top of Java-index,Other Topics,Java Game Development...
# 3
I've done similar things in C++, and I solved the problem by reseting the mouse cursor to the center of the screen after each frame. I don't know what the command to do that is in java, however.
colmmcsky at 2007-7-7 3:47:26 > top of Java-index,Other Topics,Java Game Development...