Skip to main content

Body

Struct Body 

Source
pub struct Body {
    pub position: Vec2,
    pub velocity: Vec2,
    pub acceleration: Vec2,
    pub inv_mass: f32,
    pub restitution: f32,
    pub radius: f32,
}
Expand description

Description d’un corps physique particulaire dans la simulation.

Fields§

§position: Vec2

Position actuelle du corps dans l’espace 2D.

§velocity: Vec2

Vecteur vitesse linéaire.

§acceleration: Vec2

Accélération accumulée pour le pas de temps courant.

§inv_mass: f32

Inverse de la masse (1 / m). Une valeur de 0.0 indique une masse infinie (corps statique/immobile).

§restitution: f32

Coefficient de restitution / rebond (compris entre 0.0 pour un choc inélastique et 1.0 pour un choc parfait).

§radius: f32

Rayon du corps (utilisé pour la détection des collisions avec les limites du monde).

Implementations§

Source§

impl Body

Source

pub fn new(position: Vec2, mass: f32, radius: f32) -> Self

Crée un nouveau corps physique à la position donnée.

Si mass <= 0.0, l’inverse de la masse (inv_mass) sera fixé à 0.0, rendant le corps statique (insensible à la gravité et aux forces).

Examples found in repository?
examples/simulation.rs (line 42)
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 is_static(&self) -> bool

Indique si le corps est statique (masse infinie / inv_mass == 0.0).

Source

pub fn apply_force(&mut self, force: Vec2)

Applique une force extérieure au corps.

Ne produit aucun effet si le corps est statique (inv_mass == 0.0).

Trait Implementations§

Source§

impl Clone for Body

Source§

fn clone(&self) -> Body

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Body

Source§

impl Debug for Body

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Body

§

impl RefUnwindSafe for Body

§

impl Send for Body

§

impl Sync for Body

§

impl Unpin for Body

§

impl UnsafeUnpin for Body

§

impl UnwindSafe for Body

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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.