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 {
14 const static int world_width = 500;
15 const static int world_height = 500;
19 virtual const char *name() { return "MOVE_SQUARE"; }
21 virtual int nb_degrees() { return 5; }
23 virtual int width() { return world_width; }
25 virtual int height() { return world_height; }
27 virtual void init(Universe *universe, int degree) {
28 if(degree == 0) _square_size = 200;
29 else if(degree == 1) _square_size = 150;
30 else if(degree == 2) _square_size = 100;
31 else _square_size = 50;
34 -_square_size / 2.0, _square_size / 2.0,
35 _square_size / 2.0, -_square_size / 2.0
39 -_square_size / 2.0, -_square_size / 2.0,
40 _square_size / 2.0, _square_size / 2.0
43 _target = new Polygon(0.5, 1.0, 1.0, 0.0, x, y, 4);
44 _target->set_position(_square_size/2, _square_size/2 + (world_height - _square_size) * drand48(), 0);
45 _target->set_speed(0, 0, 0);
47 universe->initialize_polygon(_target);
48 universe->add_polygon(_target);
52 scalar_t x[] = { ( 9 * world_width)/20, (11 * world_width)/20,
53 (11 * world_width)/20, ( 9 * world_width)/20 };
54 scalar_t y[] = { ( 3 * world_height)/20, ( 3 * world_height)/20,
55 (17 * world_height)/20, (17 * world_height)/20 };
56 obstacle = new Polygon(1.0, 0.0, 1.0, 0.0, x, y, 4);
57 obstacle->set_position(world_width/2, world_height/2, 0);
58 obstacle->set_speed(0, 0, 0);
59 obstacle->_nailed = true;
60 universe->initialize_polygon(obstacle);
61 universe->add_polygon(obstacle);
65 virtual void update(scalar_t dt, Universe *universe, Manipulator *manipulator) { }
67 virtual scalar_t reward(Universe *universe, Manipulator *manipulator) {
68 if(_target->_center_x > world_width - _square_size * 0.75) {
69 _target->set_position(_square_size/2, _square_size/2 + (world_height - _square_size) * drand48(), 0);
70 _target->set_speed(0, 0, 0);
71 manipulator->force_release();
78 virtual void draw(SimpleWindow *window) {
79 window->color(0.75, 0.75, 0.75);
80 int x = int (world_width - _square_size * 0.75);
81 window->draw_line(x, 0, x, world_height);
85 extern "C" Task *new_task() {
86 return new MoveSquare();