simulation/simulation.rs
1// Copyright (C) 2026 Jorge Andre Castro
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 2 of the License, or
6// (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12
13//! Exemple d'intégration et de simulation en temps réel avec `drew_physics`.
14//!
15//! Ce programme illustre la création d'un monde physique 2D contraint dans un écran
16//! de dimensions 240x320, le lâcher d'une bille avec une vitesse initiale,
17//! et l'affichage console de sa trajectoire sur 60 frames (1 seconde à 60 FPS).
18//!
19//! Exécution :
20//! ```sh
21//! cargo run --example demo
22//! ```
23
24use drew_physics::{Body, Vec2, World, WorldSettings};
25
26/// Point d'entrée principal du programme d'exemple.
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}