From c0847e19554ebd3d931a7bad226ff12cce8b6869 Mon Sep 17 00:00:00 2001 From: Max Cahill <1bardesign@gmail.com> Date: Thu, 20 Jan 2022 17:37:40 +1100 Subject: [PATCH] fixed intersect.bounce_off bouncing if the velocity was already going away from the normal --- intersect.lua | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/intersect.lua b/intersect.lua index f4ddf16..a6f3ca5 100644 --- a/intersect.lua +++ b/intersect.lua @@ -508,11 +508,14 @@ function intersect.bounce_off(velocity, normal, conservation) conservation = conservation or 1 --take a copy, we need it local old_vel = velocity:pooled_copy() - --reject on the normal (keep velocity tangential to the normal) - velocity:vector_rejection_inplace(normal) - --add back the complement of the difference; - --basically "flip" the velocity in line with the normal. - velocity:fused_multiply_add_inplace(old_vel:vector_sub_inplace(velocity), -conservation) + --heading into the normal + if old_vel:dot(normal) < 0 then + --reject on the normal (keep velocity tangential to the normal) + velocity:vector_rejection_inplace(normal) + --add back the complement of the difference; + --basically "flip" the velocity in line with the normal. + velocity:fused_multiply_add_inplace(old_vel:vector_sub_inplace(velocity), -conservation) + end --clean up old_vel:release() return velocity