Files
another-boids-in-rust/src/birdoids/physics.rs
Robert Garrett 69403bc6ca Fix the physics integrator
The velocity component never got updated. Instead, the system was only
calculating the current frame's deltaV. That instantaneous dV and any
velocity that *did* get assigned (bundle insertion, keyboard control)
would be added to the position.

Now forces are integrated into velocity, and velocity is integrated into
position. You know... like a real integration function.
2025-09-03 12:23:53 -05:00

24 lines
725 B
Rust

use bevy::prelude::*;
#[derive(Component, Default, Deref, DerefMut)]
pub struct Velocity(pub Vec3);
#[derive(Component, Default, Deref, DerefMut, PartialEq, Debug)]
pub struct Force(pub Vec3);
pub fn apply_velocity(
mut query: Query<(&mut Transform, &mut Velocity, &mut Force)>,
time: Res<Time>,
) {
for (mut transform, mut velocity, mut force) in &mut query {
// integrate forces into new velocity (assume mass of 1kg)
let delta_v = **force * time.delta_secs();
velocity.0 += delta_v;
force.0 = Vec3::ZERO; // clear force value
// integrate velocity into position
let delta_p = **velocity * time.delta_secs();
transform.translation += delta_p;
}
}