47 lines
1.8 KiB
C#
47 lines
1.8 KiB
C#
using UnityEngine;
|
|
|
|
namespace Passer.LinearAlgebra {
|
|
public class Spherical {
|
|
public float distance;
|
|
public Direction direction;
|
|
|
|
public static Spherical zero = new(0, 0, 0);
|
|
public static Spherical forward = new(1, 0, 0);
|
|
|
|
public Spherical(float distance, float horizontal, float vertical) {
|
|
this.distance = distance;
|
|
this.direction = new Direction(horizontal, vertical);
|
|
}
|
|
public Spherical(float distance, Direction direction) {
|
|
this.distance = distance;
|
|
this.direction = direction;
|
|
}
|
|
|
|
public static Spherical FromVector3(Vector3 v) {
|
|
float distance = v.magnitude;
|
|
if (distance == 0.0f)
|
|
return new Spherical(distance, 0, 0);
|
|
else {
|
|
float verticalAngle = (Mathf.PI / 2 - Mathf.Acos(v.y / distance)) * Mathf.Rad2Deg;
|
|
float horizontalAngle = Mathf.Atan2(v.x, v.z) * Mathf.Rad2Deg;
|
|
return new Spherical(distance, horizontalAngle, verticalAngle);
|
|
}
|
|
}
|
|
|
|
public Vector3 ToVector3() {
|
|
float verticalRad = (UnityEngine.Mathf.PI / 2) - this.direction.vertical * UnityEngine.Mathf.Deg2Rad;
|
|
float horizontalRad = this.direction.horizontal * UnityEngine.Mathf.Deg2Rad;
|
|
float cosVertical = UnityEngine.Mathf.Cos(verticalRad);
|
|
float sinVertical = UnityEngine.Mathf.Sin(verticalRad);
|
|
float cosHorizontal = UnityEngine.Mathf.Cos(horizontalRad);
|
|
float sinHorizontal = UnityEngine.Mathf.Sin(horizontalRad);
|
|
|
|
float x = this.distance * sinVertical * sinHorizontal;
|
|
float y = this.distance * cosVertical;
|
|
float z = this.distance * sinVertical * cosHorizontal;
|
|
|
|
Vector3 v = new Vector3(x, y, z);
|
|
return v;
|
|
}
|
|
}
|
|
} |