-
Notifications
You must be signed in to change notification settings - Fork 8
/
math_nodeps.go
52 lines (42 loc) · 1.26 KB
/
math_nodeps.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
//go:build !gmath
package input
import (
"math"
)
// We're not using any math/vector library to make it possible for the users
// to use any kind of math library they like without having to have more
// than one math library inside their project.
//
// This means that we'll have to implement some math here,
// but it's worth it.
// Vec is a simple wrapper around a pair of float64 coordinates.
//
// Since most games use float values for most values, input library
// converts int pair to the float pair once per Update() call so
// all usages inside the frame can use already converted values.
//
// We're not using some vector2d library to avoid extra dependencies.
// It should be easy to convert this Point object into any other structure.
type Vec struct {
X float64
Y float64
}
func vecDistance(v, v2 Vec) float64 {
return math.Sqrt((v.X-v2.X)*(v.X-v2.X) + (v.Y-v2.Y)*(v.Y-v2.Y))
}
func vecDot(v, v2 Vec) float64 {
return (v.X * v2.X) + (v.Y * v2.Y)
}
func vecLenSquared(v Vec) float64 {
return vecDot(v, v)
}
func vecLen(v Vec) float64 {
return math.Sqrt(vecLenSquared(v))
}
func vecAngle(v Vec) float64 {
return math.Atan2(v.Y, v.X)
}
func angleNormalized(radians float64) float64 {
radians -= math.Floor(radians/(2*math.Pi)) * 2 * math.Pi
return radians
}