iMackshun

LookTarget.java

Jun 9th, 2017
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.20 KB | None | 0 0
  1. package com.imackshun.games.talesoftheworldengine.objects3d;
  2.  
  3. /*A 3d model that is not a player or enemy. NPC's possibly.*/
  4.  
  5. import com.badlogic.gdx.Gdx;
  6. import com.badlogic.gdx.graphics.Color;
  7. import com.badlogic.gdx.graphics.PerspectiveCamera;
  8. import com.badlogic.gdx.graphics.VertexAttributes.Usage;
  9. import com.badlogic.gdx.graphics.g3d.Environment;
  10. import com.badlogic.gdx.graphics.g3d.Material;
  11. import com.badlogic.gdx.graphics.g3d.Model;
  12. import com.badlogic.gdx.graphics.g3d.ModelBatch;
  13. import com.badlogic.gdx.graphics.g3d.ModelInstance;
  14. import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
  15. import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;
  16. import com.badlogic.gdx.math.MathUtils;
  17. import com.badlogic.gdx.math.Vector3;
  18. import com.imackshun.games.talesoftheworldengine.controls.HUD;
  19.  
  20. public class LookTarget {
  21.     // Where is the player.
  22.     public Vector3 Position;
  23.     // What direction is up?
  24.     public Vector3 WorldUpDirection;
  25.     public Vector3 UpDirection;
  26.     public Vector3 ForwardDirection;
  27.     // What direction is perpendicular to the forward direction and the up
  28.     // direction?
  29.     public Vector3 RightDirection;
  30.     // public Vector3 LeftDirection;
  31.     // Visual representation of the object.
  32.     public ModelInstance ObjectInstance;
  33.     // Used for the look target. Allows the camera to move on the Y Axis.
  34.     public float XRotation;
  35.     // Used to determine the rotation of the character.
  36.     public float YRotation;
  37.     // Used for positioning the vector models.
  38.     public float AxesMultiplier;
  39.     // Models that show where the Vectors are. Used for debugging.
  40.     public Model LeftModel;
  41.     public Model RightModel;
  42.     public Model UpModel;
  43.     public Model ForwardModel;
  44.     public ModelInstance LeftInstance;
  45.     public ModelInstance RightInstance;
  46.     public ModelInstance UpInstance;
  47.     public ModelInstance ForwardInstance;
  48.  
  49.     // Constructor
  50.     public LookTarget(Vector3 Position) {
  51.         this.Position = new Vector3();
  52.         this.Position.set(Position);
  53.         // Initialize the Vectors!
  54.         WorldUpDirection = new Vector3(0f, 1f, 0f);
  55.         UpDirection = new Vector3(0f, 1f, 0f);
  56.         ForwardDirection = new Vector3(1f, 0f, 0f);
  57.         //LeftDirection = new Vector3();
  58.         RightDirection = new Vector3();
  59.         // Initialize all of the velocities...
  60.         // For Debugging
  61.         ModelBuilder modelBuilder = new ModelBuilder();
  62.         ObjectInstance = new ModelInstance(modelBuilder.createBox(1f, 1f, 1f, new Material(ColorAttribute.createDiffuse(Color.WHITE)), Usage.Position | Usage.Normal));
  63.         LeftModel = modelBuilder.createBox(1f, 1f, 1f, new Material(ColorAttribute.createDiffuse(Color.GOLD)), Usage.Position | Usage.Normal);
  64.         RightModel = modelBuilder.createBox(1f, 1f, 1f, new Material(ColorAttribute.createDiffuse(Color.YELLOW)), Usage.Position | Usage.Normal);
  65.         UpModel = modelBuilder.createBox(1f, 1f, 1f, new Material(ColorAttribute.createDiffuse(Color.BLUE)), Usage.Position | Usage.Normal);
  66.         ForwardModel = modelBuilder.createBox(1f, 1f, 1f, new Material(ColorAttribute.createDiffuse(Color.RED)), Usage.Position | Usage.Normal);
  67.         ForwardInstance = new ModelInstance(ForwardModel);
  68.         UpInstance = new ModelInstance(UpModel);
  69.         LeftInstance = new ModelInstance(LeftModel);
  70.         RightInstance = new ModelInstance(RightModel);
  71.         AxesMultiplier = 6;
  72.     }
  73.  
  74.     // Personal Methods
  75.     public void Update(float delta, PerspectiveCamera camera) {
  76.         CalculateRightDirection();
  77.         // Used for Rotation
  78.         /*if (ObjectInstance != null) {
  79.             ObjectInstance.transform.setToTranslation(Position);
  80.             ObjectInstance.transform.rotate(WorldUpDirection, YRotation);
  81.         }*/
  82.         // For debugging.
  83.         ForwardInstance.transform.setTranslation(Position);
  84.         ForwardInstance.transform.translate(ForwardDirection.x * AxesMultiplier, ForwardDirection.y * AxesMultiplier, ForwardDirection.z * AxesMultiplier);
  85.         UpInstance.transform.setTranslation(Position);
  86.         UpInstance.transform.translate(UpDirection.x * AxesMultiplier, UpDirection.y * AxesMultiplier, UpDirection.z * AxesMultiplier);
  87.         RightInstance.transform.setTranslation(Position);
  88.         RightInstance.transform.translate(RightDirection.x * AxesMultiplier, RightDirection.y * AxesMultiplier, RightDirection.z * AxesMultiplier);
  89.         Debug();
  90.     }
  91.  
  92.     public void Render(ModelBatch Batch3D, Environment environment) {
  93.         if (ObjectInstance != null) {
  94.             Batch3D.render(ObjectInstance);
  95.             Batch3D.render(ForwardInstance);
  96.             Batch3D.render(UpInstance);
  97.             Batch3D.render(RightInstance);
  98.         }
  99.     }
  100.  
  101.     public void CalculateRightDirection() {
  102.         RightDirection.set(ForwardDirection);
  103.         RightDirection.crs(UpDirection).nor();
  104.         // System.out.println("RightDirection: X:" + RightDirection.x + " Y:" +
  105.         // RightDirection.y + " Z:" + RightDirection.z);
  106.     }
  107.  
  108.     public float CalculateYRotation(Vector3 PlayerPosition, Vector3 TargetPosition) {
  109.         float Angle = 0;
  110.         Vector3 ZAxisPoint = new Vector3(TargetPosition.x, 0, PlayerPosition.z);
  111.         // Calculate Distances...
  112.         float Distance1 = (PlayerPosition.x - ZAxisPoint.x);
  113.         float Distance2 = (TargetPosition.z - ZAxisPoint.z);
  114.         Angle = (float) (MathUtils.atan2(Distance2, Distance1) * (180 / Math.PI) + 180);
  115.         return Angle;
  116.  
  117.     }
  118.  
  119.     // Makes a variable slowly creep up to a desired value.
  120.     public float Interpolate(float TargetValue, float CurrentValue, float delta) {
  121.         float Difference = TargetValue - CurrentValue;
  122.         float InterpolatedValue = 0;
  123.         // If the difference is greater than the delta, decrease it accordingly.
  124.         // Used if the TargetValue is greater than the CurrentValue.
  125.         if (Difference > delta) {
  126.             InterpolatedValue = CurrentValue + delta;
  127.         }
  128.         // If the difference is less than the negative delta, increase it
  129.         // accordingly.
  130.         // Used if the TargetValue is lower than the CurrentValue.
  131.         else if (Difference < -delta) {
  132.             InterpolatedValue = CurrentValue - delta;
  133.         } else {
  134.             InterpolatedValue = TargetValue;
  135.         }
  136.         return InterpolatedValue;
  137.     }
  138.  
  139.     // Set the forward direction based on the unit circle, given an angle. Used
  140.     // only for look target.
  141.     public void SetForwardDirectionToRotation() {
  142.         ForwardDirection.x = (float) Math.cos(YRotation * MathUtils.degreesToRadians);
  143.         ForwardDirection.z = (float) Math.sin(YRotation * MathUtils.degreesToRadians);
  144.         // System.out.println("YRotation: " + YRotation);
  145.         // System.out.println("ForwardDirectionX: " + ForwardDirection.x +
  146.         // " ForwardDirectionZ: " + ForwardDirection.z);
  147.     }
  148.  
  149.     public void SetUpDirectionToRotation() {
  150.         // Rotate the Up Direction on the X.
  151.         UpDirection.y = (float) Math.sin(XRotation * MathUtils.degreesToRadians);
  152.         UpDirection.z = (float) Math.cos(XRotation * MathUtils.degreesToRadians);
  153.         // System.out.println("XRotation: " + XRotation);
  154.         // System.out.println("UpDirectionZ: " + UpDirection.z +
  155.         // " UpDirectionY: " + UpDirection.y);
  156.     }
  157.  
  158.     public void LoadModel(Model model) {
  159.         ObjectInstance = new ModelInstance(model);
  160.     }
  161.  
  162.     public Vector3 MultiplyVector3(Vector3 FactorOne, Vector3 FactorTwo) {
  163.         Vector3 ProductVector = new Vector3();
  164.         ProductVector.set(FactorOne);
  165.         ProductVector.x *= FactorTwo.x;
  166.         ProductVector.y *= FactorTwo.y;
  167.         ProductVector.z *= FactorTwo.z;
  168.         return ProductVector;
  169.     }
  170.  
  171.     public void Debug() {
  172.         System.out.println("ForwardDirection: " + ForwardDirection);
  173.         System.out.println("UpDirection: " + UpDirection);
  174.         System.out.println("RightDirection: " + RightDirection);
  175.     }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment