mpri-graphics-project/Ball.cpp

74 lines
1.7 KiB
C++
Raw Normal View History

2018-02-07 15:08:16 +01:00
#include "Ball.hpp"
2018-02-12 01:11:23 +01:00
#include <iostream>
2018-02-07 15:08:16 +01:00
#include <cmath>
2018-02-12 18:40:05 +01:00
Ball::Ball(const Point& _center, double _min_height, double _v_x,
double _p, double _q) :
2018-02-07 15:18:53 +01:00
Center(_center),
surface(_center, _p, _q),
init_h(_center.z),
min_height(_min_height),
2018-02-12 10:21:49 +01:00
bounce_number(0.0),
2018-02-07 15:18:53 +01:00
crt_time(0),
A(_center.z),
B(0),
U(sqrt(2 * G_CTE * _center.z)),
2018-02-12 10:21:49 +01:00
T(sqrt(2.0 * _center.z / G_CTE)),
2018-02-11 22:42:15 +01:00
v_x(_v_x)
2018-02-07 15:18:53 +01:00
{
}
2018-02-07 15:08:16 +01:00
2018-02-11 22:42:15 +01:00
void Ball::_compute_pos(double dt) {
2018-02-12 12:06:44 +01:00
Center.z = fmax(0.0, A + B * crt_time - (G_CTE / 2) * crt_time * crt_time + min_height);
2018-02-11 22:42:15 +01:00
Center.x += dt * v_x;
2018-02-07 15:08:16 +01:00
surface.update_center_pos(Center);
}
void Ball::_compute_T_n() {
2018-02-12 10:21:49 +01:00
double update = (pow(0.5, bounce_number-1) * sqrt(2 * init_h / G_CTE));
T += update;
2018-02-07 15:08:16 +01:00
}
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() {
2018-02-12 10:21:49 +01:00
U *= 0.5;
2018-02-07 15:08:16 +01:00
}
2018-02-11 22:42:15 +01:00
void Ball::_compute_v_x() {
2018-02-12 10:21:49 +01:00
v_x *= 0.5;
2018-02-11 22:42:15 +01:00
}
2018-02-07 15:08:16 +01:00
void Ball::update_pos(double dt) {
crt_time += dt;
if (crt_time >= T) {
2018-02-12 10:21:49 +01:00
bounce_number += 1;
2018-02-07 15:08:16 +01:00
_compute_U_n();
_compute_A_n();
_compute_B_n();
2018-02-12 10:21:49 +01:00
_compute_T_n();
2018-02-11 22:42:15 +01:00
_compute_v_x();
2018-02-12 10:21:49 +01:00
std::cout << "New bounce :";
std::cout << "U:" << U << ":";
std::cout << "A:" << A << ":";
std::cout << "B:" << B << ":";
std::cout << "T:" << T << "\n";
2018-02-07 15:08:16 +01:00
}
2018-02-11 22:42:15 +01:00
_compute_pos(dt);
2018-02-07 15:08:16 +01:00
}
2018-02-12 01:11:23 +01:00
std::ostream& operator<< (std::ostream &out, Ball const& data) {
Point center = data.getCenter();
2018-02-12 10:21:49 +01:00
out << "t:" << data.access_crt_time() << ":";
out << "T:" << data.accessT() << ":";
2018-02-12 01:11:23 +01:00
out << center.x << ':';
out << center.y << ':';
2018-02-12 10:21:49 +01:00
out << center.z << ':';
2018-02-12 01:11:23 +01:00
return out;
}