From 188d43cb5036c10a6fc16c4798edae02304fef43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Oudin?= Date: Tue, 13 Feb 2018 10:51:07 +0100 Subject: [PATCH] Better animation Bounces are slower, final position is the ball undeformed. Also, moves continuously when it keep staying on the ground. --- Ball.cpp | 27 ++++++++++++++++++++++++--- Ball.hpp | 1 + 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/Ball.cpp b/Ball.cpp index 0593040..9cef020 100644 --- a/Ball.cpp +++ b/Ball.cpp @@ -2,10 +2,14 @@ #include #include +bool bouncing = true; +double stop_bouncing; + Ball::Ball(const Point& _center, double _min_height, double _v_x, double _v_z, double _p, double _q) : Center(_center), surface(_center, _min_height, _p, _q), + radius(_p), init_h(_center.y), min_height(_min_height), bounce_number(0.0), @@ -20,7 +24,16 @@ Ball::Ball(const Point& _center, double _min_height, double _v_x, double _v_z, } void Ball::_compute_pos(double dt) { - Center.y = fmax(0.0, A + B * crt_time - (G_CTE / 2) * crt_time * crt_time + min_height); + if (bouncing) { + Center.y = fmax( + min_height, + A + B * crt_time - (G_CTE / 2) * crt_time * crt_time + min_height + ); + } else { + double n_time = crt_time - stop_bouncing; + Center.y = ((radius * (1.0 - exp(-n_time)) + radius)/2.) + (radius - + ((radius * (1- exp(-n_time)) + radius)/2.)) * sin(5. * n_time); + } Center.x += dt * v_x; Center.z += dt * v_z; surface.update_center_pos(Center); @@ -28,7 +41,7 @@ void Ball::_compute_pos(double dt) { } void Ball::_compute_T_n() { - double update = (pow(0.5, bounce_number-1) * sqrt(2 * init_h / G_CTE)); + double update = (2. * U / G_CTE); T += update; } void Ball::_compute_B_n() { @@ -40,7 +53,7 @@ void Ball::_compute_A_n() { } void Ball::_compute_U_n() { - U *= 0.5; + U *= 0.8; } void Ball::_compute_v_x(Point normal) { @@ -56,6 +69,8 @@ void Ball::_compute_v_z(Point normal) { void Ball::update_pos(double dt) { crt_time += dt; if (crt_time >= T) { + double old_t = T; + double max_t; Point normal = surface.getNormalVector(); bounce_number += 1; _compute_U_n(); @@ -64,6 +79,12 @@ void Ball::update_pos(double dt) { _compute_T_n(); _compute_v_x(normal); _compute_v_z(normal); + max_t = (T - old_t)/2.0 + old_t; + + if ((bouncing) && ((A + B * max_t - (G_CTE / 2) * max_t * max_t + min_height) < radius)) { + stop_bouncing = crt_time; + bouncing = false; + } std::cout << "New bounce :"; std::cout << "U:" << U << ":"; std::cout << "A:" << A << ":"; diff --git a/Ball.hpp b/Ball.hpp index 60ed9bc..fc26bb1 100644 --- a/Ball.hpp +++ b/Ball.hpp @@ -19,6 +19,7 @@ class Ball { private: Point Center; Spheroid surface; + double radius; double init_h; double min_height; size_t bounce_number;