60 lines
1.8 KiB
C#
60 lines
1.8 KiB
C#
using System;
|
|
#if UNITY_5_3_OR_NEWER
|
|
using Vector3Float = UnityEngine.Vector3;
|
|
#endif
|
|
|
|
namespace LinearAlgebra
|
|
{
|
|
|
|
public class Direction
|
|
{
|
|
public float horizontal;
|
|
public float vertical;
|
|
|
|
public Direction()
|
|
{
|
|
horizontal = 0;
|
|
vertical = 0;
|
|
}
|
|
public Direction(float horizontal, float vertical)
|
|
{
|
|
this.horizontal = horizontal;
|
|
this.vertical = vertical;
|
|
//Normalize();
|
|
}
|
|
|
|
public readonly static Direction forward = new Direction(0, 0);
|
|
public readonly static Direction backward = new Direction(-180, 0);
|
|
public readonly static Direction up = new Direction(0, 90);
|
|
public readonly static Direction down = new Direction(0, -90);
|
|
public readonly static Direction left = new Direction(-90, 0);
|
|
public readonly static Direction right = new Direction(90, 0);
|
|
|
|
public void Normalize()
|
|
{
|
|
if (this.vertical > 90 || this.vertical < -90)
|
|
{
|
|
this.horizontal += 180;
|
|
this.vertical = 180 - this.vertical;
|
|
}
|
|
}
|
|
|
|
public Vector3Float ToVector3()
|
|
{
|
|
float verticalRad = (Angle.pi / 2) - this.vertical * Angle.Deg2Rad;
|
|
float horizontalRad = this.horizontal * Angle.Deg2Rad;
|
|
float cosVertical = (float)Math.Cos(verticalRad);
|
|
float sinVertical = (float)Math.Sin(verticalRad);
|
|
float cosHorizontal = (float)Math.Cos(horizontalRad);
|
|
float sinHorizontal = (float)Math.Sin(horizontalRad);
|
|
|
|
float x = sinVertical * sinHorizontal;
|
|
float y = cosVertical;
|
|
float z = sinVertical * cosHorizontal;
|
|
|
|
Vector3Float v = new(x, y, z);
|
|
return v;
|
|
}
|
|
}
|
|
|
|
} |