Skip to main content

Vec2

Struct Vec2 

Source
pub struct Vec2 {
    pub x: f32,
    pub y: f32,
}
Expand description

Vecteur 2D à composantes en virgule flottante (f32).

Fields§

§x: f32

Composante sur l’axe horizontal X.

§y: f32

Composante sur l’axe vertical Y.

Implementations§

Source§

impl Vec2

Source

pub const ZERO: Self

Vecteur nul (0.0, 0.0).

Source

pub const fn new(x: f32, y: f32) -> Self

Crée un nouveau vecteur 2D avec les composantes x et y.

Examples found in repository?
examples/simulation.rs (line 32)
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 length_squared(&self) -> f32

Calcule la norme au carré du vecteur (x² + y²).

Utile pour comparer des distances sans coût d’extraction de racine carrée (sqrt).

Trait Implementations§

Source§

impl Add for Vec2

Source§

type Output = Vec2

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self

Performs the + operation. Read more
Source§

impl AddAssign for Vec2

Source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
Source§

impl Clone for Vec2

Source§

fn clone(&self) -> Vec2

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 Vec2

Source§

impl Debug for Vec2

Source§

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

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

impl Default for Vec2

Source§

fn default() -> Vec2

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

impl Mul<f32> for Vec2

Source§

type Output = Vec2

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: f32) -> Self

Performs the * operation. Read more
Source§

impl PartialEq for Vec2

Source§

fn eq(&self, other: &Vec2) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Vec2

Source§

impl Sub for Vec2

Source§

type Output = Vec2

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Self) -> Self

Performs the - operation. Read more

Auto Trait Implementations§

§

impl Freeze for Vec2

§

impl RefUnwindSafe for Vec2

§

impl Send for Vec2

§

impl Sync for Vec2

§

impl Unpin for Vec2

§

impl UnsafeUnpin for Vec2

§

impl UnwindSafe for Vec2

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.