Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.imackshun.games.talesoftheworldengine.objects3d;
- /*Description: The camera that is used on the world map. It can be rotated left and right using the right analog stick, and rotated up and down, but constrained to ensure a
- * decent view of the scene. It's position is determined by a vector of the look target, which should be an ObjectInScene for ease of use.*/
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.graphics.PerspectiveCamera;
- import com.badlogic.gdx.math.Vector3;
- public class WorldMapCamera {
- // The Actual Camera Object.
- public PerspectiveCamera Camera;
- // Where is the camera located now?
- public Vector3 Position;
- // What is the camera's focus point?
- public Vector3 LookTarget;
- // How far is the camera from the target point? The Direction Vector is scaled by this number.
- public float DistanceFromTarget;
- // The Vector that is used to determine the position of the camera. This is scaled by the DistanceFromTarget variable to set the position of the camera.
- public Vector3 TargetDirectionVector;
- // Save on resources.
- public Vector3 TemporaryVector;
- // Constructor
- public WorldMapCamera(Vector3 Position, Vector3 LookTarget) {
- this.Position = Position;
- this.LookTarget = LookTarget;
- TargetDirectionVector = new Vector3();
- TemporaryVector = new Vector3();
- DistanceFromTarget = 40f;
- Camera = new PerspectiveCamera(36, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
- Camera.position.set(Position);
- Camera.lookAt(LookTarget);
- Camera.near = 1f;
- Camera.far = 3000f;
- Camera.update();
- }
- // Personal Methods
- public void Update(float delta) {
- Camera.up.set(0f, 1f, 0f);
- // Generate the camera's position.
- Position.x = LookTarget.x + (TargetDirectionVector.x * DistanceFromTarget);
- Position.y = LookTarget.y + (TargetDirectionVector.y * DistanceFromTarget);
- Position.z = LookTarget.z + (TargetDirectionVector.z * DistanceFromTarget);
- // Interpolate the camera's position to the target position.
- Camera.position.set(Position);
- // Make the camera look at the target. Can get some weird rotations
- // sometimes...
- Camera.lookAt(new Vector3(LookTarget.x, LookTarget.y, LookTarget.z));
- // Update the camera.
- Camera.update();
- }
- // Makes a variable slowly creep up to a desired value.
- public float Interpolate(float TargetValue, float CurrentValue, float delta) {
- float Difference = TargetValue - CurrentValue;
- float InterpolatedValue = 0;
- // If the difference is greater than the delta, decrease it accordingly.
- // Used if the TargetValue is greater than the CurrentValue.
- if (Difference > delta) {
- InterpolatedValue = CurrentValue + delta;
- }
- // If the difference is less than the negative delta, increase it
- // accordingly.
- // Used if the TargetValue is lower than the CurrentValue.
- else if (Difference < -delta) {
- InterpolatedValue = CurrentValue - delta;
- } else {
- InterpolatedValue = TargetValue;
- }
- return InterpolatedValue;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment