-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulation.go
More file actions
34 lines (27 loc) · 924 Bytes
/
simulation.go
File metadata and controls
34 lines (27 loc) · 924 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
34
package main
func updateForcesAndBodies(forces []Vector, bodies []Body, dt float64) {
for i := 0; i < len(bodies); i++ {
for j := i + 1; j < len(bodies); j++ {
// Grav force of bodies[j] on bodies[i]
var f Vector = bodies[i].GravitationalForce(bodies[j])
forces[i] = forces[i].Add(f)
// Force of bodies[i] on bodies[j] is the negation
forces[j] = forces[j].Add(f.Scale(-1))
}
// By the time we reach this point, all forces concerning bodies[i] have been counted for
bodies[i].UpdateBodyKinematics(forces[i], dt)
}
}
func BarnesHutUpdateForcesAndBodies(forces []Vector, bodies []Body, n Node, dt float64) {
for i := 0; i < len(bodies); i++ {
forces[i] = n.BarnesHut(bodies[i])
bodies[i].UpdateBodyKinematics(forces[i], dt)
}
}
func totalMomentum(bodies []Body) Vector {
momentum := Vector{}
for i := range bodies {
momentum = momentum.Add(bodies[i].Momentum())
}
return momentum
}