2 ////////////////////////////////////////////////////////////////////
5 // Written by Francois Fleuret //
6 // Contact <francois.fleuret@idiap.ch> for comments & bug reports //
9 ////////////////////////////////////////////////////////////////////
24 #include "simple_window.h"
28 #include "manipulator.h"
29 #include "intelligence.h"
30 #include "canvas_cairo.h"
32 void generate_png(Universe *universe, scalar_t scale, FILE *file) {
33 CanvasCairo canvas(scale, universe->width(), universe->height());
34 universe->draw(&canvas);
35 canvas.write_png(file);
38 int main(int argc, char **argv) {
39 scalar_t world_width = 400;
40 scalar_t world_height = 400;
41 scalar_t square_size = 100;
42 scalar_t hand_x = world_width * 0.5;
43 scalar_t hand_y = world_height * 0.5;
46 Polygon *grabbed_polygon;
49 srand48(atoi(argv[1]));
52 universe = new Universe(10, world_width, world_height);
54 // scalar_t object_center_x = world_width * 0.5;
55 // scalar_t object_center_y = world_height * 0.5;
57 scalar_t object_center_x = world_width * drand48();
58 scalar_t object_center_y = world_height * drand48();
74 Polygon *pol = new Polygon(0.5, 1.0, 1.0, 0.0, x, y, 4);
75 pol->set_position(object_center_x, object_center_y, M_PI/3);
76 pol->set_speed(0, 0, 0);
77 universe->initialize_polygon(pol);
78 universe->add_polygon(pol);
80 grabbed_polygon = universe->pick_polygon(hand_x, hand_y);
83 if(nb_attempts > 1000) {
84 cerr << "Could not initialize the universe with a grabbed polygon after 1000 attempts. Aborting().";
88 } while(!grabbed_polygon);
91 FILE *file = fopen("universe1.png", "w");
92 generate_png(universe, 0.25, file);
95 scalar_t grab_relative_x = grabbed_polygon->relative_x(hand_x, hand_y);
96 scalar_t grab_relative_y = grabbed_polygon->relative_y(hand_x, hand_y);
99 for(int i = 0; i < 10; i++) {
100 scalar_t xf = grabbed_polygon->absolute_x(grab_relative_x, grab_relative_y);
101 scalar_t yf = grabbed_polygon->absolute_y(grab_relative_x, grab_relative_y);
102 scalar_t force_x = (hand_x - xf) * 10;
103 scalar_t force_y = (hand_y - yf) * 10;
104 grabbed_polygon->apply_force(dt, xf, yf, 0.0, -1.0);
105 universe->update(dt);
109 FILE *file = fopen("universe2.png", "w");
110 generate_png(universe, 0.25, file);