Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.imackshun.games.talesoftheworldengine.objects3d;
- /*A 3d model that is not a player or enemy. NPC's possibly.*/
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.graphics.Color;
- import com.badlogic.gdx.graphics.PerspectiveCamera;
- import com.badlogic.gdx.graphics.VertexAttributes.Usage;
- import com.badlogic.gdx.graphics.g3d.Environment;
- import com.badlogic.gdx.graphics.g3d.Material;
- import com.badlogic.gdx.graphics.g3d.Model;
- import com.badlogic.gdx.graphics.g3d.ModelBatch;
- import com.badlogic.gdx.graphics.g3d.ModelInstance;
- import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
- import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;
- import com.badlogic.gdx.math.MathUtils;
- import com.badlogic.gdx.math.Vector3;
- import com.imackshun.games.talesoftheworldengine.controls.HUD;
- public class LookTarget {
- // Where is the player.
- public Vector3 Position;
- // What direction is up?
- public Vector3 WorldUpDirection;
- public Vector3 UpDirection;
- public Vector3 ForwardDirection;
- // What direction is perpendicular to the forward direction and the up
- // direction?
- public Vector3 RightDirection;
- // public Vector3 LeftDirection;
- // Visual representation of the object.
- public ModelInstance ObjectInstance;
- // Used for the look target. Allows the camera to move on the Y Axis.
- public float XRotation;
- // Used to determine the rotation of the character.
- public float YRotation;
- // Used for positioning the vector models.
- public float AxesMultiplier;
- // Models that show where the Vectors are. Used for debugging.
- public Model LeftModel;
- public Model RightModel;
- public Model UpModel;
- public Model ForwardModel;
- public ModelInstance LeftInstance;
- public ModelInstance RightInstance;
- public ModelInstance UpInstance;
- public ModelInstance ForwardInstance;
- // Constructor
- public LookTarget(Vector3 Position) {
- this.Position = new Vector3();
- this.Position.set(Position);
- // Initialize the Vectors!
- WorldUpDirection = new Vector3(0f, 1f, 0f);
- UpDirection = new Vector3(0f, 1f, 0f);
- ForwardDirection = new Vector3(1f, 0f, 0f);
- //LeftDirection = new Vector3();
- RightDirection = new Vector3();
- // Initialize all of the velocities...
- // For Debugging
- ModelBuilder modelBuilder = new ModelBuilder();
- ObjectInstance = new ModelInstance(modelBuilder.createBox(1f, 1f, 1f, new Material(ColorAttribute.createDiffuse(Color.WHITE)), Usage.Position | Usage.Normal));
- LeftModel = modelBuilder.createBox(1f, 1f, 1f, new Material(ColorAttribute.createDiffuse(Color.GOLD)), Usage.Position | Usage.Normal);
- RightModel = modelBuilder.createBox(1f, 1f, 1f, new Material(ColorAttribute.createDiffuse(Color.YELLOW)), Usage.Position | Usage.Normal);
- UpModel = modelBuilder.createBox(1f, 1f, 1f, new Material(ColorAttribute.createDiffuse(Color.BLUE)), Usage.Position | Usage.Normal);
- ForwardModel = modelBuilder.createBox(1f, 1f, 1f, new Material(ColorAttribute.createDiffuse(Color.RED)), Usage.Position | Usage.Normal);
- ForwardInstance = new ModelInstance(ForwardModel);
- UpInstance = new ModelInstance(UpModel);
- LeftInstance = new ModelInstance(LeftModel);
- RightInstance = new ModelInstance(RightModel);
- AxesMultiplier = 6;
- }
- // Personal Methods
- public void Update(float delta, PerspectiveCamera camera) {
- CalculateRightDirection();
- // Used for Rotation
- /*if (ObjectInstance != null) {
- ObjectInstance.transform.setToTranslation(Position);
- ObjectInstance.transform.rotate(WorldUpDirection, YRotation);
- }*/
- // For debugging.
- ForwardInstance.transform.setTranslation(Position);
- ForwardInstance.transform.translate(ForwardDirection.x * AxesMultiplier, ForwardDirection.y * AxesMultiplier, ForwardDirection.z * AxesMultiplier);
- UpInstance.transform.setTranslation(Position);
- UpInstance.transform.translate(UpDirection.x * AxesMultiplier, UpDirection.y * AxesMultiplier, UpDirection.z * AxesMultiplier);
- RightInstance.transform.setTranslation(Position);
- RightInstance.transform.translate(RightDirection.x * AxesMultiplier, RightDirection.y * AxesMultiplier, RightDirection.z * AxesMultiplier);
- Debug();
- }
- public void Render(ModelBatch Batch3D, Environment environment) {
- if (ObjectInstance != null) {
- Batch3D.render(ObjectInstance);
- Batch3D.render(ForwardInstance);
- Batch3D.render(UpInstance);
- Batch3D.render(RightInstance);
- }
- }
- public void CalculateRightDirection() {
- RightDirection.set(ForwardDirection);
- RightDirection.crs(UpDirection).nor();
- // System.out.println("RightDirection: X:" + RightDirection.x + " Y:" +
- // RightDirection.y + " Z:" + RightDirection.z);
- }
- public float CalculateYRotation(Vector3 PlayerPosition, Vector3 TargetPosition) {
- float Angle = 0;
- Vector3 ZAxisPoint = new Vector3(TargetPosition.x, 0, PlayerPosition.z);
- // Calculate Distances...
- float Distance1 = (PlayerPosition.x - ZAxisPoint.x);
- float Distance2 = (TargetPosition.z - ZAxisPoint.z);
- Angle = (float) (MathUtils.atan2(Distance2, Distance1) * (180 / Math.PI) + 180);
- return Angle;
- }
- // 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;
- }
- // Set the forward direction based on the unit circle, given an angle. Used
- // only for look target.
- public void SetForwardDirectionToRotation() {
- ForwardDirection.x = (float) Math.cos(YRotation * MathUtils.degreesToRadians);
- ForwardDirection.z = (float) Math.sin(YRotation * MathUtils.degreesToRadians);
- // System.out.println("YRotation: " + YRotation);
- // System.out.println("ForwardDirectionX: " + ForwardDirection.x +
- // " ForwardDirectionZ: " + ForwardDirection.z);
- }
- public void SetUpDirectionToRotation() {
- // Rotate the Up Direction on the X.
- UpDirection.y = (float) Math.sin(XRotation * MathUtils.degreesToRadians);
- UpDirection.z = (float) Math.cos(XRotation * MathUtils.degreesToRadians);
- // System.out.println("XRotation: " + XRotation);
- // System.out.println("UpDirectionZ: " + UpDirection.z +
- // " UpDirectionY: " + UpDirection.y);
- }
- public void LoadModel(Model model) {
- ObjectInstance = new ModelInstance(model);
- }
- public Vector3 MultiplyVector3(Vector3 FactorOne, Vector3 FactorTwo) {
- Vector3 ProductVector = new Vector3();
- ProductVector.set(FactorOne);
- ProductVector.x *= FactorTwo.x;
- ProductVector.y *= FactorTwo.y;
- ProductVector.z *= FactorTwo.z;
- return ProductVector;
- }
- public void Debug() {
- System.out.println("ForwardDirection: " + ForwardDirection);
- System.out.println("UpDirection: " + UpDirection);
- System.out.println("RightDirection: " + RightDirection);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment