// -*- coding: utf-8 -*- // // Copyright 2021 Michael Büsch // // Licensed under the Apache License version 2.0 // or the MIT license, at your option. // SPDX-License-Identifier: Apache-2.0 OR MIT // use crate::base::PhyType; use crate::object::PhyObject; use crate::vector::Vector; use std::cmp::max; use std::time::{Duration, Instant}; pub const GRAVITY_MERCURY: PhyType = PhyType::new_f64(3.7); pub const GRAVITY_VENUS: PhyType = PhyType::new_f64(8.87); pub const GRAVITY_EARTH: PhyType = PhyType::new_f64(9.80665); pub const GRAVITY_EARTH_MOON: PhyType = PhyType::new_f64(1.625); pub const GRAVITY_MARS: PhyType = PhyType::new_f64(3.72076); pub const GRAVITY_JUPITER: PhyType = PhyType::new_f64(24.79); pub const GRAVITY_SATURN: PhyType = PhyType::new_f64(10.44); pub const GRAVITY_URANUS: PhyType = PhyType::new_f64(8.69); pub const GRAVITY_NEPTUNE: PhyType = PhyType::new_f64(11.15); pub const GRAVITY_PLUTO: PhyType = PhyType::new_f64(0.620); #[derive(Debug)] pub struct WorldContext { now: Instant, period: Duration, gravity: Vector, } impl WorldContext { pub fn now(&self) -> Instant { self.now } pub fn period_sec(&self) -> PhyType { self.period.as_secs_f64().into() } pub fn gravity(&self) -> Vector { self.gravity } } pub struct World { gravity: Vector, now: Instant, } impl World { pub fn new(gravity: Option) -> World { let gravity = match gravity { Some(gravity) => max(gravity, 0.into()), None => GRAVITY_EARTH, }; let gravity = Vector::new(0.into(), 0.into(), -gravity); World { gravity, now: Instant::now(), } } pub fn calc(&mut self, objs: &mut Vec<&mut dyn PhyObject>) { let now = Instant::now(); let period = now - self.now; self.now = now; let context = WorldContext { now, period, gravity: self.gravity, }; for obj in objs { if obj.is_enabled() { obj.calc(&context); } } } } // vim: ts=4 sw=4 expandtab