Last active
November 12, 2015 13:23
-
-
Save tomvangreen/7424a47aae8d6ebe906b to your computer and use it in GitHub Desktop.
LibGDX CameraManager
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package space.util; | |
| import com.badlogic.gdx.graphics.OrthographicCamera; | |
| import com.badlogic.gdx.math.Vector2; | |
| import com.badlogic.gdx.math.Vector3; | |
| import com.badlogic.gdx.utils.viewport.Viewport; | |
| /** | |
| * Initialize the CameraManager with viewport and camera and use it to set | |
| * target position and zoom for your camera. The CameraManager will smoothly | |
| * move your camera to the desired target position and zoom levels. | |
| * | |
| * Add cameraManager.update(Gdx.input.getDeltaTime()); to your update loop. | |
| * | |
| * Use the pan, position, targetZoom and changeZoom methods to change the | |
| * existing values. | |
| * | |
| * @author atombrot | |
| * | |
| */ | |
| public class CameraManager { | |
| /** | |
| * Zoom speed modifier | |
| */ | |
| public static final float ZOOM_SPEED = 2f; | |
| /** | |
| * Pan speed modifier | |
| */ | |
| public static final float PAN_SPEED = 1000f; | |
| /** | |
| * LERP Modifier (the higher, the faster the camera acts) | |
| */ | |
| public static final float LERP_MODIFIER = 4f; | |
| /** | |
| * Max zoom value | |
| */ | |
| public static final float MAX_ZOOM = 3f; | |
| /** | |
| * Min zoom value | |
| */ | |
| public static final float MIN_ZOOM = 0.25f; | |
| /** | |
| * Managed viewport | |
| */ | |
| public final Viewport viewport; | |
| /** | |
| * Managed camera | |
| */ | |
| public final OrthographicCamera cam; | |
| /** | |
| * The target the camera aims at (the target will be in the screen center) | |
| */ | |
| protected final Vector2 target = new Vector2(); | |
| /** | |
| * The target zoom. | |
| */ | |
| private float targetZoom = 1f; | |
| /** | |
| * Temp vector used in calculations | |
| */ | |
| private final Vector2 v = new Vector2(); | |
| /** | |
| * Supply the viewport and camera that should be managed. | |
| * | |
| * @param viewport | |
| * @param cam | |
| */ | |
| public CameraManager(Viewport viewport, OrthographicCamera cam) { | |
| this.viewport = viewport; | |
| this.cam = cam; | |
| target.set(cam.position.x, cam.position.y); | |
| } | |
| /** | |
| * Returns the desired zoom value. | |
| * | |
| * @return | |
| */ | |
| public float targetZoom() { | |
| return targetZoom; | |
| } | |
| /** | |
| * Sets the desired zoom value. 1f is no zoom. | |
| * | |
| * @param zoom | |
| */ | |
| public void targetZoom(float zoom) { | |
| targetZoom = clampZoom(zoom); | |
| } | |
| /** | |
| * Changes the existing zoom value by a certain amount | |
| * | |
| * @param change | |
| */ | |
| public void changeZoom(float change) { | |
| targetZoom = clampZoom(targetZoom + change); | |
| } | |
| /** | |
| * Clamps a value to the restricted min and max zoom values | |
| * | |
| * @param zoom | |
| * @return | |
| */ | |
| protected float clampZoom(float zoom) { | |
| return Math.max(Math.min(zoom, CameraManager.MAX_ZOOM), CameraManager.MIN_ZOOM); | |
| } | |
| /** | |
| * Move the target position by a given vector | |
| * | |
| * @param offset | |
| */ | |
| public void pan(Vector2 offset) { | |
| pan(offset.x * cam.zoom, offset.y * cam.zoom); | |
| } | |
| /** | |
| * Move the target position by a given x and y value | |
| * | |
| * @param x | |
| * @param y | |
| */ | |
| public void pan(float x, float y) { | |
| target.add(x, y); | |
| } | |
| /** | |
| * Assign the target position to an x and y value. | |
| * | |
| * @param x | |
| * @param y | |
| */ | |
| public void position(float x, float y) { | |
| target.set(x, y); | |
| } | |
| /** | |
| * Gets the target position | |
| * | |
| * @return | |
| */ | |
| public Vector2 getTarget() { | |
| return target; | |
| } | |
| /** | |
| * Update the actual camera position and zoom to approximate target position | |
| * and zoom. | |
| * | |
| * @param delta | |
| */ | |
| public void act(float delta) { | |
| Vector3 position = cam.position; | |
| v.set(target); | |
| v.sub(position.x, position.y).scl(5f * delta); | |
| position.x += v.x; | |
| position.y += v.y; | |
| float difference = cam.zoom - targetZoom; | |
| cam.zoom -= difference * delta * CameraManager.LERP_MODIFIER; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment