Newer
Older
Grégoire Payen de La Garanderie
committed
#include "simul/robot.h"
#include "simul/element.h"
Grégoire Payen de La Garanderie
committed
#include <cmath>
#include "odometrie.h"
#include "asservissement.h"
#include "strategie.h"
#include <iostream>
Grégoire Payen de La Garanderie
committed
//Odometrie class implementation for the simulation
//Yes, it's ugly ! it should not be in this file.
//But in a separate file
Odometrie::Odometrie(Robot* robot)
Grégoire Payen de La Garanderie
committed
{
Grégoire Payen de La Garanderie
committed
this->robot = robot;
}
Grégoire Payen de La Garanderie
committed
Grégoire Payen de La Garanderie
committed
PositionPlusAngle Odometrie::getPos()
{
return robot->pos;
}
Grégoire Payen de La Garanderie
committed
Grégoire Payen de La Garanderie
committed
Distance Odometrie::getVitesseLineaire()
{
return robot->deriv.position.getNorme();
}
Grégoire Payen de La Garanderie
committed
Grégoire Payen de La Garanderie
committed
Angle Odometrie::getVitesseAngulaire()
{
return robot->deriv.angle;
}
Grégoire Payen de La Garanderie
committed
Grégoire Payen de La Garanderie
committed
void Odometrie::setPos(PositionPlusAngle p)
{
robot->pos = p;
}
Grégoire Payen de La Garanderie
committed
Robot::Robot(b2World & world) : world(world), olds(10000)
Grégoire Payen de La Garanderie
committed
{
manual = false;
elem = NULL;
joint = NULL;
level = 0;
Grégoire Payen de La Garanderie
committed
odometrie = new Odometrie(this);
Grégoire Payen de La Garanderie
committed
asservissement = new Asservissement(odometrie);
strategie = new Strategie(true, odometrie);
Grégoire Payen de La Garanderie
committed
asservissement->strategie = strategie;
Grégoire Payen de La Garanderie
committed
pos = odometrie->getPos();
deriv.position.x = 0;
deriv.position.y = 0;
deriv.angle = 0;
b2BodyDef bodyDef;
Grégoire Payen de La Garanderie
committed
#ifndef BOX2D_2_0_1
bodyDef.type = b2_dynamicBody;
#endif
bodyDef.position.Set(pos.position.x/100., pos.position.y/100.);
Grégoire Payen de La Garanderie
committed
bodyDef.angle = pos.angle;
body = world.CreateBody(&bodyDef);
Grégoire Payen de La Garanderie
committed
#ifdef BOX2D_2_0_1
Grégoire Payen de La Garanderie
committed
b2PolygonDef &fixture = box;
#define CreateFixture CreateShape
#else
b2PolygonShape box;
b2FixtureDef fixture;
fixture.shape = &box;
#endif
fixture.density = 10.0f;
fixture.friction = 1.0f;
Grégoire Payen de La Garanderie
committed
Grégoire Payen de La Garanderie
committed
box.SetAsBox(1.15,1.795, b2Vec2(-1.49+1.04,0),0);
Grégoire Payen de La Garanderie
committed
body->CreateFixture(&fixture);
Grégoire Payen de La Garanderie
committed
Grégoire Payen de La Garanderie
committed
#ifdef BOX2D_2_0_1
b2Vec2* v = box.vertices;
box.vertexCount = 4;
Grégoire Payen de La Garanderie
committed
#else
b2Vec2 v[4];
#endif
Grégoire Payen de La Garanderie
committed
int inc = 0;
Grégoire Payen de La Garanderie
committed
v[inc++].Set(-.97+1.04,1.07);
v[inc++].Set(.60+1.04,1.07);
v[inc++].Set(0+1.04,1.795);
v[inc++].Set(-0.97+1.04,1.795);
#ifndef BOX2D_2_0_1
box.Set(v, 4);
#endif
Grégoire Payen de La Garanderie
committed
body->CreateFixture(&fixture);
inc = 0;
Grégoire Payen de La Garanderie
committed
v[inc++].Set(-0.97+1.04,-1.795);
v[inc++].Set(0+1.04,-1.795);
v[inc++].Set(.60+1.04,-1.07);
v[inc++].Set(-.97+1.04,-1.07);
#ifndef BOX2D_2_0_1
box.Set(v, 4);
#endif
Grégoire Payen de La Garanderie
committed
body->CreateFixture(&fixture);
Grégoire Payen de La Garanderie
committed
#ifdef BOX2D_2_0_1
body->SetMassFromShapes();
#endif
Grégoire Payen de La Garanderie
committed
//Little hack so that linear and angular speed of the object
//are those of the local coord (0,0) of the robot.
//We don't really care of the mass center accuracy.
b2MassData md;
body->GetMassData(&md);
md.center = b2Vec2(0,0);
body->SetMassData(&md);
}
void Robot::updateForces(int dt)
{
if(dt == 0)
return;
Position impulse;
Grégoire Payen de La Garanderie
committed
impulse.x = (deriv.position.x*(float)cos(pos.angle) - deriv.position.y*(float)sin(pos.angle));
impulse.y = (deriv.position.x*(float)sin(pos.angle) + deriv.position.y*(float)cos(pos.angle));
float32 rdt = 1000./(float)dt;
b2Vec2 bvelocity = 0.01*rdt*b2Vec2(impulse.x,impulse.y);
Grégoire Payen de La Garanderie
committed
float bangular = deriv.angle*rdt;
Grégoire Payen de La Garanderie
committed
//body->ApplyForce(10*body->GetMass()*(bimpulse - body->GetLinearVelocity()), body->GetWorldCenter());
//body->ApplyTorque((bangular - body->GetAngularVelocity())*body->GetInertia());
Grégoire Payen de La Garanderie
committed
body->SetLinearVelocity(bvelocity);
Grégoire Payen de La Garanderie
committed
body->SetAngularVelocity(bangular);
Grégoire Payen de La Garanderie
committed
}
Grégoire Payen de La Garanderie
committed
void Robot::paint(QPainter &p, int dt)
{
if(dt)
Grégoire Payen de La Garanderie
committed
pos.position.x = 100*body->GetPosition().x;
pos.position.y = 100*body->GetPosition().y;
pos.angle = body->GetAngle();
Grégoire Payen de La Garanderie
committed
float rdt = (float)dt/1000.;
deriv.angle = body->GetAngularVelocity()*rdt;
float derx = 100*body->GetLinearVelocity().x*rdt;
float dery = 100*body->GetLinearVelocity().y*rdt;
Grégoire Payen de La Garanderie
committed
deriv.position.x = derx*cos(pos.angle) + dery*sin(pos.angle);
Grégoire Payen de La Garanderie
committed
deriv.position.y = 0;
olds.push_back(pos);
if(manual)
{
keyPressEvent(NULL,false);
deriv.position.x = deriv.position.x* 0.97f;
deriv.angle = deriv.angle * 0.9;
}
else
{
Asservissement::asservissement->update();
deriv.position.x = asservissement->getLinearSpeed();
deriv.position.y = 0;
deriv.angle = asservissement->getAngularSpeed();
Grégoire Payen de La Garanderie
committed
Grégoire Payen de La Garanderie
committed
p.setWorldTransform(QTransform().translate(pos.position.x,-pos.position.y).rotateRadians(-pos.angle));
Grégoire Payen de La Garanderie
committed
p.setPen(QColor(Qt::black));
p.setBrush(QBrush(QColor(90,90,90)));
p.setOpacity(.3);
Grégoire Payen de La Garanderie
committed
QPoint po[10];
int inc = 0;
Grégoire Payen de La Garanderie
committed
po[inc++] = QPoint(0+104,-179.5);
po[inc++] = QPoint(-262+104,-179.5);
po[inc++] = QPoint(-262+104,179.5);
po[inc++] = QPoint(0+104,179.5);
po[inc++] = QPoint(60+104,114.5);
po[inc++] = QPoint(60+104,-114.5);
Grégoire Payen de La Garanderie
committed
p.drawConvexPolygon(po, 6);
Grégoire Payen de La Garanderie
committed
p.drawChord(-103/2 + 104, -107, 2*103, 215, 16*90, 16*180);
Grégoire Payen de La Garanderie
committed
//p.drawRect(-268, -179.5, 268, 359);
//drawTriangle(p, 0, 0, 65, 0, 60, 0);
p.setOpacity(1);
Grégoire Payen de La Garanderie
committed
p.setPen(QColor(Qt::red));
p.drawLine(0,0,pos.position.x,0);
Grégoire Payen de La Garanderie
committed
p.drawLine(0,100*pos.angle,0,0);
Grégoire Payen de La Garanderie
committed
p.setWorldTransform(QTransform());
p.setPen(QColor(Qt::green));
for(unsigned int i=0; i+1 < olds.size(); i++)
Grégoire Payen de La Garanderie
committed
p.drawLine(olds[i].position.x, -olds[i].position.y, olds[i+1].position.x, -olds[i+1].position.y);
Grégoire Payen de La Garanderie
committed
}
#define IF_KEYSWITCH(n,a) \
static bool n=false; \
if(evt) n= a ? press : n; if(n)
void Robot::keyPressEvent(QKeyEvent* evt, bool press)
{
if(evt && press && evt->text() == "e" && !evt->isAutoRepeat())
manual = !manual;
if(evt && press && evt->text() == "i" && !evt->isAutoRepeat())
makeJoint();
if(evt && press && evt->text() == "u" && !evt->isAutoRepeat() && elem)
{
level = (level != 100) ? 100 : 0;
#ifdef BOX2D_2_0_1
#define b2Filter b2FilterData
#define GetFixtureList GetShapeList
#endif
b2Filter filter;
if(level == 100)
{
filter.categoryBits = 0x4;
filter.maskBits = 0x3;
}
else
{
filter.maskBits = 0x3;
filter.categoryBits = 0x1;
if(elem->type != Element::Pawn)
{
filter.categoryBits = 0x2;
filter.maskBits |= 0x4;
}
}
elem->body->GetFixtureList()[0].SetFilterData(filter);
}
if(manual)
{
float dinc = .5;
float ainc = 0.005;
IF_KEYSWITCH(avant,evt->key() == Qt::Key_Up)
deriv.position.x += dinc;
IF_KEYSWITCH(arriere,evt->key() == Qt::Key_Down)
deriv.position.x -= dinc;
IF_KEYSWITCH(gauche,evt->key() == Qt::Key_Right)
deriv.angle -= ainc;
Grégoire Payen de La Garanderie
committed
IF_KEYSWITCH(droite,evt->key() == Qt::Key_Left)
deriv.angle += ainc;
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
void Robot::makeJoint()
{
if(!elem)
return;
if(joint)
{
world.DestroyJoint(joint);
joint = NULL;
level = 0;
}
else
{
b2WeldJointDef jointDef;
jointDef.bodyA = body;
jointDef.bodyB = elem->body;
jointDef.localAnchorA = body->GetLocalPoint(elem->body->GetPosition());
joint = world.CreateJoint(&jointDef);
}
}
void Robot::interact(std::vector<class Element*> &elements)
{
Element* ne = NULL;
for(unsigned int i=0; i < elements.size(); i++)
{
Element* e = elements[i];
float d = (e->body->GetPosition() - body->GetWorldPoint(b2Vec2(1.64, 0.))).LengthSquared();
if(d < 0.01 && (e != elem || !ne))
ne = e;
}
if(elem && elem == ne)
return;
if(level == 100 && elem && ne)
{
for(std::vector<Element*>::iterator iter = elements.begin(); iter != elements.end(); iter++)
if(*iter == ne)
{
elements.erase(iter);
break;
}
//std::remove(elements.begin(), elements.end(), ne);
world.DestroyBody(ne->body);
elem->multiplier += ne->multiplier;
delete ne;
}
else
elem = ne;
}