Fix RigidDynamicBody gaining momentum with bounce

Bounce calculation now uses the previous frame's velocity, so it's
consistent with the actual motion of the bodies involved and not the
yet-to-be-applied forces.

When bounce is 1, using the current velocity was causing the new forces
(including gravity) to be taken into account, which lead to the bounce
velocity to be higher than the falling velocity at the moment of impact,
adding more and more energy over time.
This commit is contained in:
PouleyKetchoupp 2021-11-25 08:26:38 -07:00
parent b3ec5a16ea
commit 7032cf0637
6 changed files with 24 additions and 20 deletions

View file

@ -32,7 +32,6 @@
#include "godot_collision_solver_2d.h"
#include "godot_space_2d.h"
#define POSITION_CORRECTION
#define ACCUMULATE_IMPULSES
void GodotBodyPair2D::_add_contact(const Vector2 &p_point_A, const Vector2 &p_point_B, void *p_self) {
@ -453,9 +452,9 @@ bool GodotBodyPair2D::pre_solve(real_t p_step) {
c.bounce = combine_bounce(A, B);
if (c.bounce) {
Vector2 crA(-A->get_angular_velocity() * c.rA.y, A->get_angular_velocity() * c.rA.x);
Vector2 crB(-B->get_angular_velocity() * c.rB.y, B->get_angular_velocity() * c.rB.x);
Vector2 dv = B->get_linear_velocity() + crB - A->get_linear_velocity() - crA;
Vector2 crA(-A->get_prev_angular_velocity() * c.rA.y, A->get_prev_angular_velocity() * c.rA.x);
Vector2 crB(-B->get_prev_angular_velocity() * c.rB.y, B->get_prev_angular_velocity() * c.rB.x);
Vector2 dv = B->get_prev_linear_velocity() + crB - A->get_prev_linear_velocity() - crA;
c.bounce = c.bounce * dv.dot(c.normal);
}