mouseX and mouseY not updating even when mouse is still in window

I’ve been making an arras.io (a top down shooter) type game, and I’m trying to make the player (which is in the centre)'s cannon(s) face the cursor, using atan2() and the rotate() function. It does work as expected, but sometimes the cannon just freezes and doesn’t move at all, sometimes resolving itself and continuing as normal, or just staying there. This is mostly random and I don’t have a clue as to what causes it.

void setup() {
	fullScreen();
}

void draw() {
	fullScreen();

	pushMatrix();

	strokeWeight(5);

	translate(width/2, height/2);
	render(atan2((mouseY - height / 2), (mouseX - width / 2)));

	popMatrix();
}

void render(float _rot) {
	pushMatrix();

	rotate(_rot);

	fill(160);
	stroke(lerpColor(160, color(0), 0.1));

	beginShape();
	vertex(0, -5);
	vertex(20, -5);
	vertex(20, 5);
	vertex(0, 5);
	endShape(CLOSE);

	popMatrix();
}

I’m making this sketch in OpenProcessing. I know that mouseX and mouseY don’t update when the cursor is off the window, but when this happens the mouse has been in the window the whole time the sketch was being run.

Is this just a Processing thing? Or is this possible to fix? Please let me know.

I tried your code in Processing IDE and it worked fine. Just a copy of points

  1. Remove the fullScreen(); from the draw() method as it is not needed and shouldn’t be there
  2. Add a background statement at the beginning of draw to erase previous cannon positions.

Mouse events are sent to the active application if you click the mouse outside OpenProcessing window then your sketch will not respond to the mouse even if the mouse cursor is over your sketch window.

1 Like