1
\$\begingroup\$

This is my first non-board game and I'm trying to learn a bit about 2D graphics in Java. But I don't know, how should I efficiently implement a camera to make the screen follow the main character. I have a position-fixed, unchanging terrain as BufferedImage on my map and other also mobile and animated entities, including the character itself. Right now I'm using some calculations(x of character - middle of screen) each time on repaint() to draw the level on the correct position from the (0,0) coordinates. But this method does a lot of calculation and drawing, even for the fixed map. Is there a better and more efficient way?

\$\endgroup\$
2
\$\begingroup\$

Everytime you move the sprite, move the camera too.

This sounds like it's essentially what you're doing now. If you're encountering performance issues, then you should profile your code to determine where your slow code is. If you're not seeing performance issues, then you don't need to worry about performance issues. Doing a few calculations every frame to determine the camera position is miniscule in terms of processing power.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.