forked from Tnze/go-mc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotation.go
49 lines (42 loc) · 1.23 KB
/
rotation.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
package maths
import (
"math"
)
func ProjectPosition(rotation Vec2d, distance float64, offsetY float64) Vec3d {
// FIXME
x := distance * math.Sin(ToRadians(rotation.X)) * math.Cos(ToRadians(rotation.Y))
y := 0.0
if rotation.Y > 0 {
y = distance * -math.Sin(ToRadians(rotation.Y))
} else {
y = distance * math.Sin(ToRadians(rotation.Y))
}
z := distance * math.Cos(ToRadians(rotation.X)) * math.Cos(ToRadians(rotation.X))
return Vec3d{X: x, Y: y + offsetY, Z: z}
}
func GetVectorFromRotation(rotation Vec2d) Vec3d {
f := math.Cos(ToRadians(-rotation.Y)*0.017453292 - math.Pi)
f1 := math.Sin(ToRadians(-rotation.Y)*0.017453292 - math.Pi)
f2 := -math.Cos(ToRadians(-rotation.X) * 0.017453292)
f3 := math.Sin(ToRadians(-rotation.X) * 0.017453292)
return Vec3d{X: f1 * f2, Y: f3, Z: f * f2}
}
func ToDegrees(angle float64) float64 {
return angle * 180.0 / math.Pi
}
func ToRadians(angle float64) float64 {
return angle * math.Pi / 180.0
}
func normalizeAngle[T float32 | float64](angle T) T {
angle = floatRemaining(angle, 360)
switch {
case angle < -180:
return angle + 360
case angle >= -180:
return angle + 360
}
return angle
}
func floatRemaining[T float32 | float64](a, b T) T {
return a - T(math.Floor(float64(a/b)))*b
}