Skip to content

Instantly share code, notes, and snippets.

@Rayer
Created January 1, 2015 11:21
Show Gist options
  • Save Rayer/d962bce76ff97775da1a to your computer and use it in GitHub Desktop.
Save Rayer/d962bce76ff97775da1a to your computer and use it in GitHub Desktop.
//Update camera position
//Camera.x - viewPort.width must > 0, or just set to 0
//Camera.y - viewPort.height must > 0, or just set to 0
//restrict 0, 0 side
float cameraX = actor.getX() - (camera.viewportWidth / 2) > 0 ? actor.getX() : camera.viewportWidth / 2;
float cameraY = actor.getY() - (camera.viewportHeight / 2) > 0 ? actor.getY() : camera.viewportHeight / 2;
//restrict h, w side
cameraX = actor.getX() + (camera.viewportWidth / 2) > mapSizeX ? mapSizeX - (camera.viewportWidth / 2) : cameraX;
cameraY = actor.getY() + (camera.viewportHeight / 2) > mapSizeY ? mapSizeY - (camera.viewportHeight / 2) : cameraY;
@tzengyuxio
Copy link

// if v < a, return a; if v > b, return b; else keep v
float clamp(float v, float a, float b)
{
    return max(a, min(v, b));
}

float cameraX = clamp(actor.getX(), camera.viewportWidth / 2, mapSizeX - (camera.viewportWidth / 2) );
float cameraY = clamp(actor.getY(), camera.viewportHeight / 2, mapSizeY - (camera.viewportHeight / 2) );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment