[ad_1]
I haven’t ever used LibGDX, but what you’ll have to do is find the width of the camera viewport, and limit the x position of the camera like this pseudocode:
camera.x = min((levelWidth / 2) + levelPosition.x - (camera.viewportWidth / 2), camera.x);
Essentially, we find the smaller number with the min
function, which will limit our camera.x
value to the x position of where the camera can be and not show any of the outside of the level. The OrthographicCamera
class from LibGDX provides the viewportWidth
value, so you only need to make sure you know where your level object is located and how wide it is.
That code with prevent you from having the camera go too far to the right, to prevent it going to far too the left, use an implementation similar to:
camera.x = max(-(levelWidth / 2) + levelPosition.x + (camera.viewportWidth / 2), camera.x);
This does a similar thing to the original, it just ensures that the camera’s left bound never goes further left than the leftmost point on the level. Both of these snippets assume that the position of the level is in its center. If the level is at (0, 0), then the levelPosition.x
can be omitted completely:
camera.x = min((levelWidth / 2) - (camera.viewportWidth / 2), camera.x);
camera.x = max(-(levelWidth / 2) + (camera.viewportWidth / 2), camera.x);
[ad_2]