Skip to main content

fennel_physics/
world.rs

1use crate::body::Body;
2use crate::gravity::Gravity;
3
4/// A struct representing the physical world of the simulation
5#[derive(Debug)]
6pub struct PhysicsWorld {
7    /// List of all bodies in this world
8    pub bodies: Vec<Box<dyn Body>>,
9    /// World's gravity properties
10    pub gravity: Gravity,
11}
12
13impl Default for PhysicsWorld {
14    fn default() -> Self {
15        Self::new()
16    }
17}
18
19impl PhysicsWorld {
20    /// Create a new instance of [`PhysicsWorld`]
21    pub fn new() -> Self {
22        Self {
23            bodies: Vec::new(),
24            gravity: Gravity::new(9.81)
25        }
26    }
27
28    /// Add a new body to the world
29    ///
30    /// # Arguments
31    /// * `body`: type that implements trait [`Body`]
32    pub fn add_body(&mut self, body: Box<dyn Body>) {
33        self.bodies.push(body);
34    }
35
36    /// Update this world's bodies
37    ///
38    /// # Arguments
39    /// * `delta_time`: the time difference
40    pub fn step(&mut self, delta_time: f32) {
41        for body in self.bodies.iter_mut() {
42            body.update(delta_time, self.gravity.acceleration);
43        }
44
45        self.handle_collisions();
46    }
47
48    /// Handle collisions in this world
49    // TODO: handle collisions
50    pub fn handle_collisions(&mut self) {
51
52    }
53}