mpri-graphics-project/Ball.cpp

63 lines
1.3 KiB
C++

#include "Ball.hpp"
#include <iostream>
#include <cmath>
Ball::Ball(Point& _center, double _v_x, double _p, double _q) :
Center(_center),
surface(_center, _p, _q),
init_h(_center.z),
bounce_number(0),
crt_time(0),
A(_center.z),
B(0),
U(sqrt(2 * G_CTE * _center.z)),
T(0),
v_x(_v_x)
{
}
void Ball::_compute_pos(double dt) {
Center.z = A + B * crt_time - (G_CTE / 2) * crt_time * crt_time;
Center.x += dt * v_x;
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::_compute_v_x() {
v_x *= 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_v_x();
}
_compute_pos(dt);
}
std::ostream& operator<< (std::ostream &out, Ball const& data) {
Point center = data.getCenter();
out << center.x << ':';
out << center.y << ':';
out << center.z << '\n';
return out;
}