Archived

This topic is now archived and is closed to further replies.

raging_jakl

how to position my world around my player

Recommended Posts

I am currently making a 2D Zelda type game and I have come quite far with it. I am trying to add a feature to my map editor that allows the user to set the start position of the player on the map. I am having problems trying to set the world position so that black edges don''t appear around the screen when the map is loaded and the player is put on the screen. Example A 36x36 (tiles each 32x32 pixels in size) map with a start position at tile 35,35. How can I make it so the bottom tiles are drawn at the bottom of the screen and not at the top with a bunch of blackness below? I am not neccessarily looking for some source code to do this(would be helpful) but some insight on the algorithm involved in making this happen. My scrolling function does bounds checking to prevent scrolling beyond the edges of the map. I thought I may use this to scroll the map to the start position when the map is loaded but I thought there could be a better way. Does anyone have any ideas that may help me?

Share this post


Link to post
Share on other sites
think of your screen as a camera looking down on the map through a window centered at the player position as he moves around the world.

So, the world will be 1152 x 1152 pixels.

The screen is a window made of (x, y)-(x+640, y+480), or whatever your display resolution is. So, to avoid borders, x >= 0, y >= 0, and x < 1152-640, y < 1152-480.

(x, y) will be the coordinates on the map, and you move (x, y) so that (x+320, y+240) points to your player position on the map.

the player is at pos xplayer, yplayer on the map.

so if the player is centered on the display, the window start position is x = xplayer-320, y = yplayer-240

and also, you have the constrains on x and y.

x >= 0 && x < (1152-640)
y >= 0 && y < (1152-480)

so there you go


int player_x, player_y;
GetPlayerCoords(player_x, player_y);

//----------------------------------------------------------

// Move the dispaly window on the map so that the player

// is at the centre of the screen

//----------------------------------------------------------

display_x = player_x - (display_resolution_width /2);
display_y = player_y - (display_resolution_height/2);

//----------------------------------------------------------

// constrain the display window to the map boundaries

//----------------------------------------------------------

int min_x = 0;
int min_y = 0;
int max_x = (map_num_tiles_x * map_tile_pixels_width) - (display_resolution_width /2) - 1;
int max_y = (map_num_tiles_y * map_tile_pixels_height) - (display_resolution_height/2) - 1;


if (display_x < min_x)
display_x = min_x;
else if (display_x > max_x)
display_x = max_x;

if (display_y < min_y)
display_y = min_y;
else if (display_y > max_y)
display_y = max_y;

Share this post


Link to post
Share on other sites
Thanks, I got it to work in my engine. I had something similar to this before but the start position would get messed up if it started in the bottom right corner. Oh well it works now! Now on to the fun stuff!

Share this post


Link to post
Share on other sites