Skip to content

Commit

Permalink
Merge pull request #9 from ben9583/0.2.2
Browse files Browse the repository at this point in the history
Release 0.2.2
  • Loading branch information
ben9583 committed Jun 14, 2022
2 parents 4b2a3c8 + 0f13540 commit 4214805
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 17 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "solar-sim"
version = "0.2.1"
version = "0.2.2"
authors = ["Ben Plate <bplate9583@gmail.com>"]
edition = "2018"
description = "Physics simulator written in Rust WASM for use in Solar Sim website"
Expand Down Expand Up @@ -38,4 +38,4 @@ wasm-bindgen-test = "0.3.13"
[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = "s"
lto = true
lto = true
13 changes: 4 additions & 9 deletions rust-src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ pub struct Vector2D {
}

pub struct Body<'a> {
radius: f64,
mass: f64,
position: Vector2D,
velocity: Vector2D,
Expand All @@ -51,10 +50,7 @@ impl Body<'_> {

let angle = dy.atan2(dx);

let distance = (dx.powf(2.0) + dy.powf(2.0)).sqrt();

let acceleration = BIG_G * uni[i].mass / distance.powi(2);
let delta_v = acceleration * TIME_STEP;
let delta_v = BIG_G * uni[i].mass / (dx.powf(2.0) + dy.powf(2.0)) * TIME_STEP; // (G(m1)(m2)/d^2) / m1 * t = G(m2)/d^2 * t = at = delta_v

out.x += delta_v * -angle.cos();
out.y += delta_v * -angle.sin();
Expand All @@ -65,7 +61,7 @@ impl Body<'_> {
}

fn next_position(&self) -> Vector2D {
return Vector2D { x: self.position.x + self.velocity.x * TIME_STEP, y: self.position.y + self.velocity.y * TIME_STEP };
return Vector2D { x: self.position.x + self.velocity.x * TIME_STEP, y: self.position.y + self.velocity.y * TIME_STEP }; // x_new = x_old + vt
}
}

Expand Down Expand Up @@ -101,9 +97,8 @@ pub fn step_time() {
}

#[wasm_bindgen]
pub fn add_body(radius: f64, mass: f64, position_x: f64, position_y: f64, velocity_x: f64, velocity_y: f64) {
pub fn add_body(mass: f64, position_x: f64, position_y: f64, velocity_x: f64, velocity_y: f64) {
let new_body = Body {
radius: radius,
mass: mass,
position: Vector2D {
x: position_x,
Expand Down Expand Up @@ -143,4 +138,4 @@ pub fn get_positions() -> Array {
}

return out;
}
}
6 changes: 3 additions & 3 deletions website-src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ let trails = [
for(let i = 0; i < bodies.length; i++) {
let body = bodies[i];

SolarSim.add_body(body.name, body.mass, body.position[0], body.position[1], body.initialVelocity[0], body.initialVelocity[1]);
SolarSim.add_body(body.mass, body.position[0], body.position[1], body.initialVelocity[0], body.initialVelocity[1]);
}

const canvas = document.getElementById("scene");
Expand Down Expand Up @@ -170,6 +170,6 @@ spawnButton.addEventListener("click", (elem, e) => {
});

trails.push([]);
SolarSim.add_body(radius, mass, positionX, positionY, velocityX, velocityY);
SolarSim.add_body(mass, positionX, positionY, velocityX, velocityY);
step(false);
})
})
4 changes: 2 additions & 2 deletions website-src/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion website-src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Creates the Solar Sim website.",
"main": "index.js",
"scripts": {
"build": "webpack --config webpack.config.js",
"build": "rm -rf dist/*.wasm && webpack --config webpack.config.js",
"start": "webpack-dev-server"
},
"keywords": [
Expand Down

0 comments on commit 4214805

Please sign in to comment.