Closed CraigBuilds closed 7 months ago
I have tried restoring the energy by calling this every loop:
//rescale the velocities of all bodies to correct the total energy to its expected value
pub fn restore_energy(&mut self, handle: rapier::RigidBodyHandle, expected_energy: f32) {
let body = self.rigid_body_set.get_mut(handle).unwrap();
let current_energy = body.linvel().norm_squared() * body.mass();
let scale = (expected_energy / current_energy).sqrt();
body.set_linvel(body.linvel() * scale, true);
}
And this has worked to some extent, as the total energy now stays the same. However, the y velocity still goes towards zero.
I've solved this myself. I needed to set friction to zero. Closing issue.
I have set up a simulation using Rapier2D within a macroquad environment, where a perfectly elastic ball bounces indefinitely within a box formed by four perfectly elastic walls, in zero-gravity. The ball is given an initial velocity vector of (100, 100), and according to the setup, it should bounce off the walls without losing energy, maintaining its velocity magnitude due to the elasticity settings.
However, I observe that after several bounces, the ball's velocity decreases gradually (particularly the y velocity) until the ball no longer bounces vertically, effectively moving only horizontally between the left and right walls.
Minimal Working Example
Expected Behavior
The ball should bounce indefinitely within the box, with its velocity vector's magnitude remaining constant, reflecting perfectly elastic collisions with the walls.
Observed Behavior
The x and y component of the ball's velocity decreases after each bounce. The y component decreases faster, leading to a situation where the ball eventually only moves horizontally.