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: GravityWorld’s gravity properties
Implementations§
Source§impl PhysicsWorld
impl PhysicsWorld
Sourcepub fn new() -> Self
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}Sourcepub fn add_body(&mut self, body: Box<dyn Body>)
pub fn add_body(&mut self, body: Box<dyn 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}Sourcepub fn step(&mut self, delta_time: f32)
pub fn step(&mut self, delta_time: f32)
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}Sourcepub fn handle_collisions(&mut self)
pub fn handle_collisions(&mut self)
Handle collisions in this world
Trait Implementations§
Source§impl Debug for PhysicsWorld
impl Debug for PhysicsWorld
Auto Trait Implementations§
impl !RefUnwindSafe for PhysicsWorld
impl !Send for PhysicsWorld
impl !Sync for PhysicsWorld
impl !UnwindSafe for PhysicsWorld
impl Freeze for PhysicsWorld
impl Unpin for PhysicsWorld
impl UnsafeUnpin for PhysicsWorld
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
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
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
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.