Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Processing how to translate mouse coordinates

Tags:

processing

I am making a 2d plat former esc game and I have my game translate my characters x and y cords so my character is always in the middle of the screen, it seems however that mouseX and mouseY do not translate... how would I convert the mouseX and mouseY cords?

here is my translation code

     void draw() {
      background(100);
      if (updateBlocks == true) {
        updateBlocks();
      }
    
      pushMatrix();
      translate(-player.location.x + 320, -player.location.y + 320);
      mx = mouseX -player.location.x + 320;
      my = mouseY -player.location.y + 320;
      for(int a = 0; a < mapWidth; a ++) {
       for(int b  = 0; b < mapHeight; b ++) {
        if(mx >= 16 * a && mx <= 16 * a + 16 && my >= 16 * b && my <= 16 * b + 16) {
         map[a][b] = 1;
         updateBlocks();
         break;
        }
       } 
      }
      for (int a = validBlocks.size()-1; a >= 0; a --) {
        PVector validBlock = validBlocks.get(a);
        rect(validBlock.x, validBlock.y, 16, 16);
      }
      player.update();
      player.display();
      popMatrix();
    }
like image 910
Mark9135 Avatar asked Sep 07 '25 04:09

Mark9135


1 Answers

Yes, mouseX and mouseY are in terms of your window, regardless of your transformation matrix (translate, rotate, etc). (0, 0) is at the top-left corner no matter what's going on in your screen.

You have to translate that point yourself. In your case, some basic subtraction will do.

like image 63
Kevin Workman Avatar answered Sep 11 '25 00:09

Kevin Workman