Add movement on y-axis
This commit is contained in:
parent
247528fe63
commit
c2cd75a61c
3 changed files with 12 additions and 5 deletions
11
Ball.cpp
11
Ball.cpp
|
@ -2,7 +2,7 @@
|
|||
#include <iostream>
|
||||
#include <cmath>
|
||||
|
||||
Ball::Ball(Point& _center, double _min_height, double _v_x, double _p, double _q) :
|
||||
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),
|
||||
|
@ -13,13 +13,15 @@ Ball::Ball(Point& _center, double _min_height, double _v_x, double _p, double _q
|
|||
B(0),
|
||||
U(sqrt(2 * G_CTE * _center.z)),
|
||||
T(sqrt(2.0 * _center.z / G_CTE)),
|
||||
v_x(_v_x)
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -43,6 +45,10 @@ 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) {
|
||||
|
@ -52,6 +58,7 @@ void Ball::update_pos(double dt) {
|
|||
_compute_B_n();
|
||||
_compute_T_n();
|
||||
_compute_v_x();
|
||||
_compute_v_y();
|
||||
std::cout << "New bounce :";
|
||||
std::cout << "U:" << U << ":";
|
||||
std::cout << "A:" << A << ":";
|
||||
|
|
4
Ball.hpp
4
Ball.hpp
|
@ -24,7 +24,7 @@ class Ball {
|
|||
size_t bounce_number;
|
||||
double crt_time;
|
||||
double A, B, U, T; // Coefficients for the physical model.
|
||||
double v_x;
|
||||
double v_x, v_y;
|
||||
void _compute_pos(double dt);
|
||||
void _compute_v_x();
|
||||
void _compute_A_n();
|
||||
|
@ -32,7 +32,7 @@ class Ball {
|
|||
void _compute_U_n();
|
||||
void _compute_T_n();
|
||||
public:
|
||||
Ball(Point& _center, double _min_height, double _v_x, double p, double q);
|
||||
Ball(Point& _center, double _min_height, double _v_x, double _v_y, double p, double q);
|
||||
void update_pos(double dt);
|
||||
Point getCenter() const {return Center;}
|
||||
double accessT() const { return T;}
|
||||
|
|
|
@ -11,7 +11,7 @@ using namespace std;
|
|||
int main(int argc, char** argv) {
|
||||
int i;
|
||||
Point center(0,0,10);
|
||||
Ball ball(center, 0, 0.25, 1, 1);
|
||||
Ball ball(center, 0, 0.25, 0, 1, 1);
|
||||
for(i=0; i< 10000; i++) {
|
||||
ball.update_pos(0.001);
|
||||
cout << ball << "\n";
|
||||
|
|
Loading…
Reference in a new issue