Skip to content

Commit

Permalink
misc(runtime): add buoyancy example
Browse files Browse the repository at this point in the history
  • Loading branch information
Wodann committed May 13, 2020
1 parent 8075476 commit f9d6b7f
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 3 deletions.
40 changes: 40 additions & 0 deletions crates/mun_runtime/examples/buoyancy.rs
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::env;
// 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/fibonacci.mun --watch`
// 3. Run the application from the CLI: cargo run --example hot_reloading -- fibonacci.dll
// 3. Run the application from the CLI: cargo run --example fibonacci -- fibonacci.munlib
fn main() {
let lib_dir = env::args().nth(1).expect("Expected path to a Mun library.");
println!("lib: {}", lib_dir);
Expand Down
90 changes: 90 additions & 0 deletions crates/mun_runtime/examples/resources/buoyancy.mun
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);
}
4 changes: 2 additions & 2 deletions crates/mun_runtime/examples/resources/fibonacci.mun
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
fn nth() -> int {
pub fn nth() -> i64 {
3
}

fn fibonacci(n:int) -> int {
pub fn fibonacci(n:i64) -> i64 {
if n <= 1 {
n
} else {
Expand Down

0 comments on commit f9d6b7f

Please sign in to comment.