-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseparation.go
More file actions
33 lines (25 loc) · 839 Bytes
/
separation.go
File metadata and controls
33 lines (25 loc) · 839 Bytes
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
package main
import (
"math"
"github.com/gravestench/mathlib"
)
type Separation struct{}
var _ Velocity = (*Separation)(nil)
func (_ *Separation) Delta(boid *Boid) *mathlib.Vector2 {
velocity := mathlib.NewVector2(0, 0)
separationRange := global.params.separationRange.value()
centreOfSearchArea := boid.Position().Add(boid.Velocity.Limit(1).Scale(separationRange * .3))
neighbours, neighbourCount := global.index.GetNeighbours(centreOfSearchArea, separationRange, boid.Id)
if neighbourCount == 0 {
return velocity
}
for _, neighbour := range neighbours {
repel := boid.Position().Subtract(neighbour.Position())
repel = repel.Scale(1 / math.Pow(repel.Length(), 1.5))
velocity.Add(repel)
}
return velocity.Scale(global.params.separationWeight.value())
}
func newSeparation() *Separation {
return &Separation{}
}