-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvector3.go
185 lines (157 loc) · 3.5 KB
/
vector3.go
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package math
type Vector3 struct {
X float32
Y float32
Z float32
}
func Vec3(x, y, z float32) Vector3 {
return Vector3{x, y, z}
}
func (vec *Vector3) Set(x, y, z float32) Vector3 {
vec.X = x
vec.Y = y
vec.Z = z
return *vec
}
func (vec *Vector3) SetVec2(v Vector2) Vector3 {
vec.X = v.X
vec.Y = v.Y
return *vec
}
func (vec *Vector3) SetVec3(v Vector3) Vector3 {
vec.X = v.X
vec.Y = v.Y
vec.Z = v.Z
return *vec
}
func (vec Vector3) Vec2() Vector2 {
return Vec2(vec.X, vec.Y)
}
func (vec Vector3) Cpy() Vector3 {
return Vector3{vec.X, vec.Y, vec.Z}
}
// Returns a zero vector
func (vec Vector3) Clr() Vector3 {
vec.X = 0
vec.Y = 0
vec.Z = 0
return vec
}
func (vec Vector3) Add(vec2 Vector3) Vector3 {
vec.X += vec2.X
vec.Y += vec2.Y
vec.Z += vec2.Z
return vec
}
func (vec Vector3) Sub(vec2 Vector3) Vector3 {
vec.X -= vec2.X
vec.Y -= vec2.Y
vec.Z -= vec2.Z
return vec
}
func (vec Vector3) Mul(vec2 Vector3) Vector3 {
vec.X *= vec2.X
vec.Y *= vec2.Y
vec.Z *= vec2.Z
return vec
}
func (vec Vector3) Div(vec2 Vector3) Vector3 {
vec.X /= vec2.X
vec.Y /= vec2.Y
vec.Z /= vec2.Z
return vec
}
// The euclidian length
func (vec Vector3) Len() float32 {
return Sqrt(vec.X*vec.X + vec.Y*vec.Y + vec.Z*vec.Z)
}
// The squared euclidian length
func (vec Vector3) Len2() float32 {
return vec.X*vec.X + vec.Y*vec.Y + vec.Z*vec.Z
}
func (vec Vector3) Distance(vec2 Vector3) float32 {
return Sqrt(vec.Distance2(vec2))
}
// Returns the squared distance between this point and the given point
func (vec Vector3) Distance2(vec2 Vector3) float32 {
a := vec2.X - vec.X
b := vec2.Y - vec.Y
c := vec2.Z - vec.Z
a *= a
b *= b
c *= c
return a + b + c
}
func (vec Vector3) Nor() Vector3 {
l := vec.Len()
if l == 0 {
return vec
}
return vec.Scale(1 / l)
}
func (vec Vector3) Dot(vec2 Vector3) float32 {
return vec.X*vec2.X + vec.Y*vec2.Y + vec.Z*vec2.Z
}
// Returns the cross product between this vector and the other vector
func (vec Vector3) Cross(vec2 Vector3) Vector3 {
x := vec.Y*vec2.Z - vec.Z*vec2.Y
y := vec.Z*vec2.X - vec.X*vec2.Z
z := vec.X*vec2.Y - vec.Y*vec2.X
vec.X = x
vec.Y = y
vec.Z = z
return vec
}
// Whether this vector is a unit length vector
func (vec Vector3) IsUnit() bool {
return vec.Len() == 1
}
func (vec Vector3) IsZero() bool {
return vec.X == 0 && vec.Y == 0 && vec.Z == 0
}
// Linearly interpolates between this vector and the target vector by alpha which is in the range [0,1].
func (vec Vector3) Lerp(target Vector3, alpha float32) Vector3 {
vec.Scale(1.0 - alpha)
vec.Add(target.Cpy().Scale(alpha))
return vec
}
// Spherically interpolates between this vector and the target vector by alpha which is in the range [0,1].
func (vec Vector3) Slerp(target Vector3, alpha float32) Vector3 {
dot := vec.Dot(target)
if dot > 0.99995 || dot < 0.9995 {
vec = vec.Add(target.Sub(vec).Scale(alpha))
vec = vec.Nor()
return vec
}
if dot > 1 {
dot = 1
}
if dot < -1 {
dot = -1
}
theta0 := Acos(dot)
theta := theta0 * alpha
v2 := target.Sub(Vec3(vec.X*dot, vec.Y*dot, vec.Z*dot))
v2 = v2.Nor()
return vec.Scale(Cos(theta)).Add(v2.Scale(Sin(theta))).Nor()
}
// Returns this vector, it's length limited to given value.
func (vec Vector3) Limit(limit float32) Vector3 {
if vec.Len2() > limit*limit {
vec = vec.Nor()
vec = vec.Scale(limit)
}
return vec
}
func (vec Vector3) Scale(scalar float32) Vector3 {
vec.X *= scalar
vec.Y *= scalar
vec.Z *= scalar
return vec
}
func (vec Vector3) Invert() Vector3 {
vec.X = -vec.X
vec.Y = -vec.Y
vec.Z = -vec.Z
return vec
}