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
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);
Tile class
public class TileGen {
Block block;
public Block[] tiles = new Block[2];
public int width, height;
public int[][] index;
int mouseX, mouseY;
int worldX, worldY;
boolean selected;
Image dirt, grass, selection;
SpriteSheet tileSheet;
int startX, startY, stopX, stopY;
Camera cam;
Player player;
public void init() throws SlickException {
tileSheet = new SpriteSheet("assets/tiles/tileSheet.png", 64, 64);
grass = tileSheet.getSprite(0,0);
dirt = tileSheet.getSprite(1,0);
selection = tileSheet.getSprite(2,0);
tiles[0] = new Block(BlockType.Grass, new Vector2f(0,0), grass, true);
tiles[1] = new Block(BlockType.Dirt, new Vector2f(0,0), dirt, true);
width = 50;
height = 50;
index = new int[width][height];
cam = new Camera();
player = new Player();
Random rand = new Random();
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
index[x][y] = rand.nextInt(2);
}
}
}
public void update(GameContainer gc) {
Input input = gc.getInput();
mouseX = input.getMouseX();
mouseY = input.getMouseY();
worldX = (int) (mouseX - player.playerX);
worldY = (int) (mouseY - player.playerY);
if(input.isMousePressed(Input.MOUSE_LEFT_BUTTON)) {
selected = true;
} else {selected = false;}
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
if(IsMouseInsideTile(x, y) && selected) {
if(tiles[index[x][y]]== tiles[0])
index[x][y] = 1;
}
}
}
startX = (int) (cam.camX/64);
startY = (int) (cam.camY/64);
stopX = startX + (gc.getWidth()/64) + 2;
stopY = startY + (gc.getHeight()/64) + 2;
System.out.println(worldX);
}
public void render(Graphics g, GameContainer container) {
for (int x = startX; x < stopX; x++) {
for (int y = startY; y < stopY; y++) {
tiles[index[x][y]].texture.draw(x * 64, y *64);
if(IsMouseInsideTile(x, y)) {
selection.draw(x * 64, y * 64);
}
}
}
}
public boolean IsMouseInsideTile(int x, int y)
{
return (worldX >= x * 64 && worldX <= (x + 1) * 64 &&
worldY >= y * 64 && worldY <= (y + 1) * 64);
}
}