2 // Written and (C) by Francois Fleuret
3 // Contact <francois.fleuret@idiap.ch> for comments & bug reports
7 #include "manipulator.h"
9 class MoveSquare : public Task {
11 scalar_t _square_size;
15 const static int world_width = 500;
16 const static int world_height = 500;
20 virtual const char *name() { return "HIT_SQUARE"; }
22 virtual int nb_degrees() { return 1; }
24 virtual int width() { return world_width; }
26 virtual int height() { return world_height; }
28 void scramble(Universe *universe) {
29 scalar_t margin = sqrt(2)/2 * _square_size;
30 scalar_t x[] = { -_square_size/2, _square_size/2, _square_size/2, -_square_size/2 };
31 scalar_t y[] = { -_square_size/2, -_square_size/2, _square_size/2, _square_size/2 };
35 for(int s = 0; s < _nb_shapes; s++) _shapes[s] = 0;
38 for(int k = 0; k < _nb_shapes * 10 && s < _nb_shapes; k++) {
40 // bool red = drand48() < 0.2;
43 if(red) p = new Polygon(0.5, 1.0, 0.0, 0.0, x, y, 4);
44 else p = new Polygon(0.5, 1.0, 1.0, 0.0, x, y, 4);
46 p->set_position(margin + (world_width - 2 * margin) * drand48(),
47 margin + (world_height - 2 * margin) * drand48(),
48 2 * M_PI * drand48());
49 p->set_speed(0, 0, 0);
50 universe->initialize_polygon(p);
52 if(universe->collide(p)) delete p;
54 universe->add_polygon(p);
62 virtual void init(Universe *universe, int degree) {
65 _shapes = new Polygon *[_nb_shapes];
66 _targets = new bool[_nb_shapes];
70 virtual void update(scalar_t dt, Universe *universe, Manipulator *manipulator) { }
72 virtual scalar_t reward(Universe *universe, Manipulator *manipulator) {
73 if(manipulator->grabbing()) {
74 for(int k = 0; k < _nb_shapes; k++) if(_shapes[k] == manipulator->grabbing()) {
75 bool hit = _targets[k];
76 if(hit && (drand48() < 0.25)) {
77 manipulator->force_release();
81 // manipulator->force_release();
82 // scramble(universe);
83 // if(hit) return 1.0;
90 virtual void draw(SimpleWindow *window) { }
93 extern "C" Task *new_task() {
94 return new MoveSquare();