View more

View more

View more

Image of the Day Submit

IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

Making Screen follow the player?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

6 replies to this topic

#1Hekmawful  Members

Posted 26 August 2012 - 12:19 PM

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

#2Madhed  Members

Posted 26 August 2012 - 12:31 PM

We need more info.

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

Edited by Madhed, 26 August 2012 - 12:37 PM.

#3Hekmawful  Members

Posted 26 August 2012 - 12:33 PM

We need more info.

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

Overhead 2D RPG Game.
Built in Graphics.
Java.

#4kd7tck  Members

Posted 26 August 2012 - 03:43 PM

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) )

Edited by kd7tck, 26 August 2012 - 03:45 PM.

#5thok  Members

Posted 27 August 2012 - 01:45 AM

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.

#6destructivArts  Members

Posted 27 August 2012 - 03:17 PM

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

#7Gabriel Grom  Members

Posted 28 August 2012 - 09:54 AM

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.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.