Fix: Acceleration needs to be reset to 0

The physics integrator is real scuffed. Nothing has mass, and no forces
are applied. Instead, objects affect each other's acceleration. It acts
functionally like a force accumulator where everything has a mass of 1.

Under this model, it must be cleared each frame so that it can be
re-accumulated during the next physics calculation cycle.

This bug was spotted because of anomolous behavior of the velocity
pointer. Nearly stationary boids would have a rather large (especially
after the coefficient reduction earlier) velocity. I noticed this
earlier when building the debug tool and thought it was an error in
the calculations. There might still be bugs, I still need to test, but
that particular one seems to have been a position integrator error.
This commit is contained in:
2024-07-11 16:56:47 -05:00
parent bb33fda018
commit 32991d3e37

View File

@@ -143,11 +143,12 @@ fn turn_if_edge(
}
fn apply_velocity(
mut query: Query<(&mut Transform, &Velocity, &Acceleration)>,
mut query: Query<(&mut Transform, &Velocity, &mut Acceleration)>,
time: Res<Time>
) {
for (mut transform, velocity, acceleration) in &mut query {
for (mut transform, velocity, mut acceleration) in &mut query {
let delta_v = **acceleration * time.delta_seconds();
**acceleration = Vec3::ZERO;
let delta_position = (**velocity + delta_v) * time.delta_seconds();
transform.translation += delta_position;
}