From 15ee31c30efdb392a246ad33766e92712aeacd86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Oudin?= Date: Tue, 13 Feb 2018 17:05:49 +0100 Subject: [PATCH] Integration of the grounds --- Ball.cpp | 7 ++++--- Ball.hpp | 5 ++++- PerlinNoise.hpp | 1 + main_ball.cpp | 5 ++++- main_bounce.cpp | 5 ++++- spheroid.cpp | 24 ++++-------------------- spheroid.hpp | 6 +++--- 7 files changed, 24 insertions(+), 29 deletions(-) diff --git a/Ball.cpp b/Ball.cpp index 350f990..93bb674 100644 --- a/Ball.cpp +++ b/Ball.cpp @@ -5,10 +5,11 @@ bool bouncing = true; double stop_bouncing; -Ball::Ball(const Point& _center, double _min_height, double _v_x, double _v_z, - double _p, double _q) : +Ball::Ball(const Point& _center, const Ground* _ground, double _min_height, + double _v_x, double _v_z, double _p, double _q) : Center(_center), surface(_center, _min_height, _p, _q), + ground(_ground), radius(_p), init_h(_center.y), min_height(_min_height), @@ -41,7 +42,7 @@ void Ball::_compute_pos(double dt) { Center.x += dt * v_x; Center.z += dt * v_z; surface.update_center_pos(Center); - surface.check_horizontal_plan_collision(0.0); + surface.check_ground_collision(ground); } void Ball::_compute_T_n() { diff --git a/Ball.hpp b/Ball.hpp index fc26bb1..7695011 100644 --- a/Ball.hpp +++ b/Ball.hpp @@ -12,6 +12,8 @@ #pragma once #include #include "spheroid.hpp" +#include "FlatGround.hpp" +#include "PerlinGround.hpp" #define G_CTE 9.81 @@ -19,6 +21,7 @@ class Ball { private: Point Center; Spheroid surface; + const Ground* ground; double radius; double init_h; double min_height; @@ -34,7 +37,7 @@ class Ball { void _compute_U_n(); void _compute_T_n(); public: - Ball(const Point& _center, double _min_height, double _v_x, + Ball(const Point& _center, const Ground* ground, double _min_height, double _v_x, double _v_z, double p, double q); void update_pos(double dt); Point getCenter() const {return Center;} diff --git a/PerlinNoise.hpp b/PerlinNoise.hpp index a03760b..00e613c 100644 --- a/PerlinNoise.hpp +++ b/PerlinNoise.hpp @@ -1,6 +1,7 @@ /** * Perlin Noise implementation for the ground (header file) **/ +#pragma once #include "Implicit.hpp" #include #include diff --git a/main_ball.cpp b/main_ball.cpp index c422083..40d4ea3 100644 --- a/main_ball.cpp +++ b/main_ball.cpp @@ -5,13 +5,16 @@ #include "Ball.hpp" #include #include +#include "FlatGround.hpp" using namespace std; int main() { int i; Point center(0,10,0); - Ball ball(center, 0, 0.25, 0, 1, 1); + FlatGround* flat = new FlatGround(); + + Ball ball(center,flat, 0, 0.25, 0, 1, 1); for(i=0; i< 10000; i++) { ball.update_pos(0.001); cout << ball << "\n"; diff --git a/main_bounce.cpp b/main_bounce.cpp index b7f5cac..9f0aa65 100644 --- a/main_bounce.cpp +++ b/main_bounce.cpp @@ -4,15 +4,18 @@ #include "render/GlutRender.hpp" #include "Ball.hpp" +#include "FlatGround.hpp" #include "MarchingCubes.hpp" #include "periodic_updates.hpp" int main(int argc, char** argv) { + + FlatGround* flat = new FlatGround(); GlutRender& render = GlutRender::get_instance(); render.init(&argc, argv, 640, 480, "Bouncing stuff"); - Ball ball(Point(0, 5, 0), 0.75, -.5, -.7, 1, 1); + Ball ball(Point(0, 5, 0), flat, 0.75, -0.5, -0.7, 1, 1); Cuboid bbox = ball.get_surface()->max_bounding_box(); printf("%.2lf %.2lf %.2lf | %.2lf %.2lf %.2lf\n", diff --git a/spheroid.cpp b/spheroid.cpp index 7d347fb..4fc09b4 100644 --- a/spheroid.cpp +++ b/spheroid.cpp @@ -22,35 +22,19 @@ void Spheroid::update_radius() { q = sqrt((3./4.) * V / PI / p); } -void Spheroid::update_height() { - q = (3./4.) * V / PI / (p*p); -} - void Spheroid::update_center_pos(Point& _center) { center = _center; } -void Spheroid::check_horizontal_plan_collision(double height) { - if (((center.y - p) <= height) || (p < init_p)) { - p = fmin(init_p, center.y-height); +void Spheroid::check_ground_collision(const Ground* ground) { + double height = (*ground)(center.x, center.z); + if (((center.y -p) <= height) || (p < init_p)) { + p = fmin(init_p, center.y - height); update_radius(); } } -void Spheroid::check_vertical_plan_collision(double& abscissa) { - if (center.x <= abscissa) { - if ((center.x + q) >= abscissa) { - q = abscissa - center.x; - update_height(); - } - } else { - if ((center.x - q) <= abscissa) { - q = center.x - abscissa; - update_height(); - } - } -} Cuboid Spheroid::max_bounding_box() const { double max_radius = sqrt((3./4.) * V / PI / min_p); diff --git a/spheroid.hpp b/spheroid.hpp index 52c0499..b9549fe 100644 --- a/spheroid.hpp +++ b/spheroid.hpp @@ -8,6 +8,8 @@ #include "Implicit.hpp" #include "common_structures.hpp" #include "PerlinNoise.hpp" +#include "FlatGround.hpp" +#include "PerlinGround.hpp" const double PI = 3.141592653589793; @@ -16,9 +18,7 @@ class Spheroid : public ImplicitSurface { Spheroid(const Point& _center, double _min_p, double _p, double _q); void update_center_pos(Point& _center); void update_radius(); - void update_height(); - void check_horizontal_plan_collision(double height); - void check_vertical_plan_collision(double& abscissa); + void check_ground_collision(const Ground* ground); Cuboid max_bounding_box() const; void check_perlin_collision(PerlinNoise perlin); Point getNormalVector() const { return normal_vector;}