forked from Nivekk/KOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirection.cs
More file actions
110 lines (96 loc) · 3.09 KB
/
Direction.cs
File metadata and controls
110 lines (96 loc) · 3.09 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace kOS
{
public class Direction : SpecialValue
{
private Vector3d vector;
public Vector3d Vector
{
get { return vector; }
set
{
vector = value; rotation = Quaternion.LookRotation(value); euler = rotation.eulerAngles;
}
}
private Vector3d euler;
public Vector3d Euler
{
get { return euler; }
set
{
euler = value; rotation = Quaternion.Euler(value);
}
}
private Quaternion rotation;
public Quaternion Rotation
{
get { return rotation; }
set { rotation = value; euler = value.eulerAngles; }
}
public Direction()
{
}
public Direction(Quaternion q)
{
rotation = q;
euler = q.eulerAngles;
}
public Direction(Vector3d v3d, bool isEuler)
{
if (isEuler)
{
Euler = v3d;
}
else
{
Vector = v3d;
}
}
public override object GetSuffix(string suffixName)
{
if (suffixName == "PITCH") return euler.x;
if (suffixName == "YAW") return euler.y;
if (suffixName == "ROLL") return euler.z;
if (suffixName == "VECTOR") return new kOS.Vector(vector);
return base.GetSuffix(suffixName);
}
public void RedefineUp(Vector3d up)
{
}
public static Direction operator *(Direction a, Direction b) { return new Direction(a.Rotation * b.Rotation); }
public static Direction operator +(Direction a, Direction b) { return new Direction(a.Euler + b.Euler, true); }
public static Direction operator -(Direction a, Direction b) { return new Direction(a.Euler - b.Euler, true); }
public override object TryOperation(string op, object other, bool reverseOrder)
{
if (other is Vector)
{
other = ((Vector)other).ToDirection();
}
if (op == "*" && other is Direction)
{
// If I remember correctly, order of multiplication DOES matter with quaternions
if (!reverseOrder)
return this * (Direction)other;
else
return (Direction)other * this;
}
else if (op == "+" && other is Direction) return this + (Direction)other;
else if (op == "-" && other is Direction)
{
if (!reverseOrder)
return this - (Direction)other;
else
return (Direction)other - this;
}
return null;
}
public override string ToString()
{
return "R(" + Math.Round(euler.x, 3) + "," + Math.Round(euler.y, 3) + "," + Math.Round(euler.z, 3) + ")";
}
}
}