From: Francois Fleuret Date: Tue, 8 Jan 2019 17:13:30 +0000 (+0100) Subject: Added gravity. X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=commitdiff_plain;h=9284cd8bb4fe83fad551a6b51fc1f8c9c95b6591;p=universe.git Added gravity. --- diff --git a/main.cc b/main.cc index 974cbea..6f423a9 100644 --- a/main.cc +++ b/main.cc @@ -281,6 +281,7 @@ int main(int argc, char **argv) { for(int k = 0; k < nb_it; k++) { manipulator.update(dt, &universe); task->update(dt, &universe, &manipulator); + universe.apply_gravity(dt, 0.0, 2.0); changed |= universe.update(dt); } diff --git a/universe.cc b/universe.cc index 4a2e4f9..8dec7f5 100644 --- a/universe.cc +++ b/universe.cc @@ -163,6 +163,14 @@ void Universe::draw(Canvas *canvas) { } } +void Universe::apply_gravity(scalar_t dt, scalar_t fx, scalar_t fy) { + for(int n = 0; n < _nb_polygons; n++) + if(_polygons[n]) + _polygons[n]->apply_force(dt, + _polygons[n]->_center_x, _polygons[n]->_center_y, + fx, fy); +} + void Universe::apply_collision_forces(scalar_t dt) { const int nb_axis = 2; int nb_collision[_nb_polygons * _nb_polygons]; diff --git a/universe.h b/universe.h index a48a3ba..520a123 100644 --- a/universe.h +++ b/universe.h @@ -39,6 +39,7 @@ public: // Compute collisions between projections of the polygons on a few // axis to speed up the computation void compute_pseudo_collisions(int nb_axis, int *nb_colliding_axis); + void apply_gravity(scalar_t dt, scalar_t fx, scalar_t fy); void apply_collision_forces(scalar_t dt); bool update(scalar_t dt);