Clean up
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
using GTA.Math;
|
||||
using System;
|
||||
using System;
|
||||
using GTA.Math;
|
||||
|
||||
namespace RageCoop.Core
|
||||
{
|
||||
@ -10,9 +10,9 @@ namespace RageCoop.Core
|
||||
|
||||
public static Vector3 ToDirection(this Vector3 rotation)
|
||||
{
|
||||
double z = DegToRad(rotation.Z);
|
||||
double x = DegToRad(rotation.X);
|
||||
double num = Math.Abs(Math.Cos(x));
|
||||
var z = DegToRad(rotation.Z);
|
||||
var x = DegToRad(rotation.X);
|
||||
var num = Math.Abs(Math.Cos(x));
|
||||
|
||||
return new Vector3
|
||||
{
|
||||
@ -23,11 +23,10 @@ namespace RageCoop.Core
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public static Vector3 ToVector(this Quaternion vec)
|
||||
{
|
||||
return new Vector3()
|
||||
return new Vector3
|
||||
{
|
||||
X = vec.X,
|
||||
Y = vec.Y,
|
||||
@ -36,11 +35,10 @@ namespace RageCoop.Core
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public static Quaternion ToQuaternion(this Vector3 vec, float vW = 0.0f)
|
||||
{
|
||||
return new Quaternion()
|
||||
return new Quaternion
|
||||
{
|
||||
X = vec.X,
|
||||
Y = vec.Y,
|
||||
@ -61,35 +59,35 @@ namespace RageCoop.Core
|
||||
|
||||
public static Vector3 ToRadians(this Vector3 i)
|
||||
{
|
||||
return new Vector3()
|
||||
return new Vector3
|
||||
{
|
||||
X = ToRadians(i.X),
|
||||
Y = ToRadians(i.Y),
|
||||
Z = ToRadians(i.Z),
|
||||
Z = ToRadians(i.Z)
|
||||
};
|
||||
}
|
||||
|
||||
public static Quaternion ToQuaternion(this Vector3 vect)
|
||||
{
|
||||
vect = new Vector3()
|
||||
vect = new Vector3
|
||||
{
|
||||
X = vect.X.Denormalize() * -1,
|
||||
Y = vect.Y.Denormalize() - 180f,
|
||||
Z = vect.Z.Denormalize() - 180f,
|
||||
Z = vect.Z.Denormalize() - 180f
|
||||
};
|
||||
|
||||
vect = vect.ToRadians();
|
||||
|
||||
float rollOver2 = vect.Z * 0.5f;
|
||||
float sinRollOver2 = (float)Math.Sin(rollOver2);
|
||||
float cosRollOver2 = (float)Math.Cos(rollOver2);
|
||||
float pitchOver2 = vect.Y * 0.5f;
|
||||
float sinPitchOver2 = (float)Math.Sin(pitchOver2);
|
||||
float cosPitchOver2 = (float)Math.Cos(pitchOver2);
|
||||
float yawOver2 = vect.X * 0.5f; // pitch
|
||||
float sinYawOver2 = (float)Math.Sin(yawOver2);
|
||||
float cosYawOver2 = (float)Math.Cos(yawOver2);
|
||||
Quaternion result = new Quaternion()
|
||||
var rollOver2 = vect.Z * 0.5f;
|
||||
var sinRollOver2 = (float)Math.Sin(rollOver2);
|
||||
var cosRollOver2 = (float)Math.Cos(rollOver2);
|
||||
var pitchOver2 = vect.Y * 0.5f;
|
||||
var sinPitchOver2 = (float)Math.Sin(pitchOver2);
|
||||
var cosPitchOver2 = (float)Math.Cos(pitchOver2);
|
||||
var yawOver2 = vect.X * 0.5f; // pitch
|
||||
var sinYawOver2 = (float)Math.Sin(yawOver2);
|
||||
var cosYawOver2 = (float)Math.Cos(yawOver2);
|
||||
var result = new Quaternion
|
||||
{
|
||||
X = cosYawOver2 * cosPitchOver2 * cosRollOver2 + sinYawOver2 * sinPitchOver2 * sinRollOver2,
|
||||
Y = cosYawOver2 * cosPitchOver2 * sinRollOver2 - sinYawOver2 * sinPitchOver2 * cosRollOver2,
|
||||
@ -98,27 +96,31 @@ namespace RageCoop.Core
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
public static double DegToRad(double deg)
|
||||
{
|
||||
return deg * Math.PI / 180.0;
|
||||
}
|
||||
|
||||
public static Vector3 ToEulerRotation(this Vector3 dir, Vector3 up)
|
||||
{
|
||||
var rot = Quaternion.LookRotation(dir.Normalized, up).ToEulerAngles().ToDegree();
|
||||
return rot;
|
||||
|
||||
}
|
||||
|
||||
public static Vector3 ToDegree(this Vector3 radian)
|
||||
{
|
||||
return radian * (float)(180 / Math.PI);
|
||||
}
|
||||
|
||||
public static Vector3 ToEulerDegrees(this Quaternion q)
|
||||
{
|
||||
return q.ToEulerAngles().ToDegree();
|
||||
}
|
||||
|
||||
public static Vector3 ToEulerAngles(this Quaternion q)
|
||||
{
|
||||
Vector3 angles = new Vector3();
|
||||
var angles = new Vector3();
|
||||
|
||||
// roll / x
|
||||
double sinr_cosp = 2 * (q.W * q.X + q.Y * q.Z);
|
||||
@ -128,13 +130,9 @@ namespace RageCoop.Core
|
||||
// pitch / y
|
||||
double sinp = 2 * (q.W * q.Y - q.Z * q.X);
|
||||
if (Math.Abs(sinp) >= 1)
|
||||
{
|
||||
angles.Y = CopySign(Math.PI / 2, sinp);
|
||||
}
|
||||
else
|
||||
{
|
||||
angles.Y = (float)Math.Asin(sinp);
|
||||
}
|
||||
|
||||
// yaw / z
|
||||
double siny_cosp = 2 * (q.W * q.Z + q.X * q.Y);
|
||||
@ -143,23 +141,22 @@ namespace RageCoop.Core
|
||||
|
||||
return angles;
|
||||
}
|
||||
|
||||
private static float CopySign(double x, double y)
|
||||
{
|
||||
if (y >= 0)
|
||||
{
|
||||
return x >= 0 ? (float)x : (float)-x;
|
||||
}
|
||||
if (y >= 0) return x >= 0 ? (float)x : (float)-x;
|
||||
|
||||
return x >= 0 ? (float)-x : (float)x;
|
||||
}
|
||||
|
||||
public static double AngelTo(this Vector3 v1, Vector3 v2)
|
||||
{
|
||||
return Math.Acos(v1.GetCosTheta(v2));
|
||||
}
|
||||
|
||||
public static float GetCosTheta(this Vector3 v1, Vector3 v2)
|
||||
{
|
||||
return Vector3.Dot(v1, v2) / (v1.Length() * v2.Length());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user