-
Notifications
You must be signed in to change notification settings - Fork 0
/
fps_controler.go
84 lines (66 loc) · 1.71 KB
/
fps_controler.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
package glutils
import (
"math"
v "github.com/pzsz/lin3dmath"
)
type FpsController struct {
Camera *Camera
Pos v.Vector3f
HorAxis float32
VerAxis float32
}
func NewFpsController(camera *Camera) *FpsController {
ret := &FpsController{
Camera: camera}
return ret
}
func (s *FpsController) MoveBy(forward, strafe float32) {
s.Pos.AddIP(s.GetForwardVector().Mul(forward))
s.Pos.AddIP(s.GetStrafeVector().Mul(strafe))
}
func (s *FpsController) GetForwardVector() v.Vector3f {
return v.Vector3f{
float32(math.Sin(float64(-s.HorAxis))),
0,
float32(-math.Cos(float64(-s.HorAxis)))}
}
func (s *FpsController) GetViewVector() v.Vector3f {
hor_x := math.Sin(float64(-s.HorAxis))
hor_z := -math.Cos(float64(-s.HorAxis))
ver_len := math.Cos(float64(-s.VerAxis))
ver_hor := math.Sin(float64(-s.VerAxis))
return v.Vector3f{
float32(hor_x * ver_len),
float32(-ver_hor),
float32(hor_z * ver_len)}
}
func (s *FpsController) GetStrafeVector() v.Vector3f {
return v.Vector3f{
float32(math.Cos(float64(-s.HorAxis))),
0,
float32(math.Sin(float64(-s.HorAxis)))}
}
func (s *FpsController) RotateBy(deltaHor, deltaVer float32) {
s.HorAxis += deltaHor
if s.HorAxis > 2*math.Pi {
s.HorAxis -= 2*math.Pi
}
if s.HorAxis < 0 {
s.HorAxis += 2*math.Pi
}
s.VerAxis += deltaVer
if s.VerAxis > 0.49*math.Pi {
s.VerAxis = 0.49*math.Pi
}
if s.VerAxis < -0.49*math.Pi {
s.VerAxis = -0.49*math.Pi
}
}
func (s *FpsController) SetupCamera() {
hor := v.MatrixRotate(v.Angle(-s.HorAxis), 0, 1, 0)
ver := v.MatrixRotate(v.Angle(-s.VerAxis), 1, 0, 0)
tr := v.MatrixTranslate(-s.Pos.X, -s.Pos.Y, -s.Pos.Z)
rot := ver.Mul(hor)
mat := rot.Mul(tr)
s.Camera.SetCustomModelview(s.Pos.X, s.Pos.Y, s.Pos.Z, &mat)
}