-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelbing.go
244 lines (184 loc) · 5.17 KB
/
helbing.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package main
import (
crand "crypto/rand"
"fmt"
"image/color"
"math"
"math/big"
"math/rand"
"github.com/faiface/pixel"
"github.com/faiface/pixel/imdraw"
"github.com/faiface/pixel/pixelgl"
)
const width, height = float64(8), float64(4)
const ratio = float64(100)
const (
ti = 0.03
t = 0.03
)
// Ai is a constant of repulsive interaction force.
var Ai = math.Pow(2.0, 3)
// Bi is a constant of repulsive interaction force.
var Bi = 0.08
// k1 is a constant of body force.
var k1 = math.Pow(1.2, 5) // 1.2e5
// k2 is a constant of sliding friction force.
var k2 = math.Pow(2.4, 5) // 2.4e5
var pedestrians [256]*pedestrian
type pedestrian struct {
pixel.Vec
stateCurrent int
stateFuture int
locationX [2]float64
locationY [2]float64
desiredVelocityX float64
desiredVelocityY float64
desiredVelocity float64
velocityX [2]float64
velocityY [2]float64
weight float64
bodyRadius float64
C color.RGBA
}
func newPedestrian() *pedestrian {
p := new(pedestrian)
p.stateCurrent = 0
p.stateFuture = 1
p.locationX[p.stateCurrent] = random(0, width)
p.locationY[p.stateCurrent] = random(0, height)
p.weight = 60
p.bodyRadius = 0.3
var pi float64
if rand.Intn(2) == 0 {
pi = 0
p.C = color.RGBA{0, 255, 0, 255}
} else {
pi = math.Pi
p.C = color.RGBA{255, 0, 255, 255}
}
var distance float64
distance = rand.Float64()*0.6 + 1.2
p.desiredVelocityX = distance * math.Cos(pi)
p.desiredVelocityY = distance * math.Sin(pi)
return p
}
func (p *pedestrian) update() {
if p.locationX[p.stateFuture] > width {
p.locationX[p.stateFuture] = p.locationX[p.stateFuture] - width
} else if p.locationX[p.stateFuture] < 0 {
p.locationX[p.stateFuture] = width + p.locationX[p.stateFuture]
}
if p.locationY[p.stateFuture] > height {
p.locationY[p.stateFuture] = p.locationY[p.stateFuture] - height
} else if p.locationY[p.stateFuture] < 0 {
p.locationY[p.stateFuture] = height + p.locationY[p.stateFuture]
}
tmp := p.stateCurrent
p.stateCurrent = p.stateFuture
p.stateFuture = tmp
//p.X = p.X + 1
//p.Y = p.Y - 1
}
func (p *pedestrian) draw(imd *imdraw.IMDraw) {
pix := pixel.V(
//width/2-p.X,
//height/2-p.Y,
(width/2-p.locationX[p.stateCurrent])*ratio,
(height/2-p.locationY[p.stateCurrent])*ratio,
)
fmt.Printf("x: %f, y:%f\n", p.locationX[p.stateCurrent], p.locationY[p.stateCurrent])
imd.Color = p.C
imd.Push(pix)
imd.Circle(p.bodyRadius*ratio/5, 1)
}
func main() {
seed, _ := crand.Int(crand.Reader, big.NewInt(math.MaxInt64))
rand.Seed(seed.Int64())
for i := 0; i < len(pedestrians); i++ {
pedestrians[i] = newPedestrian()
}
pixelgl.Run(func() {
win, err := pixelgl.NewWindow(pixelgl.WindowConfig{
Bounds: pixel.R(0, 0, width*ratio, height*ratio),
VSync: true,
Undecorated: false,
})
if err != nil {
panic(err)
}
imd := imdraw.New(nil)
imd.Precision = 7
imd.SetMatrix(pixel.IM.Moved(win.Bounds().Center()))
for !win.Closed() {
win.SetClosed(win.JustPressed(pixelgl.KeyEscape) || win.JustPressed(pixelgl.KeyQ))
imd.Clear()
for i := 0; i < len(pedestrians); i++ {
pi := pedestrians[i]
m := pi.weight
var fX, fY float64
maX := m * (pi.desiredVelocityX - pi.velocityX[pi.stateCurrent]) / ti
maY := m * (pi.desiredVelocityY - pi.velocityY[pi.stateCurrent]) / ti
fX += maX
fY += maY
var fijX, fijY float64
for j := 0; j < len(pedestrians); j++ {
if i == j {
continue
}
pj := pedestrians[j]
diffX := pi.locationX[pi.stateCurrent] - pj.locationX[pj.stateCurrent]
diffY := pi.locationY[pi.stateCurrent] - pj.locationY[pj.stateCurrent]
disX := math.Abs(diffX)
disY := math.Abs(diffY)
if disX >= -15 && disX <= 15 && disY >= -15 && disY <= 15 {
dij := math.Sqrt(disX*disX + disY*disY)
nijX := diffX / dij
nijY := diffY / dij
rij := pi.bodyRadius + pj.bodyRadius
tijX := -nijY
tijY := nijX
dvjitX := (pj.velocityX[pj.stateCurrent] - pi.velocityX[pi.stateCurrent]) * tijX
dvjitY := (pj.velocityY[pj.stateCurrent] - pi.velocityY[pi.stateCurrent]) * tijY
funcG := rij - dij
if funcG < 0 {
funcG = 0
}
appr := (rij - dij) / Bi
fExp := Ai*math.Exp(appr) + k1*funcG
fijX += fExp*nijX + k2*funcG*dvjitX*tijX
fijY += fExp*nijY + k2*funcG*dvjitY*tijY
}
}
fX += fijX
fY += fijY
v := math.Sqrt(math.Pow(pi.desiredVelocityX, 2)+math.Pow(pi.desiredVelocityY, 2)) / t * m
fLength := math.Sqrt(math.Pow(fX, 2) + math.Pow(fY, 2))
if fX > v {
fX = fX / fLength * v
}
if fX < -v {
fX = fX / fLength * v
}
if fY > v {
fY = fY / fLength * v
}
if fY < -v {
fY = fY / fLength * v
}
pi.velocityX[pi.stateFuture] = (fX / m) * t
pi.velocityY[pi.stateFuture] = (fY / m) * t
pi.locationX[pi.stateFuture] = pi.locationX[pi.stateCurrent] + (fX/(2*m))*t*t
pi.locationY[pi.stateFuture] = pi.locationY[pi.stateCurrent] + (fY/(2*m))*t*t
pi.draw(imd)
pi.update()
}
win.Clear(color.Black)
imd.Draw(win)
win.Update()
}
},
)
}
func random(min, max float64) float64 {
return rand.Float64() * (max - min)
}