Better animation

Bounces are slower, final position is the ball undeformed.
Also, moves continuously when it keep staying on the ground.
This commit is contained in:
Rémi Oudin 2018-02-13 10:51:07 +01:00
parent 2a8b551355
commit 188d43cb50
2 changed files with 25 additions and 3 deletions

View file

@ -2,10 +2,14 @@
#include <iostream> #include <iostream>
#include <cmath> #include <cmath>
bool bouncing = true;
double stop_bouncing;
Ball::Ball(const Point& _center, double _min_height, double _v_x, double _v_z, Ball::Ball(const Point& _center, double _min_height, double _v_x, double _v_z,
double _p, double _q) : double _p, double _q) :
Center(_center), Center(_center),
surface(_center, _min_height, _p, _q), surface(_center, _min_height, _p, _q),
radius(_p),
init_h(_center.y), init_h(_center.y),
min_height(_min_height), min_height(_min_height),
bounce_number(0.0), 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) { 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.x += dt * v_x;
Center.z += dt * v_z; Center.z += dt * v_z;
surface.update_center_pos(Center); surface.update_center_pos(Center);
@ -28,7 +41,7 @@ void Ball::_compute_pos(double dt) {
} }
void Ball::_compute_T_n() { 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; T += update;
} }
void Ball::_compute_B_n() { void Ball::_compute_B_n() {
@ -40,7 +53,7 @@ void Ball::_compute_A_n() {
} }
void Ball::_compute_U_n() { void Ball::_compute_U_n() {
U *= 0.5; U *= 0.8;
} }
void Ball::_compute_v_x(Point normal) { void Ball::_compute_v_x(Point normal) {
@ -56,6 +69,8 @@ void Ball::_compute_v_z(Point normal) {
void Ball::update_pos(double dt) { void Ball::update_pos(double dt) {
crt_time += dt; crt_time += dt;
if (crt_time >= T) { if (crt_time >= T) {
double old_t = T;
double max_t;
Point normal = surface.getNormalVector(); Point normal = surface.getNormalVector();
bounce_number += 1; bounce_number += 1;
_compute_U_n(); _compute_U_n();
@ -64,6 +79,12 @@ void Ball::update_pos(double dt) {
_compute_T_n(); _compute_T_n();
_compute_v_x(normal); _compute_v_x(normal);
_compute_v_z(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 << "New bounce :";
std::cout << "U:" << U << ":"; std::cout << "U:" << U << ":";
std::cout << "A:" << A << ":"; std::cout << "A:" << A << ":";

View file

@ -19,6 +19,7 @@ class Ball {
private: private:
Point Center; Point Center;
Spheroid surface; Spheroid surface;
double radius;
double init_h; double init_h;
double min_height; double min_height;
size_t bounce_number; size_t bounce_number;