From ad61354acd6f4caf051060a50b7403a98fbaac8d Mon Sep 17 00:00:00 2001 From: Joshua Moerman Date: Tue, 7 May 2013 20:54:26 +0200 Subject: [PATCH] Removed recursion, rewriten in loop --- Simulation.h | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/Simulation.h b/Simulation.h index 984dfe5..8f8e696 100644 --- a/Simulation.h +++ b/Simulation.h @@ -94,6 +94,7 @@ struct Simulation { std::vector collisions_in_update; Bounds bounds; + math::Vec2 gravity{0.0f, 50.0f}; float collision_timer{0.0}; int total_collisions{0}; @@ -111,10 +112,13 @@ struct Simulation { // move balls for(auto& b : balls){ - b.speed += dt * math::Vec2{0.0f, 50.0f}; + b.speed += dt * gravity; - float rest_time = collide(b, lines, dt); - b.position += rest_time*b.speed; + auto rest_time = collide(b, lines, dt); + while(rest_time.second >= 0.0f && rest_time.first){ + rest_time = collide(b, lines, rest_time.second, rest_time.first); + } + b.position += rest_time.second*b.speed; } // count collisions per second (per half second) @@ -166,7 +170,7 @@ struct Simulation { } } - float collide(ball_type& b, std::vector const & lines, float dt, line_type const * ignore = nullptr){ + std::pair collide(ball_type& b, std::vector const & lines, float dt, line_type const * ignore = nullptr){ line_type const * closest_line = nullptr; math::Vec2 collision{0.0, 0.0}; float closeness = 100.0f; @@ -186,7 +190,7 @@ struct Simulation { } if(closest_line == nullptr){ - return dt; + return std::make_pair(closest_line, dt); } else { ++total_collisions; ++collisions; @@ -201,7 +205,7 @@ struct Simulation { b.speed = 2.0*b1 + b.speed; } - return collide(b, lines, dt - closeness, closest_line); + return std::make_pair(closest_line, dt - closeness); } } };