-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboids.go
More file actions
147 lines (122 loc) · 3.27 KB
/
boids.go
File metadata and controls
147 lines (122 loc) · 3.27 KB
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
package main
import (
"github.com/go-gl/mathgl/mgl32"
"image/color"
"math"
)
func dist(p, q mgl32.Vec2) float32 {
return float32(math.Sqrt(math.Pow(float64(q[0] - p[0]), 2) + math.Pow(float64(q[1] - p[1]), 2)))
}
func limit(p mgl32.Vec2, lim float32) mgl32.Vec2{
if p[0] > lim {
p[0] = lim
} else if p[0] < -lim {
p[0] = -lim
}
if p[1] > lim {
p[1] = lim
} else if p[1] < -lim {
p[1] = -lim
}
return p
}
var alignmentPerception = float32(20.0)
var cohesionPerception = float32(40.0)
var divisionPerception = float32(20.0)
var alignmentCoef = float32(1.0)
var cohesionCoef = float32(1.0)
var divisionCoef = float32(1.0)
var maxSpeed = float32(4)
var maxForce = float32(1)
var avg mgl32.Vec2
var total int
type Boid struct{
siblings *[]Boid
position mgl32.Vec2
velocity mgl32.Vec2
acceleration mgl32.Vec2
col color.RGBA
}
func NewBoid(x, y float32, boids *[]Boid) Boid {
b := Boid{
siblings: boids,
position:mgl32.Vec2{x, y},
velocity:mgl32.Vec2{(r.Float32() * 3) - 1.5, (r.Float32() * 3) - 1.5},
acceleration:mgl32.Vec2{0,0},
col:color.RGBA{uint8(r.Intn(255)), uint8(r.Intn(255)), uint8(r.Intn(255)), 0xff},
}
return b
}
func (boid* Boid) alignment() mgl32.Vec2 {
avg = mgl32.Vec2{0,0}
total = 0
for _, sibling := range *boid.siblings {
if (sibling != *boid) && dist(boid.position, sibling.position) < alignmentPerception {
avg = avg.Add(sibling.velocity)
total++
}
}
if total > 0 {
avg = avg.Mul(1.0 / float32(total) * alignmentCoef)
avg = avg.Normalize().Mul(maxSpeed)
avg = avg.Sub(boid.velocity)
avg = limit(avg, maxForce)
}
return avg
}
func (boid* Boid) cohesion() mgl32.Vec2 {
avg = mgl32.Vec2{0,0}
total = 0
for _, sibling := range *boid.siblings {
if (sibling != *boid) && dist(boid.position, sibling.position) < cohesionPerception {
avg = avg.Add(sibling.position)
total++
}
}
if total > 0 {
avg = avg.Mul(1.0 / float32(total) * cohesionCoef)
avg = avg.Sub(boid.position)
avg = avg.Normalize().Mul(maxSpeed)
avg = avg.Sub(boid.velocity)
avg = limit(avg, maxForce)
}
return avg
}
func (boid* Boid) separation() mgl32.Vec2 {
avg = mgl32.Vec2{0,0}
total = 0
for _, sibling := range *boid.siblings {
d := dist(boid.position, sibling.position)
if (sibling != *boid) && d < divisionPerception {
diff := boid.position.Sub(sibling.position).Mul(1/(d*d))
avg = avg.Add(diff)
total++
}
}
if total > 0 {
avg = avg.Mul(1.0 / float32(total) * divisionCoef)
avg = avg.Normalize().Mul(maxSpeed)
avg = avg.Sub(boid.velocity)
avg = limit(avg, maxForce)
}
return avg
}
func (boid* Boid) Tick() {
boid.acceleration = boid.acceleration.Add(boid.alignment().Mul(1.5))
boid.acceleration = boid.acceleration.Add(boid.cohesion().Mul(1.0))
boid.acceleration = boid.acceleration.Add(boid.separation().Mul(2.0))
boid.position = boid.position.Add(boid.velocity)
boid.velocity = boid.velocity.Add(boid.acceleration.Mul(0.5))
boid.velocity = limit(boid.velocity, maxSpeed)
boid.acceleration = boid.acceleration.Mul(0)
if float64(boid.position.X()) > width {
boid.position[0] = 0
} else if float64(boid.position.X()) < 0 {
boid.position[0] = float32(width)
}
if float64(boid.position.Y()) > height {
boid.position[1] = 0
} else if float64(boid.position.Y()) < 0 {
boid.position[1] = float32(height)
}
}