MCPcopy Create free account
hub / github.com/DeadlockCode/n-body / new

Method new

src/simulation.rs:29–52  ·  view source on GitHub ↗
(seed: u64)

Source from the content-addressed store, hash-verified

27
28impl Simulation {
29 pub fn new(seed: u64) -> Self {
30 fastrand::seed(seed);
31 let mut bodies = Vec::new();
32
33 let n = 3;
34 for _ in 0..n {
35 bodies.push(rand_body());
36 }
37
38 let vel = bodies.iter().map(|b| b.vel * b.mass).sum::<Vec2>() / n as Real;
39 let pos = bodies.iter().map(|b| b.pos * b.mass).sum::<Vec2>() / n as Real;
40 for b in &mut bodies {
41 b.vel -= vel;
42 b.pos -= pos;
43 }
44 let r = bodies.iter().map(|b| b.pos.mag()).max_by(Real::total_cmp).unwrap();
45 for b in &mut bodies {
46 b.pos /= r;
47 }
48
49 Self {
50 bodies,
51 }
52 }
53
54 pub fn update(&mut self) {
55 for i in 0..self.bodies.len() {

Callers

nothing calls this directly

Calls 1

rand_bodyFunction · 0.85

Tested by

no test coverage detected