Jump to content
  • Advertisement
Sign in to follow this  

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.

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

Recommended Posts



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 this post

Link to post
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.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!