# How can i calculate camera position

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

## Recommended Posts

Hi!

I have one problem with my camera in 2D game, my camera look like this: http://www.coubetech.republika.pl/test/main.html camera tarack one obiect but camera can move only when is near game border, and i don't know how i can calculate camera position something obiect allowing of this game border. Now i try do this that:

var value = pos_obj.x - widthGame / 2;
if (value < 0) value = 0;
if (value > widthGame / 2) value = widthGame / 2;


But it's dosen't work very good;

My camera code:

var border = 150;
if (g_CameraX > pos_obj.x - border) g_CameraX = pos_obj.x - border;
if (g_CameraX < pos_obj.x + border - 640) g_CameraX = pos_obj.x + border - 640;
if (g_CameraX < 0) g_CameraX = 0;
if (g_CameraX > 1100 - 640) g_CameraX = 1100 - 640;


##### Share on other sites

Think of it in another way: The world is W pixels wide, running from 0 to W-1. The camera shows you a slice of that world, and the slice is w pixels wide. The view width should be selected less than or at most equal to the world width, i.e. w <= W. Then the allowed positions of the camera are in the range [0, W-w] if using the left margins as origin, or else [w/2, W-w/2] if using the center of the view as origin. So the camera can move everywhere except it is too close to the borders of the world.

• 13
• 18
• 29
• 11