Infinite Mouse Tracking

I wanted to use the mouse as a simple surface optical encoder to get infinite panning motion. The problem with reading mouse coordinates on the screen, is that once the mouse reaches the edge of the screen, it stops counting. So a simple solution is to reposition the mouse (using the robot class) every few frames and calculate the change in mouse positions.


//InfiniteMouseTracking
//by Che-Wei Wang
//2.20.2008

float mapPositionX;
float mapPositionY;
long count=0;
int moveX=0;
int moveY=0;

void setup() 
{
  size(screen.width,screen.height,P3D);
  mapPositionX=width/2;
  mapPositionY=height/2;

  //noCursor();
  
  //set the mouse postion once before the program begins
  try {
    Robot robot = new Robot();
    robot.mouseMove(width/2, height/2);    
  } 
  catch (AWTException e) {
  }

}

void draw()
{
  background(0);

  //reset the cursor Position every few frames
  if(count%4==0){
    try {
      Robot robot = new Robot();
      robot.mouseMove(width/2, height/2);    
    } 
    catch (AWTException e) {
    }
    moveX=mouseX-pmouseX;
    moveY=mouseY-pmouseY;
  }

  count++;

  //new position= old position + movement * decay
  mapPositionX=mapPositionX+moveX*.8;
  mapPositionY=mapPositionY+moveY*.8;

  stroke(255);
  line(width/2,height/2,mapPositionX,mapPositionY);
  ellipse(mapPositionX,mapPositionY,100,100);

}