Making my own 2D Minecraft Java game, issue with destroying tiles

Hey guys! I’m new to this forum but not new to Java programming. I need help with destroying tiles I have randomly generated.

So clicking on the drawn-in tile marked “1”, made the tile marked “2” blank, which is obviously not where I clicked so I’m wondering why it does this… I have been trying for hours on my own to fix and I have no clue.

Can anyone help? If you need to see any certain code I already have, just ask me to reply it with. I don’t know what you would need to see so I just posted a screenshot. Any help would be appreciated. :slight_smile:

How do you translate the coordinates of the world to the screen? Make sure the transformation from screen coords to world coords is being calculated correctly.

I read a little about that but didn’t understand much… Any tips on how I’d go about doing that?

I’m assuming your world moves about, the tiles aren’t being drawn at the same place every frame right? You translate everything by some (X,Y) offset correct? Make sure you are translating the mouse coord (in window space) is translated by (-X,-Y).

I’m translating the camera by this:

x = -playerObj.getX() + (800 / 2);
y = -playerObj.getY() + (600 / 2);

And then:


g2d.translate(playerCam.getX(), playerCam.getY());
// render
g2d.translate(-playerCam.getX(), -playerCam.getY());

So where would the mouse translation come in?

I solved my issue by adding these two very simple lines of code:


int x = e.getX();
int y = e.getY();
			
x += Main.player.getX() - Main.Width / 2;
y += Main.player.getY() - Main.Height / 2;

Thanks for your help :slight_smile: