I am using a camera/viewport for my screen which always centers around the player. Right now I always offset the coordinates for everything so get drawn on the screen when they should be visible. This methods seems very inelegant and I want to use the translate() method of g2d. My problem is, that it doesn’t work at all. If I move the player towards the edge of the screen it just ignores my translate and draws everything at it’s original coordinates. Maybe I’m just translating wrong.
public class Game implements Runnable{
private void render() {
Graphics2D g = (Graphics2D) bs.getDrawGraphics();
g.setBackground(Color.black);
g.clearRect(0, 0, WIDTH, HEIGHT);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
render(g);
g.dispose();
bs.show(); // the bufferstrategy
}
private void render(Graphics2D g) {
screen.render(g);
}
}
public class GameScreen {
public void render(Graphics2D g) {
g.translate(Math.round(cam.camX), Math.round(cam.camY));
mc.render(g);
player.render(g);
hud.render(g);
g.translate(-(Math.round(cam.camX)), -(Math.round(cam.camY)));
}
}
public class Player {
g.drawImage(current, Math.round(x /*- cam.camX*/), Math.round(y /*- cam.camY*/), null); //commented out my usual offset
}
I am translating at the game screen because other screens don’t need the translation.
NOTE: My inelegant method works perfectly which is basically a translation.
EDIT: I am using java2D/swing/awt if that’s of any importance