Added the same dissipation model for the angular speed.
authorFrancois Fleuret <francois@fleuret.org>
Sat, 1 Apr 2017 21:18:04 +0000 (23:18 +0200)
committerFrancois Fleuret <francois@fleuret.org>
Sat, 1 Apr 2017 21:18:04 +0000 (23:18 +0200)
polygon.cc

index 353486e..b899950 100644 (file)
@@ -32,6 +32,7 @@ using namespace std;
 static const scalar_t dl = 20.0;
 static const scalar_t repulsion_constant = 0.2;
 static const scalar_t speed_max = 1e2;
+static const scalar_t angular_speed_max = M_PI / 10;
 
 Polygon::Polygon(scalar_t mass,
                  scalar_t red, scalar_t green, scalar_t blue,
@@ -307,10 +308,19 @@ void Polygon::initialize(int nb_polygons) {
 
 bool Polygon::update(scalar_t dt) {
   scalar_t speed = sqrt(_dcenter_x * _dcenter_x + _dcenter_y * _dcenter_y);
-  scalar_t speed_target = speed_max - exp(-speed / speed_max) * speed_max;
 
-  _dcenter_x = speed_target * _dcenter_x / speed;
-  _dcenter_y = speed_target * _dcenter_y / speed;
+  if(speed > 0) {
+    scalar_t speed_target = speed_max - exp(-speed / speed_max) * speed_max;
+    _dcenter_x = speed_target * _dcenter_x / speed;
+    _dcenter_y = speed_target * _dcenter_y / speed;
+  }
+
+  scalar_t angular_speed = abs(_dtheta);
+
+  if(angular_speed > 0) {
+    scalar_t angular_speed_target = angular_speed_max - exp(-angular_speed / angular_speed_max) * angular_speed_max;
+    _dtheta = angular_speed_target * _dtheta / angular_speed;
+  }
 
   if(!_nailed) {
     _center_x += _dcenter_x * dt;