Skip to content

Commit

Permalink
Merge pull request simbleau#58 from Canleskis/perf-impr
Browse files Browse the repository at this point in the history
Refactor force calculation
  • Loading branch information
simbleau authored Aug 9, 2022
2 parents 7ad9911 + 252226e commit f5feeab
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions src/sim/physics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,14 @@ impl PhysicsContext {
let mut force = Vec2::ZERO;
for other in &self.bodies {
if body != other {
let sqr_dist = (other.position(self) - body.position(self))
.length_squared();
let force_dir = (other.position(self)
- body.position(self))
.normalize();
force += force_dir
let dir = other.position(self) - body.position(self);
let sqr_dist = dir.length_squared();
force += dir
* UNIVERSAL_GRAVITY
* GRAVITY_AMPLIFIER
* body.mass(self)
* other.mass(self)
/ sqr_dist;
/ (sqr_dist * sqr_dist.sqrt());
}
}

Expand Down

0 comments on commit f5feeab

Please sign in to comment.