Making Screen follow the player?

Started by
5 comments, last by Gabriel Grom 11 years, 7 months ago
Basically what I want to do, is make the screen follow the player.
The map is made with a 2 dimensional array, and is made with a chunk loading system.
I basically just don't know how to make the screen follow the player :)
Advertisement
We need more info.

What kind of game?
Graphics library?
[s]Language?[/s] My bad, didn't see the JAVA tag
What have you tried?

We need more info.

What kind of game?
Graphics library?
Language?
What have you tried?

Overhead 2D RPG Game.
Built in Graphics.
Java.
Don't follow the character. Have the character in a fixed position, and move the world behind the character instead.

If however you have your heart set on it, then write a map panning function. I assume you are using swing since you did not specify otherwise.

To do it, seperate your background drawing over to a texture, one that is sized about N^2 larger than the window resolution. Then draw a subrectangle of it over to the window canvas, and only flip once v-sync is ready. The key to making it center on the character is setting the subrectangle to the following coordinates.

X = CHARX - ((WINWIDTH - CHARWIDTH) / 2)
Y = CHARY - ((WINHEIGHT - CHARHEIGHT) / 2)

Make certain that Character is always within the following bounds of the texture, otherwise shift the map to compensate.

( ((WINWIDTH - CHARWIDTH) / 2) < x < TEXTWIDTH - ((WINWIDTH - CHARWIDTH) / 2) )
( ((WINHEIGHT - CHARHEIGHT) / 2) < y < TEXTHEIGHT - ((WINHEIGHT - CHARHEIGHT) / 2) )
One thing I've used (with Swing/AWT) to handling level scrolling is Graphics.translate: http://docs.oracle.com/javase/6/docs/api/java/awt/Graphics.html#translate(int, int).

The rest is just arithmetic (calculating a centered position) and handling level boundaries.
Here's what I've done:
Create a camera object that follows the player. Every frame, subtract all your active objects x and y coordinates from the camera's, and set those values into each objects local position variable. If either value is negative or outside the cameras view, dont draw it. Otherwise draw the objects at their local position. Then you just have to base all mouse clicks off local positions and you already have it calculated.

I don't know if I explained this well enough so heres an example:
Lets say you have an enemy at a global position of 35,40;
The camera's top left corner is at 0,100 and has dimensions of 100,100;
You would draw the enemy on screen based on the calculation:
int locX = entX - camX; >> locX = 35-0 = 35
int locY = camY - entY; >> locY = 100-40 = 60
so you would draw the entity at 35,60.

Drawing things in this way accomplishes a lot. You know what is visible so you don't draw things that don't need to be. You don't have to reposition everything in the world every frame. You also have things local positions for mouse events.

Hope this helps,
Peter
-------------------------------------
"Other than that, I have no opinion."
My Blog - Check it Out

One thing I've used (with Swing/AWT) to handling level scrolling is Graphics.translate: http://docs.oracle.c...l#translate(int, int).

The rest is just arithmetic (calculating a centered position) and handling level boundaries.


Yeah, this seems a lot simpler than translating every object you create, which might have an impact on performance.

What you do is just translate the graphics so that the rendering is done with the player in the middle.

Almost every 2D library has a translate function for graphics or a camera class which you can use.

This topic is closed to new replies.

Advertisement