82 lines
2 KiB
C++
82 lines
2 KiB
C++
#include "Ball.hpp"
|
|
#include <iostream>
|
|
#include <cmath>
|
|
|
|
Ball::Ball(Point& _center, double _min_height, double _v_x, double _v_z, double _p, double _q) :
|
|
Center(_center),
|
|
surface(_center, min_height, _p, _q),
|
|
init_h(_center.y),
|
|
min_height(_min_height),
|
|
bounce_number(0.0),
|
|
crt_time(0),
|
|
A(_center.y),
|
|
B(0),
|
|
U(sqrt(2 * G_CTE * _center.y)),
|
|
T(sqrt(2.0 * _center.y / G_CTE)),
|
|
v_x(_v_x),
|
|
v_z(_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);
|
|
Center.x += dt * v_x;
|
|
Center.z += dt * v_z;
|
|
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(Point normal) {
|
|
double factor = normal.x / (4*(normal.x + normal.z));
|
|
v_x *= (0.5 + factor);
|
|
}
|
|
|
|
void Ball::_compute_v_z(Point normal) {
|
|
double factor = normal.x / (4*(normal.x + normal.z));
|
|
v_z *= (0.5 + factor);
|
|
}
|
|
|
|
void Ball::update_pos(double dt) {
|
|
crt_time += dt;
|
|
if (crt_time >= T) {
|
|
Point normal = surface.getNormalVector();
|
|
bounce_number += 1;
|
|
_compute_U_n();
|
|
_compute_A_n();
|
|
_compute_B_n();
|
|
_compute_T_n();
|
|
_compute_v_x(normal);
|
|
_compute_v_z(normal);
|
|
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.z << ':';
|
|
out << center.y << ':';
|
|
return out;
|
|
}
|