• Create Account

Need scary sound effects or creepy audio loops for your next horror-themed game? Check out Highscore Vol.3 - The Horror Edition in our marketplace. 50 sounds and 10 loops for only \$9.99!

# How can i calculate camera position

### #1adrian_134  Members   -  Reputation: 120

Like
0Likes
Like

Posted 13 October 2013 - 06:57 AM

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;


### #2haegarr  Crossbones+   -  Reputation: 2330

Like
0Likes
Like

Posted 13 October 2013 - 09:15 AM

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.

PARTNERS