Skip to main content

World

Struct World 

Source
pub struct World<const N: usize> {
    pub bodies: [Option<Body>; N],
    pub settings: WorldSettings,
}
Expand description

Monde physique gérant l’intégration et la simulation globale.

Stocke un ensemble de corps dans un tableau statique de taille N, évitant toute allocation sur le tas (heap).

Fields§

§bodies: [Option<Body>; N]

Emplacements réservés aux corps physiques du monde.

§settings: WorldSettings

Configuration globale du monde physique.

Implementations§

Source§

impl<const N: usize> World<N>

Source

pub fn new(settings: WorldSettings) -> Self

Crée un nouveau monde physique avec les paramètres spécifiés.

Examples found in repository?
examples/simulation.rs (line 39)
27fn main() {
28    println!("=== Test de simulation avec drew-physics ===");
29
30    // Configuration des paramètres du monde (écran 240x320)
31    let settings = WorldSettings {
32        gravity: Vec2::new(0.0, 9.81),
33        viscosity: 0.05,
34        bounds_min: Vec2::ZERO,
35        bounds_max: Vec2::new(240.0, 320.0),
36    };
37
38    // Instanciation du monde physique sans allocation dynamique (capacité fixe de 4 corps)
39    let mut world = World::<4>::new(settings);
40
41    // Initialisation d'une bille dynamique avec vitesse initiale
42    let mut bille = Body::new(Vec2::new(120.0, 10.0), 1.0, 8.0);
43    bille.velocity = Vec2::new(40.0, 0.0);
44    let _ = world.add_body(bille);
45
46    // Intervalle de temps par frame (soit ~60 images par seconde)
47    let dt = 0.016;
48
49    // Boucle de simulation sur 60 pas de temps
50    for step in 0..60 {
51        world.step(dt);
52
53        if let Some(body) = world.bodies[0] {
54            let x_px = body.position.x as i32;
55            let y_px = body.position.y as i32;
56
57            // Affichage toutes les 10 frames pour suivre l'évolution
58            if step % 10 == 0 {
59                println!(
60                    "Frame {:02} | Pos: ({:3}, {:3}) | Vel: ({:6.2}, {:6.2})",
61                    step, x_px, y_px, body.velocity.x, body.velocity.y
62                );
63            }
64        }
65    }
66}
Source

pub fn bodies_mut(&mut self) -> &mut [Option<Body>; N]

Renvoie un itérateur mutable sur la liste des corps du monde.

Source

pub fn add_body(&mut self, body: Body) -> Option<usize>

Ajoute un corps dans le premier emplacement disponible du monde.

Renvoie Some(index) en cas de succès, ou None si la capacité maximale N est atteinte.

Examples found in repository?
examples/simulation.rs (line 44)
27fn main() {
28    println!("=== Test de simulation avec drew-physics ===");
29
30    // Configuration des paramètres du monde (écran 240x320)
31    let settings = WorldSettings {
32        gravity: Vec2::new(0.0, 9.81),
33        viscosity: 0.05,
34        bounds_min: Vec2::ZERO,
35        bounds_max: Vec2::new(240.0, 320.0),
36    };
37
38    // Instanciation du monde physique sans allocation dynamique (capacité fixe de 4 corps)
39    let mut world = World::<4>::new(settings);
40
41    // Initialisation d'une bille dynamique avec vitesse initiale
42    let mut bille = Body::new(Vec2::new(120.0, 10.0), 1.0, 8.0);
43    bille.velocity = Vec2::new(40.0, 0.0);
44    let _ = world.add_body(bille);
45
46    // Intervalle de temps par frame (soit ~60 images par seconde)
47    let dt = 0.016;
48
49    // Boucle de simulation sur 60 pas de temps
50    for step in 0..60 {
51        world.step(dt);
52
53        if let Some(body) = world.bodies[0] {
54            let x_px = body.position.x as i32;
55            let y_px = body.position.y as i32;
56
57            // Affichage toutes les 10 frames pour suivre l'évolution
58            if step % 10 == 0 {
59                println!(
60                    "Frame {:02} | Pos: ({:3}, {:3}) | Vel: ({:6.2}, {:6.2})",
61                    step, x_px, y_px, body.velocity.x, body.velocity.y
62                );
63            }
64        }
65    }
66}
Source

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

Fait avancer la simulation d’un intervalle de temps dt (en secondes).

Applique la gravité, calcule l’intégration semi-implicite d’Euler, applique la viscosité du milieu et résout les collisions avec les limites du monde.

Examples found in repository?
examples/simulation.rs (line 51)
27fn main() {
28    println!("=== Test de simulation avec drew-physics ===");
29
30    // Configuration des paramètres du monde (écran 240x320)
31    let settings = WorldSettings {
32        gravity: Vec2::new(0.0, 9.81),
33        viscosity: 0.05,
34        bounds_min: Vec2::ZERO,
35        bounds_max: Vec2::new(240.0, 320.0),
36    };
37
38    // Instanciation du monde physique sans allocation dynamique (capacité fixe de 4 corps)
39    let mut world = World::<4>::new(settings);
40
41    // Initialisation d'une bille dynamique avec vitesse initiale
42    let mut bille = Body::new(Vec2::new(120.0, 10.0), 1.0, 8.0);
43    bille.velocity = Vec2::new(40.0, 0.0);
44    let _ = world.add_body(bille);
45
46    // Intervalle de temps par frame (soit ~60 images par seconde)
47    let dt = 0.016;
48
49    // Boucle de simulation sur 60 pas de temps
50    for step in 0..60 {
51        world.step(dt);
52
53        if let Some(body) = world.bodies[0] {
54            let x_px = body.position.x as i32;
55            let y_px = body.position.y as i32;
56
57            // Affichage toutes les 10 frames pour suivre l'évolution
58            if step % 10 == 0 {
59                println!(
60                    "Frame {:02} | Pos: ({:3}, {:3}) | Vel: ({:6.2}, {:6.2})",
61                    step, x_px, y_px, body.velocity.x, body.velocity.y
62                );
63            }
64        }
65    }
66}

Auto Trait Implementations§

§

impl<const N: usize> Freeze for World<N>
where [Option<Body>; N]: Freeze,

§

impl<const N: usize> RefUnwindSafe for World<N>

§

impl<const N: usize> Send for World<N>
where [Option<Body>; N]: Send,

§

impl<const N: usize> Sync for World<N>
where [Option<Body>; N]: Sync,

§

impl<const N: usize> Unpin for World<N>
where [Option<Body>; N]: Unpin,

§

impl<const N: usize> UnsafeUnpin for World<N>
where [Option<Body>; N]: UnsafeUnpin,

§

impl<const N: usize> UnwindSafe for World<N>
where [Option<Body>; N]: UnwindSafe,

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.