-
-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
133 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use mun_runtime::{invoke_fn, RetryResultExt, RuntimeBuilder, StructRef}; | ||
use std::{env, time}; | ||
|
||
extern "C" fn log_f32(value: f32) { | ||
println!("{}", value); | ||
} | ||
|
||
// How to run? | ||
// 1. On the CLI, navigate to the `crates/mun_runtime/examples` directory. | ||
// 2. Run the compiler daemon from the CLI: `/path/to/mun build resources/buoyancy.mun --watch` | ||
// 3. Run the application from the CLI: cargo run --example buoyancy -- buoyancy.munlib | ||
fn main() { | ||
let lib_dir = env::args().nth(1).expect("Expected path to a Mun library."); | ||
|
||
let runtime = RuntimeBuilder::new(lib_dir) | ||
.insert_fn("log_f32", log_f32 as extern "C" fn(f32)) | ||
.spawn() | ||
.expect("Failed to spawn Runtime"); | ||
|
||
let ctx: StructRef = invoke_fn!(runtime, "new_sim").wait(); | ||
|
||
let mut previous = time::Instant::now(); | ||
const FRAME_TIME: time::Duration = time::Duration::from_millis(40); | ||
loop { | ||
let now = time::Instant::now(); | ||
let elapsed = now.duration_since(previous); | ||
|
||
let elapsed_secs = if elapsed < FRAME_TIME { | ||
std::thread::sleep(FRAME_TIME - elapsed); | ||
FRAME_TIME.as_secs_f32() | ||
} else { | ||
elapsed.as_secs_f32() | ||
}; | ||
|
||
let _: () = invoke_fn!(runtime, "sim_update", ctx.clone(), elapsed_secs).wait(); | ||
previous = now; | ||
|
||
runtime.borrow_mut().update(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
extern fn log_f32(value: f32); | ||
|
||
struct SimContext { | ||
sphere: Sphere, | ||
water: Water, | ||
gravity: f32, | ||
} | ||
|
||
struct Sphere { | ||
radius: f32, | ||
mass: f32, // density: f32, | ||
height: f32, | ||
velocity: f32, | ||
} | ||
|
||
struct Water { | ||
density: f32, | ||
} | ||
|
||
pub fn new_sim() -> SimContext { | ||
SimContext { | ||
sphere: new_sphere(), | ||
water: new_water(), | ||
gravity: 9.81, | ||
} | ||
} | ||
|
||
fn new_sphere() -> Sphere { | ||
let radius = 1.0; | ||
let density = 250.0; | ||
|
||
let volume = calc_sphere_volume(radius); | ||
let mass = density * volume; | ||
|
||
Sphere { | ||
radius, | ||
mass, | ||
height: 1.0, | ||
velocity: 0.0, | ||
} | ||
} | ||
|
||
fn new_water() -> Water { | ||
Water { | ||
density: 1000.0, | ||
} | ||
} | ||
|
||
fn calc_submerged_ratio(s: Sphere) -> f32 { | ||
let bottom = s.height - s.radius; | ||
if bottom >= 0.0 { | ||
0.0 | ||
} else if bottom <= -1.0 { | ||
1.0 | ||
} else { | ||
-bottom | ||
} | ||
} | ||
|
||
fn calc_sphere_volume(radius: f32) -> f32 { | ||
let pi = 3.1415926535897; | ||
let r = radius; | ||
|
||
3.0/4.0 * pi * r * r * r | ||
} | ||
|
||
fn calc_buoyancy_force(s: Sphere, w: Water, gravity: f32, submerged_ratio: f32) -> f32 { | ||
let volume = calc_sphere_volume(s.radius); | ||
volume * submerged_ratio * w.density * gravity | ||
} | ||
|
||
pub fn sim_update(ctx: SimContext, elapsed_secs: f32) { | ||
let submerged_ratio = calc_submerged_ratio(ctx.sphere); | ||
if submerged_ratio > 0.0 { | ||
let buoyancy_force = calc_buoyancy_force( | ||
ctx.sphere, | ||
ctx.water, | ||
ctx.gravity, | ||
submerged_ratio | ||
); | ||
let buoyancy_acc = buoyancy_force / ctx.sphere.mass; | ||
ctx.sphere.velocity += buoyancy_acc * elapsed_secs; | ||
} | ||
|
||
ctx.sphere.velocity -= ctx.gravity * elapsed_secs; | ||
|
||
ctx.sphere.height += ctx.sphere.velocity * elapsed_secs; | ||
|
||
log_f32(ctx.sphere.height); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters