Initial project setup: Rust/WASM solar system simulator with SvelteKit frontend
- Rust workspace with 4 crates: orbital-mechanics, mass-driver-core, mass-driver-wasm, mass-driver-backend - Keplerian orbital mechanics engine with JPL elements for 14 bodies (Sun, 8 planets, Pluto, Ceres, Europa, Titan, Ganymede) - Kepler equation solver and orbital position computation compiled to WASM - SvelteKit frontend with Tailwind CSS, Canvas2D renderer showing animated solar system - Orbit ellipses, planet dots with labels, Sun glow, grid, scale bar, pan/zoom controls - Time controls (play/pause, 5 speed levels, date picker) driving live simulation - 2D/3D view toggle (3D placeholder for Threlte integration) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
59
crates/mass-driver-wasm/src/api.rs
Normal file
59
crates/mass-driver-wasm/src/api.rs
Normal file
@@ -0,0 +1,59 @@
|
||||
use orbital_mechanics::bodies;
|
||||
use orbital_mechanics::orbits;
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
/// Initialize the WASM module. Called once on page load.
|
||||
#[wasm_bindgen]
|
||||
pub fn init() -> Result<(), JsValue> {
|
||||
web_sys::console::log_1(&"mass-driver WASM initialized".into());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Get the number of celestial bodies in the simulation.
|
||||
#[wasm_bindgen]
|
||||
pub fn get_body_count() -> usize {
|
||||
bodies::all_bodies().len()
|
||||
}
|
||||
|
||||
/// Get positions of all celestial bodies at a given Julian Date.
|
||||
///
|
||||
/// Returns a Float64Array of [x0, y0, z0, x1, y1, z1, ...] in AU.
|
||||
#[wasm_bindgen]
|
||||
pub fn get_body_positions_at_epoch(jd: f64) -> Vec<f64> {
|
||||
let all = bodies::all_bodies();
|
||||
orbits::all_positions_at_epoch(&all, jd)
|
||||
}
|
||||
|
||||
/// Get body names as a JSON array of strings.
|
||||
#[wasm_bindgen]
|
||||
pub fn get_body_names() -> String {
|
||||
let all = bodies::all_bodies();
|
||||
let names: Vec<&str> = all.iter().map(|b| b.name).collect();
|
||||
serde_json::to_string(&names).unwrap()
|
||||
}
|
||||
|
||||
/// Get body colors as a flat array [r0, g0, b0, r1, g1, b1, ...].
|
||||
#[wasm_bindgen]
|
||||
pub fn get_body_colors() -> Vec<u8> {
|
||||
let all = bodies::all_bodies();
|
||||
all.iter().flat_map(|b| b.color.iter().copied()).collect()
|
||||
}
|
||||
|
||||
/// Get body radii in km as a Float64Array.
|
||||
#[wasm_bindgen]
|
||||
pub fn get_body_radii() -> Vec<f64> {
|
||||
let all = bodies::all_bodies();
|
||||
all.iter().map(|b| b.radius_km).collect()
|
||||
}
|
||||
|
||||
/// Get orbit points for a given body, sampled around its full orbit.
|
||||
/// Returns a Float64Array of [x0, y0, z0, x1, y1, z1, ...] in AU.
|
||||
/// `samples` is the number of points around the orbit.
|
||||
#[wasm_bindgen]
|
||||
pub fn get_orbit_points(body_id: usize, jd: f64, samples: usize) -> Vec<f64> {
|
||||
let all = bodies::all_bodies();
|
||||
if body_id >= all.len() || body_id == bodies::id::SUN {
|
||||
return vec![];
|
||||
}
|
||||
orbits::orbit_points(&all, body_id, jd, samples)
|
||||
}
|
||||
Reference in New Issue
Block a user