Ajout d’un bras virtuel pour robot virtuel

parent 96567cd7
../include
\ No newline at end of file
../stm32/include
\ No newline at end of file
../src
\ No newline at end of file
../stm32/src
\ No newline at end of file
......@@ -11,6 +11,7 @@ class Robot
{
private:
bool manual;
b2World &world;
public:
PositionPlusAngle pos;
......@@ -21,12 +22,17 @@ public:
class Strategie* strategie;
b2Body* body;
b2Joint* joint;
class Element* elem;
unsigned int level;
Robot(b2World &world);
void paint(QPainter &p, int dt);
void keyPressEvent(QKeyEvent* evt,bool press);
void updateForces(int dt);
void interact(std::vector<class Element*> &elements);
void makeJoint();
};
#endif //ROBOT_H_INCLUDED
......
......@@ -4,7 +4,7 @@ Element::Element(b2World & world, Position p, Type t)
{
this->p = p;
type = t;
multiplier = 0;
multiplier = 1;
b2BodyDef bodyDef;
#ifndef BOX2D_2_0_1
......@@ -31,6 +31,17 @@ Element::Element(b2World & world, Position p, Type t)
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.4f;
//Categorie :
//1 - normal
//2 - Dame or Roi
//4 - Objet élevé
fixtureDef.filter.maskBits = 0x3;
fixtureDef.filter.categoryBits = 0x1;
if(type != Pawn)
{
fixtureDef.filter.categoryBits = 0x2;
fixtureDef.filter.maskBits |= 0x4;
}
body->CreateFixture(&fixtureDef);
#ifdef BOX2D_2_0_1
body->SetMassFromShapes();
......@@ -89,7 +100,7 @@ void Element::paint(QPainter & pa)
QString text;
if(type == Pawn)
{
if(multiplier > 0)
if(multiplier > 1)
text = QString::number(multiplier);
}
else
......@@ -98,9 +109,9 @@ void Element::paint(QPainter & pa)
text = "Q";
else
text = "K";
if(multiplier > 0)
text = QString(" ") + QString::number(multiplier);
if(multiplier > 1)
text += QString::fromStdWString(L"×") + QString::number(multiplier);
}
pa.drawText(p.x-50, -p.y+50, 100, -100, Qt::AlignCenter, text);
pa.drawText(p.x-80, -p.y+80, 160, -160, Qt::AlignCenter, text);
}
#include "simul/robot.h"
#include "simul/element.h"
#include <cmath>
......@@ -40,7 +41,7 @@ public:
};
Robot::Robot(b2World & world) : olds(10000)
Robot::Robot(b2World & world) : world(world), olds(10000)
{
deriv.position.x = 0;
......@@ -48,6 +49,9 @@ Robot::Robot(b2World & world) : olds(10000)
deriv.angle = 0;
manual = false;
elem = NULL;
joint = NULL;
level = 0;
odometrie = new OdoRobot(this);
asservissement = new Asservissement(odometrie);
......@@ -210,6 +214,32 @@ 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;
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;
......@@ -226,3 +256,56 @@ void Robot::keyPressEvent(QKeyEvent* evt, bool press)
}
}
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;
}
......@@ -116,7 +116,10 @@ void Table::update(int dt)
{
this->dt = dt;
for(unsigned int i=0; i < robots.size(); i++)
{
robots[i]->updateForces(dt);
robots[i]->interact(elements);
}
#ifdef BOX2D_2_0_1
world.Step((float)dt/1000., 10);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment