Skip to main content

PhysicsWorld

Struct PhysicsWorld 

Source
pub struct PhysicsWorld {
    pub bodies: Vec<Box<dyn Body>>,
    pub gravity: Gravity,
}
Expand description

A struct representing the physical world of the simulation

Fields§

§bodies: Vec<Box<dyn Body>>

List of all bodies in this world

§gravity: Gravity

World’s gravity properties

Implementations§

Source§

impl PhysicsWorld

Source

pub fn new() -> Self

Create a new instance of PhysicsWorld

Examples found in repository?
examples/plot_gravity.rs (line 7)
6fn main() -> Result<(), Box<dyn std::error::Error>> {
7    let mut world = PhysicsWorld::new();
8    let mut body = RigidBody::new();
9    body.set_mass(1.0);
10    body.set_position(nalgebra::Vector2::new(0.0, 100.0));
11    world.add_body(Box::new(body));
12
13    let dt = 1.0 / 60.0_f32;
14    let steps = 600;
15    let mut samples: Vec<(f32, f32)> = Vec::with_capacity(steps + 1);
16
17    let t0 = 0.0_f32;
18    let y0 = world.bodies[0].get_position().y;
19    samples.push((t0, y0));
20
21    for i in 1..=steps {
22        world.step(dt);
23        let t = i as f32 * dt;
24        let y = world.bodies[0].get_position().y;
25        println!("step: {}, y: {}", i, y);
26        samples.push((t, y));
27    }
28
29    let out_path = "free_fall.png";
30    let root = BitMapBackend::new(out_path, (800, 600)).into_drawing_area();
31    root.fill(&WHITE)?;
32
33    let t_min = samples.first().unwrap().0;
34    let t_max = samples.last().unwrap().0;
35    let y_min = samples.iter().map(|s| s.1).fold(f32::INFINITY, f32::min);
36    let y_max = samples.iter().map(|s| s.1).fold(f32::NEG_INFINITY, f32::max);
37
38    let mut chart = ChartBuilder::on(&root)
39        .caption("Free Fall", ("sans-serif", 30).into_font())
40        .margin(10)
41        .x_label_area_size(40)
42        .y_label_area_size(60)
43        .build_cartesian_2d(t_min..t_max, (y_min - 1.0)..(y_max + 1.0))?;
44
45    chart.configure_mesh().x_desc("time (s)").y_desc("y (m)").draw()?;
46
47    chart
48        .draw_series(LineSeries::new(
49            samples.into_iter(),
50            &BLUE,
51        ))?
52        .label("y(t)")
53        .legend(|(x, y)| PathElement::new(vec![(x, y), (x + 20, y)], BLUE));
54
55    chart.configure_series_labels().background_style(WHITE.mix(0.8)).border_style(BLACK).draw()?;
56
57    root.present()?;
58    println!("Wrote {}", out_path);
59
60    Ok(())
61}
Source

pub fn add_body(&mut self, body: Box<dyn Body>)

Add a new body to the world

§Arguments
  • body: type that implements trait Body
