iMackshun

WorldMapCamera.java

Jun 9th, 2017
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. package com.imackshun.games.talesoftheworldengine.objects3d;
  2.  
  3. /*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
  4. * 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.*/
  5.  
  6. import com.badlogic.gdx.Gdx;
  7. import com.badlogic.gdx.graphics.PerspectiveCamera;
  8. import com.badlogic.gdx.math.Vector3;
  9.  
  10. public class WorldMapCamera {
  11. // The Actual Camera Object.
  12. public PerspectiveCamera Camera;
  13. // Where is the camera located now?
  14. public Vector3 Position;
  15. // What is the camera's focus point?
  16. public Vector3 LookTarget;
  17. // How far is the camera from the target point? The Direction Vector is scaled by this number.
  18. public float DistanceFromTarget;
  19. // 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.
  20. public Vector3 TargetDirectionVector;
  21. // Save on resources.
  22. public Vector3 TemporaryVector;
  23. // Constructor
  24. public WorldMapCamera(Vector3 Position, Vector3 LookTarget) {
  25. this.Position = Position;
  26. this.LookTarget = LookTarget;
  27. TargetDirectionVector = new Vector3();
  28. TemporaryVector = new Vector3();
  29. DistanceFromTarget = 40f;
  30. Camera = new PerspectiveCamera(36, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
  31. Camera.position.set(Position);
  32. Camera.lookAt(LookTarget);
  33. Camera.near = 1f;
  34. Camera.far = 3000f;
  35. Camera.update();
  36. }
  37.  
  38. // Personal Methods
  39. public void Update(float delta) {
  40. Camera.up.set(0f, 1f, 0f);
  41. // Generate the camera's position.
  42. Position.x = LookTarget.x + (TargetDirectionVector.x * DistanceFromTarget);
  43. Position.y = LookTarget.y + (TargetDirectionVector.y * DistanceFromTarget);
  44. Position.z = LookTarget.z + (TargetDirectionVector.z * DistanceFromTarget);
  45. // Interpolate the camera's position to the target position.
  46. Camera.position.set(Position);
  47. // Make the camera look at the target. Can get some weird rotations
  48. // sometimes...
  49. Camera.lookAt(new Vector3(LookTarget.x, LookTarget.y, LookTarget.z));
  50. // Update the camera.
  51. Camera.update();
  52. }
  53.  
  54. // Makes a variable slowly creep up to a desired value.
  55. public float Interpolate(float TargetValue, float CurrentValue, float delta) {
  56. float Difference = TargetValue - CurrentValue;
  57. float InterpolatedValue = 0;
  58. // If the difference is greater than the delta, decrease it accordingly.
  59. // Used if the TargetValue is greater than the CurrentValue.
  60. if (Difference > delta) {
  61. InterpolatedValue = CurrentValue + delta;
  62. }
  63. // If the difference is less than the negative delta, increase it
  64. // accordingly.
  65. // Used if the TargetValue is lower than the CurrentValue.
  66. else if (Difference < -delta) {
  67. InterpolatedValue = CurrentValue - delta;
  68. } else {
  69. InterpolatedValue = TargetValue;
  70. }
  71. return InterpolatedValue;
  72. }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment