79 lines
1.8 KiB
C++
79 lines
1.8 KiB
C++
#include "Ball.hpp"
|
|
#include <iostream>
|
|
#include <cmath>
|
|
|
|
Ball::Ball(Point& _center, double _min_height, double _v_x, double _v_y, double _p, double _q) :
|
|
Center(_center),
|
|
surface(_center, _p, _q),
|
|
init_h(_center.z),
|
|
min_height(_min_height),
|
|
bounce_number(0.0),
|
|
crt_time(0),
|
|
A(_center.z),
|
|
B(0),
|
|
U(sqrt(2 * G_CTE * _center.z)),
|
|
T(sqrt(2.0 * _center.z / G_CTE)),
|
|
v_x(_v_x),
|
|
v_y(_v_y)
|
|
{
|
|
}
|
|
|
|
void Ball::_compute_pos(double dt) {
|
|
Center.z = fmax(0.0, A + B * crt_time - (G_CTE / 2) * crt_time * crt_time + min_height);
|
|
Center.x += dt * v_x;
|
|
Center.y += dt * v_y;
|
|
surface.update_center_pos(Center);
|
|
}
|
|
|
|
void Ball::_compute_T_n() {
|
|
double update = (pow(0.5, bounce_number-1) * sqrt(2 * init_h / G_CTE));
|
|
T += update;
|
|
}
|
|
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 *= 0.5;
|
|
}
|
|
|
|
void Ball::_compute_v_x() {
|
|
v_x *= 0.5;
|
|
}
|
|
|
|
void Ball::_compute_v_y() {
|
|
v_y *= 0.5;
|
|
}
|
|
|
|
void Ball::update_pos(double dt) {
|
|
crt_time += dt;
|
|
if (crt_time >= T) {
|
|
bounce_number += 1;
|
|
_compute_U_n();
|
|
_compute_A_n();
|
|
_compute_B_n();
|
|
_compute_T_n();
|
|
_compute_v_x();
|
|
_compute_v_y();
|
|
std::cout << "New bounce :";
|
|
std::cout << "U:" << U << ":";
|
|
std::cout << "A:" << A << ":";
|
|
std::cout << "B:" << B << ":";
|
|
std::cout << "T:" << T << "\n";
|
|
}
|
|
_compute_pos(dt);
|
|
}
|
|
|
|
std::ostream& operator<< (std::ostream &out, Ball const& data) {
|
|
Point center = data.getCenter();
|
|
out << "t:" << data.access_crt_time() << ":";
|
|
out << "T:" << data.accessT() << ":";
|
|
out << center.x << ':';
|
|
out << center.y << ':';
|
|
out << center.z << ':';
|
|
return out;
|
|
}
|