From b3f0e0da03a3cd4976a5a90d040cbe7fd7472987 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Mon, 16 Dec 2024 18:32:26 +0100 Subject: added dynamic bounce --- src/crepe/system/CollisionSystem.cpp | 64 +++++++++++++++++++++++++++++++++--- 1 file changed, 59 insertions(+), 5 deletions(-) diff --git a/src/crepe/system/CollisionSystem.cpp b/src/crepe/system/CollisionSystem.cpp index 4e84d8b..0cd6f5c 100644 --- a/src/crepe/system/CollisionSystem.cpp +++ b/src/crepe/system/CollisionSystem.cpp @@ -410,15 +410,69 @@ void CollisionSystem::dynamic_collision_handler(CollisionInfo & info){ switch (info.resolution_direction) { case Direction::BOTH: - info.this_rigidbody.data.linear_velocity = {0, 0}; + if (info.this_rigidbody.data.elastisity_coefficient > 0) { + info.this_rigidbody.data.linear_velocity + = -info.this_rigidbody.data.linear_velocity + * info.this_rigidbody.data.elastisity_coefficient; + } + else { + info.this_rigidbody.data.linear_velocity = {0, 0}; + } + + if (info.other_rigidbody.data.elastisity_coefficient > 0) { + info.other_rigidbody.data.linear_velocity + = -info.other_rigidbody.data.linear_velocity + * info.other_rigidbody.data.elastisity_coefficient; + } + else { + info.other_rigidbody.data.linear_velocity = {0, 0}; + } break; case Direction::Y_DIRECTION: - info.this_rigidbody.data.linear_velocity.y = 0; - info.this_transform.position.x -= info.resolution.x; + if (info.this_rigidbody.data.elastisity_coefficient > 0) { + info.this_rigidbody.data.linear_velocity.y + = -info.this_rigidbody.data.linear_velocity.y + * info.this_rigidbody.data.elastisity_coefficient; + } + // Stop movement + else { + info.this_rigidbody.data.linear_velocity.y = 0; + info.this_transform.position.x -= info.resolution.x; + } + + if (info.other_rigidbody.data.elastisity_coefficient > 0) { + info.other_rigidbody.data.linear_velocity.y + = -info.other_rigidbody.data.linear_velocity.y + * info.other_rigidbody.data.elastisity_coefficient; + } + // Stop movement + else { + info.other_rigidbody.data.linear_velocity.y = 0; + info.other_transform.position.x -= info.resolution.x; + } break; case Direction::X_DIRECTION: - info.this_rigidbody.data.linear_velocity.x = 0; - info.this_transform.position.y -= info.resolution.y; + if (info.this_rigidbody.data.elastisity_coefficient > 0) { + info.this_rigidbody.data.linear_velocity.x + = -info.this_rigidbody.data.linear_velocity.x + * info.this_rigidbody.data.elastisity_coefficient; + } + // Stop movement + else { + info.this_rigidbody.data.linear_velocity.x = 0; + info.this_transform.position.y -= info.resolution.y; + } + + if (info.other_rigidbody.data.elastisity_coefficient > 0) { + info.other_rigidbody.data.linear_velocity.x + = -info.other_rigidbody.data.linear_velocity.x + * info.other_rigidbody.data.elastisity_coefficient; + } + // Stop movement + else { + info.other_rigidbody.data.linear_velocity.x = 0; + info.other_transform.position.y -= info.resolution.y; + } break; case Direction::NONE: // Not possible -- cgit v1.2.3