Java Slick2d - Mouse picking how to take into account camera

Posted by Corey on Game Development See other posts from Game Development or by Corey
Published on 2012-07-09T17:15:06Z Indexed on 2012/07/09 21:24 UTC
Read the original article Hit count: 253

Filed under:
|
|
|

When I move it it obviously changes the viewport so my mouse picking is off.

My camera is just a float x and y and I use g.translate(-cam.cameraX+400, -cam.cameraY+300); to translate the graphics. I have the numbers hard coded just for testing purposes.

How would I take into account the camera so my mouse picking works correctly.

double mousetileX = Math.floor((double)mouseX/tiles.tileWidth);
    double mousetileY = Math.floor((double)mouseY/tiles.tileHeight);

    double playertileX = Math.floor(playerX/tiles.tileWidth);
    double playertileY = Math.floor(playerY/tiles.tileHeight);

    double lengthX = Math.abs((float)playertileX - mousetileX); 
    double lengthY = Math.abs((float)playertileY - mousetileY);
    double distance = Math.sqrt((lengthX*lengthX)+(lengthY*lengthY)); 

    if(input.isMousePressed(Input.MOUSE_LEFT_BUTTON) && distance < 4) { 
        if(tiles.map[(int)mousetileX][(int)mousetileY] == 1) {
            tiles.map[(int)mousetileX][(int)mousetileY] = 0;
        }
    } 

That is my mouse picking code

© Game Development or respective owner

Related posts about java

Related posts about camera