robot.cpp

00001 
00002 #include "robot.h"
00003 #include <math.h>
00004 #include <QPen>
00005 #include "playgroundscene.h"
00006 
00007 robot::robot(QGraphicsItem *parent, QGraphicsScene *scene) : QGraphicsItem(parent)
00008 {
00009         actPhi = M_PI/2;
00010         
00011         this->scene = scene;
00012 
00013         this->robotBody = new QGraphicsRectItem(QRectF(OFFSET_X,OFFSET_Y,ROBOT_SIZE_X,ROBOT_SIZE_Y),parent,scene);
00014         this->robotBody->setPen(QPen(QBrush(Qt::black),1,Qt::SolidLine,Qt::FlatCap,Qt::BevelJoin));
00015         this->robotBody->setBrush(QBrush(Qt::black));
00016         this->robotBody->setParentItem(this);
00017         this->robotBody->setZValue(10);         
00018                         ;
00019         this->sensShort[0] = new QGraphicsLineItem(OFFSET_X+2*2,OFFSET_Y,OFFSET_X+2*2,OFFSET_Y-30*2,parent,scene);
00020         
00021         this->sensShort[1] = new QGraphicsLineItem(OFFSET_X+9.5*2,OFFSET_Y,OFFSET_X+9.5*2,OFFSET_Y-30*2,parent,scene);
00022         
00023         this->sensShort[2] = new QGraphicsLineItem(OFFSET_X+17*2,OFFSET_Y,OFFSET_X+17*2,OFFSET_Y-30*2,parent,scene);
00024         
00025         this->sensShort[3] = new QGraphicsLineItem(OFFSET_X+28*2,OFFSET_Y,OFFSET_X+28*2,OFFSET_Y-30*2,parent,scene);
00026         
00027         this->sensShort[4] = new QGraphicsLineItem(OFFSET_X+25*2,OFFSET_Y+ROBOT_SIZE_Y,OFFSET_X+25*2,OFFSET_Y+ROBOT_SIZE_Y+30*2,parent,scene);
00028         
00029         this->sensShort[5] = new QGraphicsLineItem(OFFSET_X+5*2,OFFSET_Y+ROBOT_SIZE_Y,OFFSET_X+5*2,OFFSET_Y+ROBOT_SIZE_Y+30*2,parent,scene);
00030         
00031         for(int i=0;i<6;i++) {
00032                 this->sensShort[i]->setParentItem(this);
00033                 this->sensShort[i]->setPen(QPen(QBrush(Qt::red),2,Qt::DashLine,Qt::FlatCap,Qt::BevelJoin));
00034         }       
00035 
00036         this->sensLong[0] = new QGraphicsLineItem(OFFSET_X+5*2,OFFSET_Y,OFFSET_X+5*2,OFFSET_Y-80*2,parent,scene);
00037 
00038         this->sensLong[1] = new QGraphicsLineItem(OFFSET_X+15*2,OFFSET_Y,OFFSET_X+15*2,OFFSET_Y-80*2,parent,scene);
00039 
00040         this->sensLong[2] = new QGraphicsLineItem(OFFSET_X+25*2,OFFSET_Y,OFFSET_X+25*2,OFFSET_Y-80*2,parent,scene);
00041         
00042         this->setZValue(10);
00043 
00044 //      for(int i=0; i<3; i++) {
00045 //              beacons[i] = new QGraphicsLineItem(parent, scene);
00046 // //           beacon1->setLine(QLineF(OFFSET_X+ROBOT_SIZE_X/2,OFFSET_Y+ROBOT_SIZE_Y/2,BEACON1_X,BEACON1_Y));
00047 //              beacons[i]->setPen(QPen(QBrush(Qt::cyan),2,Qt::SolidLine,Qt::SquareCap,Qt::BevelJoin));
00048 //              beacons[i]->setZValue(20);
00049 //      }
00050 //      beacon2 = new QGraphicsLineItem(parent, scene);
00051 //      beacon2->setLine(QLineF(OFFSET_X+ROBOT_SIZE_X/2,OFFSET_Y+ROBOT_SIZE_Y/2,BEACON2_X,BEACON2_Y));
00052 //      beacon2->setPen(QPen(QBrush(Qt::cyan),2,Qt::SolidLine,Qt::SquareCap,Qt::BevelJoin));
00053 //      beacon2->setZValue(20);
00054 
00055 //      beacon3 = new QGraphicsLineItem(parent, scene);
00056 //      beacon3->setLine(QLineF(OFFSET_X+ROBOT_SIZE_X/2,OFFSET_Y+ROBOT_SIZE_Y/2,BEACON3_X,BEACON3_Y));
00057 //      beacon3->setPen(QPen(QBrush(Qt::cyan),2,Qt::SolidLine,Qt::SquareCap,Qt::BevelJoin));
00058 //      beacon3->setZValue(20);
00059         
00060         this->pCenter = new QPoint(OFFSET_X+ROBOT_SIZE_X/2, 420 - OFFSET_Y -ROBOT_SIZE_Y);
00061 }
00062 
00063 void robot::initBeacons() {
00064         
00065         setBeaconAngle(0,M_PI/4);
00066         beacons[0]->setLine(this->pos().x()+ROBOT_SIZE_X/2,this->pos().y()+ROBOT_SIZE_Y/2,BEACON1_X,BEACON1_Y);
00067         beacons[1]->setLine(this->pos().x()+ROBOT_SIZE_X/2,this->pos().y()+ROBOT_SIZE_Y/2,BEACON2_X,BEACON2_Y);
00068         beacons[2]->setLine(this->pos().x()+ROBOT_SIZE_X/2,this->pos().y()+ROBOT_SIZE_Y/2,BEACON3_X,BEACON3_Y);
00069 }
00070 
00071 void robot::setBeaconAngle(int idx, double angle) {
00072         double y,x;
00073         double c = 800;
00074         angle+=actPhi;
00075         y = c*(sin(angle));
00076         x = c*(cos(angle));
00077         if(angle>M_PI) y = -y;
00078         if((angle>M_PI/2)&&(angle<3*M_PI/2)) x = -x;
00079         printf("x %d, y %d\n",pCenter->x(),pCenter->y());
00080 //      printf("x %f, y %f, c %f, sin %f, cos %f\n",a,b,c,sin(angle),cos(angle));
00081         beacons[idx]->setLine(0,0,(int)x,(int)y);
00082         beacons[idx]->moveBy(pCenter->x(),420-pCenter->y());
00083 }
00084 
00085 robot::~robot()
00086 {
00087 }
00088 
00089 QRectF robot::boundingRect() const
00090  {
00091      return QRectF(this->pos().x(), this->pos().y(), ROBOT_SIZE_X+80, ROBOT_SIZE_Y+80);
00092  }
00093 
00094 void robot::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
00095  {     
00096         Q_UNUSED(option);
00097         Q_UNUSED(widget);
00098 //      Q_UNUSED(painter);
00099         
00100 //      painter->setPen(QPen(QBrush(Qt::black),1,Qt::SolidLine,Qt::FlatCap,Qt::BevelJoin));
00101 //      painter->setBrush(QBrush(Qt::black));
00102 //      painter->drawRect(QRectF(OFFSET_X,OFFSET_Y,ROBOT_SIZE_X,ROBOT_SIZE_Y));
00103 }
00104 
00105 void robot::rotateRobot ( qreal angle )
00106 {
00107         this->translate(ROBOT_SIZE_X/2,ROBOT_SIZE_Y/2);
00108         this->rotate(angle);
00109         this->translate(-ROBOT_SIZE_X/2,-ROBOT_SIZE_Y/2);
00110 }
00111 
00112 void robot::sensorRange(qreal size, int sens_id)
00113 {       
00114         qreal x1 = this->sensShort[sens_id]->line().x1();
00115         qreal y1 = this->sensShort[sens_id]->line().y1();
00116         qreal x2 = this->sensShort[sens_id]->line().x2();
00117         qreal y2 = this->sensShort[sens_id]->line().y2();
00118         
00119         qreal dx = x1+size*(x2-x1);
00120         qreal dy = y1+size*(y2-y1);     
00121 
00122         this->sensShort[sens_id]->setLine(x1,y1,dx,dy);
00123 }
00124 
00125 void robot::moveRobot(double x, double y, double phi)
00126 {
00127     QPointF pos(x,y);
00128     pos = PlaygroundScene::world2scene(pos);
00129     //TODO use this transpormation function
00130     
00131         this->setPos(x*100*2-(ROBOT_SIZE_X/2),420-ROBOT_SIZE_Y-(y*100*2)+(ROBOT_SIZE_Y/2));
00132 //      this->setPos(x*100*2-(ROBOT_SIZE_X),420-ROBOT_SIZE_Y-(y*100*2)+(ROBOT_SIZE_Y/2));
00133         this->rotateRobot((actPhi-phi)*180/M_PI);
00134         actPhi = phi;
00135 }

Generated on Thu Sep 13 11:28:28 2007 for DCE-Eurobot by  doxygen 1.5.3