processing

Processing how to translate mouse coordinates


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();
    }

Solution

  • 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.