Vector3 conversion fixes
This commit is contained in:
parent
d102fc7b80
commit
a1d1f7ba24
@ -3,21 +3,18 @@ using System;
|
|||||||
using Vector3 = UnityEngine.Vector3;
|
using Vector3 = UnityEngine.Vector3;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace LinearAlgebra
|
namespace LinearAlgebra {
|
||||||
{
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A spherical vector
|
/// A spherical vector
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <remark>This is a struct such that it is a value type and cannot be null
|
/// <remark>This is a struct such that it is a value type and cannot be null
|
||||||
public struct Spherical
|
public struct Spherical {
|
||||||
{
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Create a spherical vector
|
/// Create a spherical vector
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="distance">The distance in meters</param>
|
/// <param name="distance">The distance in meters</param>
|
||||||
/// <param name="direction">The direction of the vector</param>
|
/// <param name="direction">The direction of the vector</param>
|
||||||
public Spherical(float distance, Direction direction)
|
public Spherical(float distance, Direction direction) {
|
||||||
{
|
|
||||||
this.distance = distance;
|
this.distance = distance;
|
||||||
this.direction = direction;
|
this.direction = direction;
|
||||||
}
|
}
|
||||||
@ -29,15 +26,13 @@ namespace LinearAlgebra
|
|||||||
/// <param name="horizontal">The horizontal angle in degrees</param>
|
/// <param name="horizontal">The horizontal angle in degrees</param>
|
||||||
/// <param name="vertical">The vertical angle in degrees</param>
|
/// <param name="vertical">The vertical angle in degrees</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static Spherical Degrees(float distance, float horizontal, float vertical)
|
public static Spherical Degrees(float distance, float horizontal, float vertical) {
|
||||||
{
|
|
||||||
Direction direction = Direction.Degrees(horizontal, vertical);
|
Direction direction = Direction.Degrees(horizontal, vertical);
|
||||||
Spherical s = new(distance, direction);
|
Spherical s = new(distance, direction);
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Spherical Radians(float distance, float horizontal, float vertical)
|
public static Spherical Radians(float distance, float horizontal, float vertical) {
|
||||||
{
|
|
||||||
Direction direction = Direction.Radians(horizontal, vertical);
|
Direction direction = Direction.Radians(horizontal, vertical);
|
||||||
Spherical s = new(distance, direction);
|
Spherical s = new(distance, direction);
|
||||||
return s;
|
return s;
|
||||||
@ -62,79 +57,19 @@ namespace LinearAlgebra
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public readonly static Spherical forward = new(1, Direction.forward);
|
public readonly static Spherical forward = new(1, Direction.forward);
|
||||||
|
|
||||||
|
|
||||||
// public static Spherical FromVector3Float(Vector3Float v) {
|
|
||||||
// float distance = v.magnitude;
|
|
||||||
// if (distance == 0.0f)
|
|
||||||
// return Spherical.zero;
|
|
||||||
// else {
|
|
||||||
// float verticalAngle = (float)((Angle.pi / 2 - Math.Acos(v.y / distance)) * Angle.Rad2Deg);
|
|
||||||
// float horizontalAngle = (float)Math.Atan2(v.x, v.z) * Angle.Rad2Deg;
|
|
||||||
// return Spherical.Degrees(distance, horizontalAngle, verticalAngle);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
public static Spherical FromVector3Float(Vector3Float v)
|
|
||||||
{
|
|
||||||
float distance = v.magnitude;
|
|
||||||
if (distance == 0.0f)
|
|
||||||
return Spherical.zero;
|
|
||||||
else
|
|
||||||
{
|
|
||||||
float verticalAngle = (float)(Math.PI / 2 - Math.Acos(v.vertical / distance)) * AngleFloat.Rad2Deg;
|
|
||||||
float horizontalAngle = (float)Math.Atan2(v.horizontal, v.depth) * AngleFloat.Rad2Deg;
|
|
||||||
return Degrees(distance, horizontalAngle, verticalAngle);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// public Vector3Float ToVector3Float() {
|
|
||||||
// float verticalRad = (Angle.pi / 2) - this.direction.vertical * Angle.Deg2Rad;
|
|
||||||
// float horizontalRad = this.direction.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 = this.distance * sinVertical * sinHorizontal;
|
|
||||||
// float y = this.distance * cosVertical;
|
|
||||||
// float z = this.distance * sinVertical * cosHorizontal;
|
|
||||||
|
|
||||||
// Vector3Float v = new(x, y, z);
|
|
||||||
// return v;
|
|
||||||
// }
|
|
||||||
|
|
||||||
public Vector3Float ToVector3Float()
|
|
||||||
{
|
|
||||||
float verticalRad = (AngleFloat.deg90 - this.direction.vertical).inRadians;
|
|
||||||
float horizontalRad = this.direction.horizontal.inRadians;
|
|
||||||
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 = this.distance * sinVertical * sinHorizontal;
|
|
||||||
float y = this.distance * cosVertical;
|
|
||||||
float z = this.distance * sinVertical * cosHorizontal;
|
|
||||||
|
|
||||||
Vector3Float v = new(x, y, z);
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
#if UNITY_5_3_OR_NEWER
|
#if UNITY_5_3_OR_NEWER
|
||||||
public static Spherical FromVector3(Vector3 v)
|
public static Spherical FromVector3(Vector3 v) {
|
||||||
{
|
|
||||||
float distance = v.magnitude;
|
float distance = v.magnitude;
|
||||||
if (distance == 0.0f)
|
if (distance == 0.0f)
|
||||||
return Spherical.zero;
|
return Spherical.zero;
|
||||||
else
|
else {
|
||||||
{
|
|
||||||
float verticalAngle = (float)(Math.PI / 2 - Math.Acos(v.y / distance)) * AngleFloat.Rad2Deg;
|
float verticalAngle = (float)(Math.PI / 2 - Math.Acos(v.y / distance)) * AngleFloat.Rad2Deg;
|
||||||
float horizontalAngle = (float)Math.Atan2(v.x, v.z) * AngleFloat.Rad2Deg;
|
float horizontalAngle = (float)Math.Atan2(v.x, v.z) * AngleFloat.Rad2Deg;
|
||||||
return Degrees(distance, horizontalAngle, verticalAngle);
|
return Degrees(distance, horizontalAngle, verticalAngle);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Vector3 ToVector3()
|
public readonly Vector3 ToVector3() {
|
||||||
{
|
|
||||||
float verticalRad = (AngleFloat.deg90 - this.direction.vertical).inRadians;
|
float verticalRad = (AngleFloat.deg90 - this.direction.vertical).inRadians;
|
||||||
float horizontalRad = this.direction.horizontal.inRadians;
|
float horizontalRad = this.direction.horizontal.inRadians;
|
||||||
float cosVertical = (float)Math.Cos(verticalRad);
|
float cosVertical = (float)Math.Cos(verticalRad);
|
||||||
@ -149,21 +84,42 @@ namespace LinearAlgebra
|
|||||||
Vector3 v = new(x, y, z);
|
Vector3 v = new(x, y, z);
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
#endif
|
#else
|
||||||
|
public static Spherical FromVector3(Vector3Float v)
|
||||||
public float magnitude
|
|
||||||
{
|
{
|
||||||
get
|
float distance = v.magnitude;
|
||||||
|
if (distance == 0.0f)
|
||||||
|
return Spherical.zero;
|
||||||
|
else
|
||||||
{
|
{
|
||||||
return this.distance;
|
float verticalAngle = (float)(Math.PI / 2 - Math.Acos(v.vertical / distance)) * AngleFloat.Rad2Deg;
|
||||||
|
float horizontalAngle = (float)Math.Atan2(v.horizontal, v.depth) * AngleFloat.Rad2Deg;
|
||||||
|
return Degrees(distance, horizontalAngle, verticalAngle);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public Spherical normalized
|
|
||||||
{
|
public readonly Vector3Float ToVector3() {
|
||||||
get
|
float verticalRad = (AngleFloat.deg90 - this.direction.vertical).inRadians;
|
||||||
{
|
float horizontalRad = this.direction.horizontal.inRadians;
|
||||||
Spherical r = new()
|
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 = this.distance * sinVertical * sinHorizontal;
|
||||||
|
float y = this.distance * cosVertical;
|
||||||
|
float z = this.distance * sinVertical * cosHorizontal;
|
||||||
|
|
||||||
|
Vector3Float v = new(x, y, z);
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
public readonly float magnitude => this.distance;
|
||||||
|
|
||||||
|
public Spherical normalized {
|
||||||
|
get {
|
||||||
|
Spherical r = new() {
|
||||||
distance = 1,
|
distance = 1,
|
||||||
direction = this.direction
|
direction = this.direction
|
||||||
};
|
};
|
||||||
@ -171,13 +127,13 @@ namespace LinearAlgebra
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Spherical operator +(Spherical s1, Spherical s2)
|
public static Spherical operator +(Spherical s1, Spherical s2) {
|
||||||
{
|
|
||||||
// let's do it the easy way...
|
// let's do it the easy way...
|
||||||
Vector3Float v1 = s1.ToVector3Float();
|
// using vars to be compatible with both unity (Vector3) and native (Vector3Float)
|
||||||
Vector3Float v2 = s2.ToVector3Float();
|
var v1 = s1.ToVector3();
|
||||||
Vector3Float v = v1 + v2;
|
var v2 = s2.ToVector3();
|
||||||
Spherical r = FromVector3Float(v);
|
var v = v1 + v2;
|
||||||
|
Spherical r = FromVector3(v);
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user