-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaths.cs
More file actions
43 lines (32 loc) · 1.4 KB
/
Maths.cs
File metadata and controls
43 lines (32 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using System;
using UnityEngine;
namespace com.drowmods.DistanceTelemetryMod
{
public static class Maths
{
/// <summary>
/// Convert quaternion to Euler angles
/// </summary>
/// <param name="q"></param>(float pitch, float yaw, float roll)
/// <returns>pitch(x-rotation), yaw (y-rotation) , roll (z-rotation)</returns>
public static Vector3 ToEuler(this Quaternion q, bool returnDegrees = true)
{
var p = (float)q.ToPitch();
var y = (float)q.ToYaw();
var r = (float)q.ToRoll();
if (!returnDegrees)
return new Vector3(p, y, r);
// Convert the angles from radians to degrees
return new Vector3(p * Mathf.Rad2Deg, y * Mathf.Rad2Deg, r * Mathf.Rad2Deg);
}
private static double ToPitch(this Quaternion q)
{
double num = 2.0 * (q.x * q.y + q.w * q.y);
double num2 = 2.0 * (q.w * q.x - q.y * q.z);
double num3 = 1.0 - 2.0 * (q.x * q.x + q.y * q.y);
return Math.Atan2(num2, Math.Sqrt(num * num + num3 * num3));
}
private static double ToYaw(this Quaternion q) => Math.Atan2(2.0 * (q.x * q.y + q.w * q.y), 1.0 - 2.0 * (q.x * q.x + q.y * q.y));
private static double ToRoll(this Quaternion q) => Math.Atan2(2.0 * (q.x * q.y + q.w * q.z), 1.0 - 2.0 * (q.x * q.x + q.z * q.z));
}
}