Update.
authorFrancois Fleuret <francois@fleuret.org>
Fri, 9 Sep 2016 12:00:03 +0000 (14:00 +0200)
committerFrancois Fleuret <francois@fleuret.org>
Fri, 9 Sep 2016 12:00:03 +0000 (14:00 +0200)
generate.cc

index 62b61ec..b0dfd87 100644 (file)
@@ -39,40 +39,72 @@ int main(int argc, char **argv) {
   scalar_t world_width = 400;
   scalar_t world_height = 400;
   scalar_t square_size = 100;
+  scalar_t hand_x = world_width * 0.5;
+  scalar_t hand_y = world_height * 0.5;
+
+  Universe *universe;
+  Polygon *grabbed_polygon;
+  int nb_attempts = 0;
+
+  srand48(atoi(argv[1]));
+
+  do {
+    universe = new Universe(10, world_width, world_height);
+
+    // scalar_t object_center_x = world_width * 0.5;
+    // scalar_t object_center_y = world_height * 0.5;
+
+    scalar_t object_center_x = world_width * drand48();
+    scalar_t object_center_y = world_height * drand48();
 
-  Universe *universe = new Universe(10, world_width, world_height);
+    scalar_t x[] = {
+      - square_size * 0.5,
+      + square_size * 0.5,
+      + square_size * 0.5,
+      - square_size * 0.5,
+    };
 
-  scalar_t x[] = {
-    world_width * 0.5 - square_size * 0.5,
-    world_width * 0.5 + square_size * 0.5,
-    world_width * 0.5 + square_size * 0.5,
-    world_width * 0.5 - square_size * 0.5,
-  };
+    scalar_t y[] = {
+      - square_size * 0.5,
+      - square_size * 0.5,
+      + square_size * 0.5,
+      + square_size * 0.5,
+    };
 
-  scalar_t y[] = {
-    world_height * 0.5 - square_size * 0.5,
-    world_height * 0.5 - square_size * 0.5,
-    world_height * 0.5 + square_size * 0.5,
-    world_height * 0.5 + square_size * 0.5,
-  };
+    Polygon *pol = new Polygon(0.5, 1.0, 1.0, 0.0, x, y, 4);
+    pol->set_position(object_center_x, object_center_y, M_PI/3);
+    pol->set_speed(0, 0, 0);
+    universe->initialize_polygon(pol);
+    universe->add_polygon(pol);
 
-  Polygon *pol = new Polygon(0.5, 1.0, 1.0, 0.0, x, y, 4);
-  pol->set_position(world_width * 0.5, world_height * 0.5, M_PI/3);
-  pol->set_speed(0, 0, 0);
-  universe->initialize_polygon(pol);
-  universe->add_polygon(pol);
+    grabbed_polygon = universe->pick_polygon(hand_x, hand_y);
+
+    nb_attempts++;
+    if(nb_attempts > 1000) {
+      cerr << "Could not initialize the universe with a grabbed polygon after 1000 attempts. Aborting().";
+      abort();
+    }
+
+  } while(!grabbed_polygon);
 
   {
     FILE *file = fopen("universe1.png", "w");
     generate_png(universe, 0.25, file);
   }
 
-  scalar_t hand_x = world_width * 0.5;
-  scalar_t hand_y = world_height * 0.5;
-  Polygon *grabbed_polygon = universe->pick_polygon(hand_x, hand_y);
   scalar_t grab_relative_x = grabbed_polygon->relative_x(hand_x, hand_y);
   scalar_t grab_relative_y = grabbed_polygon->relative_y(hand_x, hand_y);
 
+  scalar_t dt = 1.0;
+  for(int i = 0; i < 10; i++) {
+    scalar_t xf = grabbed_polygon->absolute_x(grab_relative_x, grab_relative_y);
+    scalar_t yf = grabbed_polygon->absolute_y(grab_relative_x, grab_relative_y);
+    scalar_t force_x = (hand_x - xf) * 10;
+    scalar_t force_y = (hand_y - yf) * 10;
+    grabbed_polygon->apply_force(dt, xf, yf, 0.0, -1.0);
+    universe->update(dt);
+  }
+
   {
     FILE *file = fopen("universe2.png", "w");
     generate_png(universe, 0.25, file);