From 522ab7be397ce5c8dc5373d8257528a9d875bec6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Oudin?= Date: Wed, 7 Feb 2018 15:08:16 +0100 Subject: [PATCH] Raw implementation of a Ball --- Ball.cpp | 38 ++++++++++++++++++++++++++++++++++++++ Ball.hpp | 30 ++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 Ball.cpp create mode 100644 Ball.hpp diff --git a/Ball.cpp b/Ball.cpp new file mode 100644 index 0000000..8e01d60 --- /dev/null +++ b/Ball.cpp @@ -0,0 +1,38 @@ +#include "Ball.hpp" +#include + +Ball::Ball(Point& _center, double _p, double _q) : Center(_center), + init_h(_center.z), bounce_number(0), crt_time(0), T(0), A(_center.z), B(0), + U(sqrt(2 * G_CTE * _center.z)), surface(_center, _p, _q) {} + +void Ball::_compute_pos() { + Center.x = A + B * crt_time - (G_CTE / 2) * crt_time * crt_time; + surface.update_center_pos(Center); +} + +void Ball::_compute_T_n() { + double swap = T; + T = swap + 2/G_CTE * pow(1/2, bounce_number - 2) * sqrt(2 * init_h / G_CTE); +} +void Ball::_compute_B_n() { + B = G_CTE * T + U; +} + +void Ball::_compute_A_n() { + A = - G_CTE * T * T / 2 - (U * T); +} + +void Ball::_compute_U_n() { + U *= 1/2; +} + +void Ball::update_pos(double dt) { + crt_time += dt; + if (crt_time >= T) { + _compute_T_n(); + _compute_U_n(); + _compute_A_n(); + _compute_B_n(); + } + _compute_pos(); +} diff --git a/Ball.hpp b/Ball.hpp new file mode 100644 index 0000000..5980f5d --- /dev/null +++ b/Ball.hpp @@ -0,0 +1,30 @@ +/** + * Header file defining a ball. + * A ball is the kind of real object, with a physical model, and the implicit + * surface. + * The physical model is strongly inspired from + * http://www.physics-online.info/book1/chapt1/sect1a/problem1a-12/Problem1A-12.htm + **/ + +#include +#include "spheroid.hpp" + +#define G_CTE 9.81 + +class Ball { + private: + Point Center; + Spheroid surface; + double init_h; + size_t bounce_number; + double crt_time; + double A, B, U, T; // Coefficients for the physical model. + void _compute_pos(); + void _compute_A_n(); + void _compute_B_n(); + void _compute_U_n(); + void _compute_T_n(); + public: + Ball(Point& _center, double p, double q); + void update_pos(double dt); +};