pub struct Vec2 {
pub x: f32,
pub y: f32,
}Expand description
Vecteur 2D à composantes en virgule flottante (f32).
Fields§
§x: f32Composante sur l’axe horizontal X.
y: f32Composante sur l’axe vertical Y.
Implementations§
Source§impl Vec2
impl Vec2
Sourcepub const fn new(x: f32, y: f32) -> Self
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}Sourcepub fn length_squared(&self) -> f32
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 AddAssign for Vec2
impl AddAssign for Vec2
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
Performs the
+= operation. Read moreimpl Copy for Vec2
impl StructuralPartialEq for Vec2
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> 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