-
Notifications
You must be signed in to change notification settings - Fork 0
/
traveler.go
93 lines (75 loc) · 1.99 KB
/
traveler.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
package main
import (
// "fmt" // tests
// "errors"
"github.com/go-gl/mathgl/mgl32"
"math"
)
const TravelerAcceleration = 16 // units per second*second
const TravelerInitMovSpeed = 1 // units per second
const TravelerMaxMovSpeed = 40 // units per second
const TravelerRotSpeed = 90 // degrees per second
type Traveler struct {
Position mgl32.Vec3 // position
Rot RotHVType // orientation
MovSpeed float64
RotSpeed float64
CapturedBy *MonsterType
}
func (t *Traveler) ViewMatrix() mgl32.Mat4 {
c1 := float32(math.Cos(-t.Rot.XZ * degToRadians))
s1 := float32(math.Sin(-t.Rot.XZ * degToRadians))
c2 := float32(math.Cos(-t.Rot.YZ * degToRadians))
s2 := float32(math.Sin(-t.Rot.YZ * degToRadians))
v := t.Rot.ViewerRotatedVector(t.Position.Mul(-1))
// row-major ??
return mgl32.Mat4{
c1, 0, -s1, v[0],
-s2 * s1, c2, -s2 * c1, v[1],
c2 * s1, s2, c2 * c1, v[2],
0, 0, 0, 1,
}.Transpose()
}
func (t *Traveler) Move(dx, dy, dz float32) {
v := t.Rot.WorldRotatedVector(mgl32.Vec3{dx, dy, dz})
t.Position = t.Position.Add(v)
}
func (t *Traveler) ClipToBox(vmin, vmax mgl32.Vec3) {
if t.Position[0] > vmax[0] {
t.Position[0] = vmax[0]
}
if t.Position[0] < vmin[0] {
t.Position[0] = vmin[0]
}
if t.Position[1] > vmax[1] {
t.Position[1] = vmax[1]
}
if t.Position[1] < vmin[1] {
t.Position[1] = vmin[1]
}
if t.Position[2] > vmax[2] {
t.Position[2] = vmax[2]
}
if t.Position[2] < vmin[2] {
t.Position[2] = vmin[2]
}
}
func MakeTraveler(position mgl32.Vec3) *Traveler {
var t Traveler
t.Position = position
t.MovSpeed = TravelerInitMovSpeed
t.RotSpeed = TravelerRotSpeed
return &t
}
func (t *Traveler) Update(g *Mki3dGame) {
if t.CapturedBy != nil {
t.Position = t.CapturedBy.Position
g.StageDSPtr.UniPtr.ViewUni = g.TravelerPtr.ViewMatrix()
}
}
func (t *Traveler) UpdateSpeed(timeDelta float64) {
t.MovSpeed = math.Min(t.MovSpeed+timeDelta*TravelerAcceleration, TravelerMaxMovSpeed)
}
func (t *Traveler) ResetSpeed() {
t.MovSpeed = TravelerInitMovSpeed
}