From 667425a4172869b273df37be0ceadaae656511d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Payen=20de=20La=20Garanderie?= <gregoire.payendelagaranderie@telecom-bretagne.eu> Date: Sun, 16 Jan 2011 13:46:34 +0100 Subject: [PATCH] =?UTF-8?q?Ajout=20des=20frottements=20solides=20sur=20le?= =?UTF-8?q?=20robot=20et=20les=20=C3=A9lements.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/simul/element.cpp | 31 ++++++++++++++++++++++++++++++- src/simul/robot.cpp | 7 ++++--- src/simul/table.cpp | 2 +- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/simul/element.cpp b/src/simul/element.cpp index 61c441bc..bc693b2b 100644 --- a/src/simul/element.cpp +++ b/src/simul/element.cpp @@ -18,7 +18,7 @@ Element::Element(b2World & world, Position p, Type t) b2FixtureDef fixtureDef; fixtureDef.shape = &circle; fixtureDef.density = 1.0f; - fixtureDef.friction = 0; //0.3f; + fixtureDef.friction = 0.4f; body->CreateFixture(&fixtureDef); } @@ -26,4 +26,33 @@ void Element::updatePos() { p.x = 100.*body->GetPosition().x; p.y = 100.*body->GetPosition().y; + + float friction = 0.5; + b2Vec2 velocity = body->GetLinearVelocity(); + float32 speed = velocity.Length(); + + if(speed > FLT_EPSILON) + { + float32 newSpeed = speed - friction; + if(newSpeed < 0) + newSpeed = 0; + velocity *= newSpeed/speed; + body->SetLinearVelocity(velocity); + } + + float angularFriction = 0.1; + float angular = body->GetAngularVelocity(); + if(angular > 0) + { + angular -= angularFriction; + if(angular < 0) + angular = 0; + } + else + { + angular += angularFriction; + if(angular > 0) + angular = 0; + } + body->SetAngularVelocity(angular); } diff --git a/src/simul/robot.cpp b/src/simul/robot.cpp index 562e518d..51382abb 100644 --- a/src/simul/robot.cpp +++ b/src/simul/robot.cpp @@ -72,7 +72,7 @@ Robot::Robot(b2World & world) : olds(10000) b2FixtureDef fixtureDef; fixtureDef.shape = &box; fixtureDef.density = 10.0f; - fixtureDef.friction = 0.f; + fixtureDef.friction = 1.0f; body->CreateFixture(&fixtureDef); } @@ -87,11 +87,12 @@ void Robot::updateForces(int dt) float32 rdt = 1000./(float)dt; - b2Vec2 bimpulse = 0.01*rdt*b2Vec2(impulse.x.getValueInMillimeters(),impulse.y.getValueInMillimeters()); + b2Vec2 bvelocity = 0.01*rdt*b2Vec2(impulse.x.getValueInMillimeters(),impulse.y.getValueInMillimeters()); float bangular = deriv.angle.getValueInRadian()*rdt; //body->ApplyForce(10*body->GetMass()*(bimpulse - body->GetLinearVelocity()), body->GetWorldCenter()); //body->ApplyTorque((bangular - body->GetAngularVelocity())*body->GetInertia()); - body->SetLinearVelocity(bimpulse); + + body->SetLinearVelocity(bvelocity); body->SetAngularVelocity(bangular); } diff --git a/src/simul/table.cpp b/src/simul/table.cpp index d570d655..09d293ab 100644 --- a/src/simul/table.cpp +++ b/src/simul/table.cpp @@ -26,7 +26,7 @@ Table::Table(QWidget* parent) : QWidget(parent), world(b2Vec2(0.f,0.f), false) b2PolygonShape box; b2FixtureDef fixtureDef; fixtureDef.shape = &box; - fixtureDef.friction = 0; + fixtureDef.friction = 0.5; fixtureDef.density = 0; box.SetAsBox(30,1, b2Vec2(0,-1),0); -- GitLab