-
Notifications
You must be signed in to change notification settings - Fork 4
/
vector.go
127 lines (105 loc) · 2.57 KB
/
vector.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
_ "embed"
"fmt"
"gonum.org/v1/plot/plotter"
"math"
)
//go:embed drag.json
var dragTable []byte
type DragTable map[string]float64
var dragTableJson = DragTable{}
const timeStep = 1.0 / 60
type vector3 struct {
x float64
y float64
}
func (v vector3) magnitude() float64 {
return math.Sqrt(v.x*v.x + v.y*v.y)
}
func (v vector3) subtract(vector32 vector3) vector3 {
newVector := vector3{}
newVector.x = v.x - vector32.x
newVector.y = v.y - vector32.y
return newVector
}
func (v vector3) add(vector32 vector3) vector3 {
newVector := vector3{}
newVector.x = v.x + vector32.x
newVector.y = v.y + vector32.y
return newVector
}
func (v vector3) mul(vector32 vector3) vector3 {
newVector := vector3{}
newVector.x = v.x * vector32.x
newVector.y = v.y * vector32.y
return newVector
}
func (v vector3) divide(vector32 vector3) vector3 {
newVector := vector3{}
newVector.x = v.x / vector32.x
newVector.y = v.y / vector32.y
return newVector
}
func (v vector3) lerp(vector vector3, t float64) vector3 {
a := vector3{
x: v.x * t,
y: v.y * t,
}
b := vector3{
x: vector.x * (1.0 - t),
y: vector.y * (1.0 - t),
}
return a.add(b)
}
func CalculateForAngle(delta, testAngle, muz float64, drag bool) (float64, float64, plotter.XYs) {
newPlot := plotter.XYs{}
trueDrag := 0.0
if drag {
trueDrag = dragTableJson[currentGunString]
}
k := -1 * trueDrag * 1
temperature := 15.0
currentPos := vector3{
x: 0,
y: 0,
}
lastPos := currentPos
currentVelocity := vector3{
x: muz * math.Cos(testAngle/r2m) * (((temperature+273.13)/288.13-1.0)/40.0 + 1.0),
y: muz * math.Sin(testAngle/r2m) * (((temperature+273.13)/288.13-1.0)/40.0 + 1.0),
}
wind := vector3{
x: 0,
y: 0,
}
grav := vector3{
x: 0,
y: -9.8066,
}
currentTime := 0.0
for currentVelocity.y > 0 || currentPos.y >= delta {
lastPos = currentPos
apparentWind := wind.subtract(currentVelocity)
changeInVelocity := grav.add(apparentWind.mul(vector3{
x: k * apparentWind.magnitude(),
y: k * apparentWind.magnitude(),
}))
fmt.Println(changeInVelocity)
currentVelocity = vector3{
x: currentVelocity.x + (changeInVelocity.x * timeStep),
y: currentVelocity.y + (changeInVelocity.y * timeStep),
}
currentPos = vector3{
x: currentPos.x + (currentVelocity.x * timeStep),
y: currentPos.y + (currentVelocity.y * timeStep),
}
currentTime += timeStep
newPlot = append(newPlot, plotter.XY{
X: currentPos.x,
Y: currentPos.y,
})
}
lastRatio := (delta - currentPos.y) / (lastPos.y - currentPos.y)
return lastPos.lerp(currentPos, lastRatio).x, currentTime, newPlot
}