From cb016c0bd9e16576abe727d1564b6ba10dbc289a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Payen=20de=20La=20Garanderie?= Date: Wed, 26 Jan 2011 15:50:17 +0100 Subject: [PATCH] =?UTF-8?q?Ajout=20d=E2=80=99un=20bras=20virtuel=20pour=20?= =?UTF-8?q?robot=20virtuel?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- simulation/include | 2 +- simulation/src | 2 +- stm32/include/simul/robot.h | 6 +++ stm32/src/simul/element.cpp | 21 ++++++--- stm32/src/simul/robot.cpp | 85 ++++++++++++++++++++++++++++++++++++- stm32/src/simul/table.cpp | 3 ++ 6 files changed, 111 insertions(+), 8 deletions(-) diff --git a/simulation/include b/simulation/include index f5030fe8..4184f36c 120000 --- a/simulation/include +++ b/simulation/include @@ -1 +1 @@ -../include \ No newline at end of file +../stm32/include \ No newline at end of file diff --git a/simulation/src b/simulation/src index 5cd551cf..49a38113 120000 --- a/simulation/src +++ b/simulation/src @@ -1 +1 @@ -../src \ No newline at end of file +../stm32/src \ No newline at end of file diff --git a/stm32/include/simul/robot.h b/stm32/include/simul/robot.h index ff9a99cd..d02e09f2 100644 --- a/stm32/include/simul/robot.h +++ b/stm32/include/simul/robot.h @@ -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 &elements); + void makeJoint(); }; #endif //ROBOT_H_INCLUDED diff --git a/stm32/src/simul/element.cpp b/stm32/src/simul/element.cpp index d2c15bce..2672011c 100644 --- a/stm32/src/simul/element.cpp +++ b/stm32/src/simul/element.cpp @@ -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); } diff --git a/stm32/src/simul/robot.cpp b/stm32/src/simul/robot.cpp index 58c9b87e..276684e2 100644 --- a/stm32/src/simul/robot.cpp +++ b/stm32/src/simul/robot.cpp @@ -1,4 +1,5 @@ #include "simul/robot.h" +#include "simul/element.h" #include @@ -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 &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::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; +} + diff --git a/stm32/src/simul/table.cpp b/stm32/src/simul/table.cpp index 3c4d3e33..4613c964 100644 --- a/stm32/src/simul/table.cpp +++ b/stm32/src/simul/table.cpp @@ -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); -- GitLab