Java Slick2d - How to translate mouse coordinates to world coordinates

Posted by Corey on Game Development See other posts from Game Development or by Corey
Published on 2012-04-12T22:20:39Z Indexed on 2012/04/12 23:45 UTC
Read the original article Hit count: 523

Filed under:
|
|
|

I am translating in my main class render. How do I get the mouse position where my mouse actually is after I scroll the screen

public void render(GameContainer gc, Graphics g) throws SlickException 
{
    float centerX = 800/2;
    float centerY = 600/2;
    g.translate(centerX, centerY);
    g.translate(-player.playerX, -player.playerY);
    gen.render(g);
    player.render(g);
}

playerX = 800 /2 - sprite.getWidth();
playerY = 600 /2 - sprite.getHeight();

Image to help with explanation

game world showing selected grid not where the mouse is

I tried implementing a camera but it seems no matter what I can't get the mouse position. I was told to do this worldX = mouseX + camX; but it didn't work the mouse was still off.

Here is my Camera class if that helps:

public class Camera {
public float camX;
public float camY;

Player player;

public void init() {
    player = new Player();
}

public void update(GameContainer gc, int delta) {
    Input input = gc.getInput();

    if(input.isKeyDown(Input.KEY_W)) {
        camY -= player.speed * delta;
    }
    if(input.isKeyDown(Input.KEY_S)) {
        camY += player.speed * delta;
    }
    if(input.isKeyDown(Input.KEY_A)) {
        camX -= player.speed * delta;
    }
    if(input.isKeyDown(Input.KEY_D)) {
        camX += player.speed * delta;
    }
}

Code used to convert mouse

worldX = (int) (mouseX + cam.camX);
worldY = (int) (mouseY + cam.camY);

© Game Development or respective owner

Related posts about java

Related posts about coordinates