Jump to content
  • Advertisement
Sign in to follow this  
Hekmawful

Making Screen follow the player?

This topic is 2239 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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? Edited by Madhed

Share this post


Link to post
Share on other sites

We need more info.

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

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!