-
Notifications
You must be signed in to change notification settings - Fork 0
/
vehicle.go
99 lines (78 loc) · 2.27 KB
/
vehicle.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
package main
import (
"math"
"log"
"github.com/golang/geo/r3"
)
type Vehicle struct {
x, y float64
a, b float64
alpha float64
}
// tang, tilt
func (v *Vehicle) FindDegrees(h func(float64, float64) float64) (float64, float64) {
alpha := v.alpha / 180 * math.Pi
xbr := v.x + v.a*math.Cos(alpha) + v.b*math.Sin(alpha)
xtr := v.x + v.a*math.Cos(alpha) - v.b*math.Sin(alpha)
xtl := v.x - v.a*math.Cos(alpha) - v.b*math.Sin(alpha)
xbl := v.x - v.a*math.Cos(alpha) + v.b*math.Sin(alpha)
ybr := v.y + v.a*math.Sin(alpha) - v.b*math.Cos(alpha)
ytr := v.y + v.a*math.Sin(alpha) + v.b*math.Cos(alpha)
ytl := v.y - v.a*math.Sin(alpha) + v.b*math.Cos(alpha)
ybl := v.y - v.a*math.Sin(alpha) - v.b*math.Cos(alpha)
ztr := h(xtr, ytr)
ztl := h(xtl, ytl)
zbl := h(xbl, ybl)
zbr := h(xbr, ybr)
tltr := r3.Vector{X: xtr - xtl, Y: ytr - ytl, Z: ztr - ztl}
trbr := r3.Vector{X: xbr - xtr, Y: ybr - ytr, Z: zbr - ztr}
brbl := r3.Vector{X: xbl - xbr, Y: ybl - ybr, Z: zbl - zbr}
bltl := r3.Vector{X: xtl - xbl, Y: ytl - ybl, Z: ztl - zbl}
ntl := bltl.Cross(tltr).Normalize()
ntr := tltr.Cross(trbr).Normalize()
nbr := trbr.Cross(brbl).Normalize()
nbl := brbl.Cross(bltl).Normalize()
n := ntl.Add(ntr).Add(nbr).Add(nbl).Mul(0.25)
return 90 - n.Angle(r3.Vector{X: -math.Sin(alpha), Y: math.Cos(alpha), Z: 0}).Degrees(), -90 + n.Angle(r3.Vector{X: math.Cos(alpha), Y: math.Sin(alpha), Z: 0}).Degrees()
}
func (v *Vehicle) TurnRight(dAlpha float64) {
v.alpha -= dAlpha
}
func (v *Vehicle) TurnLeft(dAlpha float64) {
v.TurnRight(-dAlpha)
}
func (v *Vehicle) MoveForward(dDist float64) {
alpha := v.alpha / 180 * math.Pi
v.x += -dDist * math.Sin(alpha)
v.y += dDist * math.Cos(alpha)
}
func (v *Vehicle) FindDeviation(x, y float64) float64 {
x -= v.x
y -= v.y
log.Println("Dev:", x, y)
ang := r3.Vector{x, y, 0}.Angle(r3.Vector{0, 1, 0}).Degrees()
log.Println("Dev_ang", ang)
if x > 0 {
ang = -ang
}
log.Println("Dev_ang", ang)
ang -= v.alpha //- ang
for ang > 180 {
ang -= 360
}
for ang < -180 {
ang += 360
}
return -ang
}
type VehiclePosition struct {
x, y, alpha float64
}
func Pos(v *Vehicle) VehiclePosition {
return VehiclePosition{x: v.x, y: v.y, alpha: v.alpha}
}
func (v *Vehicle) ApplyPosition(pos *VehiclePosition) {
v.alpha = pos.alpha
v.x = pos.x
v.y = pos.y
}