67 lines
1.8 KiB
C#
67 lines
1.8 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
/// <summary>
|
|
/// A class that does some kind of wizardry i dont really know, its suppose to convert cartesian X,Y,Z coordinates into a spherical coordinate system
|
|
/// </summary>
|
|
class SphericalCoordinatesHelper
|
|
{
|
|
private Vector3 _positionInput;
|
|
private float _radius;
|
|
private float _azimuth;
|
|
private float _inclination;
|
|
|
|
public float Radius
|
|
{
|
|
get
|
|
{
|
|
return _radius;
|
|
}
|
|
}
|
|
|
|
public float Azimuth
|
|
{
|
|
get
|
|
{
|
|
return _azimuth;
|
|
}
|
|
}
|
|
|
|
public float Inclination
|
|
{
|
|
get
|
|
{
|
|
return _inclination;
|
|
}
|
|
}
|
|
|
|
public SphericalCoordinatesHelper(Vector3 cartesianCoordinate)
|
|
{
|
|
_positionInput = cartesianCoordinate;
|
|
|
|
_radius = Mathf.Sqrt(Mathf.Pow(_positionInput.x, 2) + Mathf.Pow(_positionInput.y, 2) + Mathf.Pow(_positionInput.z, 2));
|
|
_azimuth = Mathf.Atan2(_positionInput.y, _positionInput.x);
|
|
_inclination = Mathf.Acos(_positionInput.z / _radius);
|
|
}
|
|
|
|
public Vector3 GetCartesianVector()
|
|
{
|
|
float x = _radius * Mathf.Cos(_azimuth) * Mathf.Sin(_inclination);
|
|
float y = _radius * Mathf.Sin(_azimuth) * Mathf.Sin(_inclination);
|
|
float z = _radius * Mathf.Cos(_inclination);
|
|
|
|
return new Vector3(x, y, z);
|
|
}
|
|
|
|
public static float operator- (SphericalCoordinatesHelper lhs, SphericalCoordinatesHelper rhs)
|
|
{
|
|
Vector3 lhsCart = lhs.GetCartesianVector();
|
|
Vector3 rhsCart = rhs.GetCartesianVector();
|
|
|
|
float answer = Mathf.Sqrt(Mathf.Pow(lhsCart.x - rhsCart.x, 2) + Mathf.Pow(lhsCart.y - rhsCart.y, 2) + Mathf.Pow(lhsCart.z - rhsCart.z, 2));
|
|
|
|
return answer;
|
|
}
|
|
}
|