Examples found in repository?
examples/plot_gravity.rs (line 11)
6fn main() -> Result<(), Box<dyn std::error::Error>> {
7    let mut world = PhysicsWorld::new();
8    let mut body = RigidBody::new();
9    body.set_mass(1.0);
10    body.set_position(nalgebra::Vector2::new(0.0, 100.0));
11    world.add_body(Box::new(body));
12
13    let dt = 1.0 / 60.0_f32;
14    let steps = 600;
15    let mut samples: Vec<(f32, f32)> = Vec::with_capacity(steps + 1);
16
17    let t0 = 0.0_f32;
18    let y0 = world.bodies[0].get_position().y;
19    samples.push((t0, y0));
20
21    for i in 1..=steps {
22        world.step(dt);
23        let t = i as f32 * dt;
24        let y = world.bodies[0].get_position().y;
25        println!("step: {}, y: {}", i, y);
26        samples.push((t, y));
27    }
28
29    let out_path = "free_fall.png";
30    let root = BitMapBackend::new(out_path, (800, 600)).into_drawing_area();
31    root.fill(&WHITE)?;
32
33    let t_min = samples.first().unwrap().0;
34    let t_max = samples.last().unwrap().0;
35    let y_min = samples.iter().map(|s| s.1).fold(f32::INFINITY, f32::min);
36    let y_max = samples.iter().map(|s| s.1).fold(f32::NEG_INFINITY, f32::max);
37
38    let mut chart = ChartBuilder::on(&root)
39        .caption("Free Fall", ("sans-serif", 30).into_font())
40        .margin(10)
41        .x_label_area_size(40)
42        .y_label_area_size(60)
43        .build_cartesian_2d(t_min..t_max, (y_min - 1.0)..(y_max + 1.0))?;
44
45    chart.configure_mesh().x_desc("time (s)").y_desc("y (m)").draw()?;
46
47    chart
48        .draw_series(LineSeries::new(
49            samples.into_iter(),
50            &BLUE,
51        ))?
52        .label("y(t)")
53        .legend(|(x, y)| PathElement::new(vec![(x, y), (x + 20, y)], BLUE));
54
55    chart.configure_series_labels().background_style(WHITE.mix(0.8)).border_style(BLACK).draw()?;
56
57    root.present()?;
58    println!("Wrote {}", out_path);
59
60    Ok(())
61}
Source

pub fn step(&mut self, delta_time: f32)

Update this world’s bodies

§Arguments
  • delta_time: the time difference
Examples found in repository?
examples/plot_gravity.rs (line 22)
6fn main() -> Result<(), Box<dyn std::error::Error>> {
7    let mut world = PhysicsWorld::new();
8    let mut body = RigidBody::new();
9    body.set_mass(1.0);
10    body.set_position(nalgebra::Vector2::new(0.0, 100.0));
11    world.add_body(Box::new(body));
12
13    let dt = 1.0 / 60.0_f32;
14    let steps = 600;
15    let mut samples: Vec<(f32, f32)> = Vec::with_capacity(steps + 1);
16
17    let t0 = 0.0_f32;
18    let y0 = world.bodies[0].get_position().y;
19    samples.push((t0, y0));
20
21    for i in 1..=steps {
22        world.step(dt);
23        let t = i as f32 * dt;
24        let y = world.bodies[0].get_position().y;
25        println!("step: {}, y: {}", i, y);
26        samples.push((t, y));
27    }
28
29    let out_path = "free_fall.png";
30    let root = BitMapBackend::new(out_path, (800, 600)).into_drawing_area();
31    root.fill(&WHITE)?;
32
33    let t_min = samples.first().unwrap().0;
34    let t_max = samples.last().unwrap().0;
35    let y_min = samples.iter().map(|s| s.1).fold(f32::INFINITY, f32::min);
36    let y_max = samples.iter().map(|s| s.1).fold(f32::NEG_INFINITY, f32::max);
37
38    let mut chart = ChartBuilder::on(&root)
39        .caption("Free Fall", ("sans-serif", 30).into_font())
40        .margin(10)
41        .x_label_area_size(40)
42        .y_label_area_size(60)
43        .build_cartesian_2d(t_min..t_max, (y_min - 1.0)..(y_max + 1.0))?;
44
45    chart.configure_mesh().x_desc("time (s)").y_desc("y (m)").draw()?;
46
47    chart
48        .draw_series(LineSeries::new(
49            samples.into_iter(),
50            &BLUE,
51        ))?
52        .label("y(t)")
53        .legend(|(x, y)| PathElement::new(vec![(x, y), (x + 20, y)], BLUE));
54
55    chart.configure_series_labels().background_style(WHITE.mix(0.8)).border_style(BLACK).draw()?;
56
57    root.present()?;
58    println!("Wrote {}", out_path);
59
60    Ok(())
61}
Source

pub fn handle_collisions(&mut self)

Handle collisions in this world

Trait Implementations§

Source§

impl Debug for PhysicsWorld

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for PhysicsWorld

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.