22 Robot::Robot(b2World* world,
config::sRobot robotParameters,
double posX,
double posY,
double angle) {
26 createBody(world, robotParameters, posX, posY, angle);
31 printf(
" destructor called \n");
34 std::cout <<
"destruction of robot id: " << m_id << std::endl;
38 b2World* world = (m_robotBody->GetWorld());
39 m_robotBody->GetWorld()->DestroyBody(m_leftWheel);
40 m_robotBody->GetWorld()->DestroyBody(m_rightWheel);
41 m_robotBody->GetWorld()->DestroyBody(m_robotBody);
44 m_robotBody =
nullptr;
45 m_leftWheel =
nullptr;
46 m_rightWheel =
nullptr;
48 m_rightWheelMotor =
nullptr;
49 m_leftWheelMotor =
nullptr;
52 m_currentGripJoint =
nullptr;
57 m_robotParameters = robotParameters;
58 m_robotParameters.wheel_radius = (m_robotParameters.body_length - 0.02)/4;
59 m_robotParameters.wheel_distance = (m_robotParameters.body_length - 2*m_robotParameters.wheel_radius);
60 m_robotParameters.attach_height = m_robotParameters.wheel_radius/2;
64 BodyDef.position = b2Vec2(posX, posY);
65 BodyDef.angle = angle;
66 BodyDef.type = b2_dynamicBody;
67 BodyDef.bullet =
true;
68 m_robotBody = world->CreateBody(&BodyDef);
69 m_robotBody->SetUserData(
this );
71 b2PolygonShape boxShape;
72 boxShape.SetAsBox(m_robotParameters.wheel_distance/2,m_robotParameters.attach_height/2);
73 b2FixtureDef boxFixtureDef;
74 boxFixtureDef.shape = &boxShape;
75 boxFixtureDef.density = 2.f;
76 m_robotBody->CreateFixture(&boxFixtureDef);
79 BodyDef.position = m_robotBody->GetWorldPoint(b2Vec2(-m_robotParameters.wheel_distance/2, 0));
80 BodyDef.fixedRotation =
true;
82 m_leftWheel = world->CreateBody(&BodyDef);
83 m_leftWheel->SetUserData(
this );
86 b2CircleShape mcircleShape;
87 mcircleShape.m_p.Set(0, 0);
88 mcircleShape.m_radius = m_robotParameters.wheel_radius;
89 b2FixtureDef leftFixtureDef;
90 leftFixtureDef.density = 1.f;
91 leftFixtureDef.shape = &mcircleShape;
92 m_leftWheel->CreateFixture(&leftFixtureDef);
95 b2RevoluteJointDef revoluteJointDef;
96 revoluteJointDef.bodyA = m_leftWheel;
97 revoluteJointDef.bodyB = m_robotBody;
98 revoluteJointDef.localAnchorB.Set(-m_robotParameters.wheel_distance/2,0);
99 revoluteJointDef.localAnchorA.Set(0,0);
101 revoluteJointDef.collideConnected =
false;
102 m_leftWheelMotor =
static_cast<b2RevoluteJoint*
>(world->CreateJoint( &revoluteJointDef ));
103 m_leftWheelMotor->SetMaxMotorTorque(300.0f);
104 m_leftWheelMotor->EnableMotor(
true);
105 m_leftWheelMotor->SetMotorSpeed(0.0);
107 double a = m_leftWheelMotor->GetJointAngle();
108 m_leftWheelMotor->EnableLimit(
true);
109 m_leftWheelMotor->SetLimits( a-0.01, a+0.01 );
112 BodyDef.position = m_robotBody->GetWorldPoint(b2Vec2(m_robotParameters.wheel_distance/2, 0));
113 m_rightWheel = world->CreateBody(&BodyDef);
114 m_rightWheel->SetUserData(
this );
117 b2FixtureDef rightFixtureDef;
118 mcircleShape.m_p.Set(0, 0);
119 rightFixtureDef.shape = &mcircleShape;
120 rightFixtureDef.density = 1.f;
121 m_rightWheel->CreateFixture(&rightFixtureDef);
124 b2RevoluteJointDef revoluteJointDef2;
125 revoluteJointDef2.bodyA = m_rightWheel;
126 revoluteJointDef2.bodyB = m_robotBody;
127 revoluteJointDef2.localAnchorB.Set(m_robotParameters.wheel_distance/2,0);
128 revoluteJointDef2.localAnchorA.Set(0,0);
130 revoluteJointDef2.collideConnected =
false;
131 m_rightWheelMotor =
static_cast<b2RevoluteJoint*
>(world->CreateJoint( &revoluteJointDef2 ));
132 m_rightWheelMotor->SetMaxMotorTorque(300.0f);
133 m_rightWheelMotor->EnableMotor(
true);
134 m_rightWheelMotor->SetMotorSpeed(0.0);
136 a = m_rightWheelMotor->GetJointAngle();
137 m_rightWheelMotor->EnableLimit(
true);
138 m_rightWheelMotor->SetLimits( a-0.01, a+0.01 );
145 sf::CircleShape left_wheelShape(m_to_pix* m_robotParameters.wheel_radius);
146 if (m_state == BRIDGE){
149 if(m_movingSide == LEFT){
150 left_wheelShape.setFillColor(sf::Color(237,201,217));
153 left_wheelShape.setFillColor(sf::Color(172,197,217));
157 if(m_movingSide == LEFT){
158 left_wheelShape.setFillColor(sf::Color(246,125,150));
161 left_wheelShape.setFillColor(sf::Color(58,105,186));
164 double bodyAngle = m_leftWheel->GetAngle();
165 double topLeftX = m_leftWheel->GetWorldPoint(b2Vec2(-m_robotParameters.wheel_radius,-m_robotParameters.wheel_radius)).x;
166 double topLeftY = m_leftWheel->GetWorldPoint(b2Vec2(-m_robotParameters.wheel_radius,-m_robotParameters.wheel_radius)).y;
168 left_wheelShape.setPosition(m_to_pix*topLeftX,m_to_pix*topLeftY);
169 left_wheelShape.setRotation(bodyAngle*RAD_TO_DEG);
171 sf::Vertex angle_left[2];
172 b2Vec2 start = m_leftWheel->GetWorldCenter();
173 b2Vec2 end = m_leftWheel->GetWorldPoint(b2Vec2(0,m_robotParameters.wheel_radius));
174 angle_left[0]= sf::Vertex(sf::Vector2f(start.x*m_to_pix, start.y*m_to_pix), sf::Color::Black);
175 angle_left[1]= sf::Vertex(sf::Vector2f(end.x*m_to_pix, end.y*m_to_pix), sf::Color::Black);
178 sf::CircleShape right_wheelShape(m_to_pix* m_robotParameters.wheel_radius);
180 if (m_state == BRIDGE){
182 if(m_movingSide == RIGHT){
183 right_wheelShape.setFillColor(sf::Color(237,201,217));
186 right_wheelShape.setFillColor(sf::Color(172,197,217));
191 if(m_movingSide == RIGHT){
192 right_wheelShape.setFillColor(sf::Color(246,125,150));
195 right_wheelShape.setFillColor(sf::Color(58,105,186));
198 bodyAngle = m_rightWheel->GetAngle();
199 topLeftX = m_rightWheel->GetWorldPoint(b2Vec2(-m_robotParameters.wheel_radius,-m_robotParameters.wheel_radius)).x;
200 topLeftY = m_rightWheel->GetWorldPoint(b2Vec2(-m_robotParameters.wheel_radius,-m_robotParameters.wheel_radius)).y;
202 right_wheelShape.setPosition(m_to_pix*topLeftX,m_to_pix*topLeftY);
203 right_wheelShape.setRotation(bodyAngle*RAD_TO_DEG);
205 sf::Vertex angle_right[2];
206 start = m_rightWheel->GetWorldCenter();
207 end = m_rightWheel->GetWorldPoint(b2Vec2(0, m_robotParameters.wheel_radius));
208 angle_right[0] = sf::Vertex(sf::Vector2f(start.x*m_to_pix, start.y*m_to_pix), sf::Color::Black);
209 angle_right[1] = sf::Vertex(sf::Vector2f(end.x*m_to_pix, end.y*m_to_pix), sf::Color::Black);
212 sf::RectangleShape attachShape(sf::Vector2f(m_robotParameters.wheel_distance*m_to_pix, m_robotParameters.attach_height*m_to_pix));
213 attachShape.setFillColor(sf::Color::Black);
214 bodyAngle = m_robotBody->GetAngle();
215 topLeftX = m_robotBody->GetWorldPoint(b2Vec2(-m_robotParameters.wheel_distance/2,-m_robotParameters.attach_height/2)).x;
216 topLeftY = m_robotBody->GetWorldPoint(b2Vec2(-m_robotParameters.wheel_distance/2,-m_robotParameters.attach_height/2)).y;
218 attachShape.setPosition(m_to_pix*topLeftX,m_to_pix*topLeftY);
219 attachShape.setRotation(bodyAngle*RAD_TO_DEG);
221 window.draw(attachShape);
223 window.draw(left_wheelShape);
224 window.draw(right_wheelShape);
243 edge[0] = sf::Vertex(sf::Vector2f(pA.x*m_to_px, m_to_px*pA.y), sf::Color::Black);
246 float xB = delta*m_leftWheel->GetLocalPoint(pA).x;
247 float yB = delta*m_leftWheel->GetLocalPoint(pA).y;
248 b2Vec2 pB = m_leftWheel->GetWorldPoint(b2Vec2(xB, yB));
249 edge[1] = sf::Vertex(sf::Vector2f(m_to_px*pB.x, m_to_px*pB.y), sf::Color::Black);
253 float xB = delta*m_rightWheel->GetLocalPoint(pA).x;
254 float yB = delta*m_rightWheel->GetLocalPoint(pA).y;
255 b2Vec2 pB = m_rightWheel->GetWorldPoint(b2Vec2(xB, yB));
256 edge[1] = sf::Vertex(sf::Vector2f(m_to_px*pB.x, m_to_px*pB.y), sf::Color::Black);
261 p1.setFillColor(sf::Color::Black);
262 p1.setPosition(m_to_px*pA.x-3,pA.y*m_to_px-3);
264 window.draw(edge, 2, sf::Lines);
271 if(m_currentGripJoint){
272 b2Vec2 pA = m_currentGripJoint->GetAnchorA();
276 edge[0] = sf::Vertex(sf::Vector2f(pA.x*m_to_px, m_to_px*pA.y), sf::Color::Black);
278 if((m_currentGripJoint->GetBodyA() == m_leftWheel) || (m_currentGripJoint->GetBodyB() == m_leftWheel)){
279 float xB = delta*m_leftWheel->GetLocalPoint(pA).x;
280 float yB = delta*m_leftWheel->GetLocalPoint(pA).y;
281 b2Vec2 pB = m_leftWheel->GetWorldPoint(b2Vec2(xB, yB));
282 edge[1] = sf::Vertex(sf::Vector2f(m_to_px*pB.x, m_to_px*pB.y), sf::Color::Black);
285 else if(m_currentGripJoint->GetBodyA() == m_rightWheel || (m_currentGripJoint->GetBodyB() == m_rightWheel)){
286 float xB = delta*m_rightWheel->GetLocalPoint(pA).x;
287 float yB = delta*m_rightWheel->GetLocalPoint(pA).y;
288 b2Vec2 pB = m_rightWheel->GetWorldPoint(b2Vec2(xB, yB));
289 edge[1] = sf::Vertex(sf::Vector2f(m_to_px*pB.x, m_to_px*pB.y), sf::Color::Black);
294 p1.setFillColor(sf::Color::Black);
295 p1.setPosition(m_to_px*pA.x-3,pA.y*m_to_px-3);
297 window.draw(edge, 2, sf::Lines);
304 b2JointEdge* jointEdge = m_leftWheel->GetJointList();
306 if(jointEdge ==
nullptr){
310 b2Joint* joint = jointEdge->joint;
311 b2Vec2 pA = joint->GetAnchorA();
312 b2Vec2 pB = joint->GetAnchorB();
315 edge[0] = sf::Vertex(sf::Vector2f(pA.x*m_to_px, m_to_px*pA.y), sf::Color::Black);
316 edge[1] = sf::Vertex(sf::Vector2f(m_to_px*pB.x, m_to_px*pB.y), sf::Color::Black);
321 p1.setFillColor(sf::Color::Black);
322 p1.setPosition(m_to_px*pA.x-3,pA.y*m_to_px-3);
324 p2.setFillColor(sf::Color::Black);
325 p2.setPosition(m_to_px*pB.x-3,pB.y*m_to_px-3);
327 window.draw(edge, 2, sf::Lines);
331 if(!(jointEdge->next ==
nullptr)){
332 joint = jointEdge->next->joint;
333 pA = joint->GetAnchorA();
334 pB = joint->GetAnchorB();
336 edge[0] = sf::Vertex(sf::Vector2f(pA.x*m_to_px, m_to_px*pA.y), sf::Color::Black);
337 edge[1] = sf::Vertex(sf::Vector2f(m_to_px*pB.x, m_to_px*pB.y), sf::Color::Black);
339 p1.setPosition(m_to_px*pA.x-3,pA.y*m_to_px-3);
340 p2.setPosition(m_to_px*pB.x-3,pB.y*m_to_px-3);
342 window.draw(edge, 2, sf::Lines);
346 if(!(jointEdge->next->next ==
nullptr)){
347 joint = jointEdge->next->next->joint;
348 pA = joint->GetAnchorA();
349 pB = joint->GetAnchorB();
351 edge[0] = sf::Vertex(sf::Vector2f(pA.x*m_to_px, m_to_px*pA.y), sf::Color::Black);
352 edge[1] = sf::Vertex(sf::Vector2f(m_to_px*pB.x, m_to_px*pB.y), sf::Color::Black);
353 p1.setPosition(m_to_px*pA.x-3,pA.y*m_to_px-3);
354 p2.setPosition(m_to_px*pB.x-3,pB.y*m_to_px-3);
356 window.draw(edge, 2, sf::Lines);
362 if(!(jointEdge->prev ==
nullptr)){
363 joint = jointEdge->prev->joint;
364 pA = joint->GetAnchorA();
365 pB = joint->GetAnchorB();
367 edge[0] = sf::Vertex(sf::Vector2f(pA.x*m_to_px, m_to_px*pA.y), sf::Color::Black);
368 edge[1] = sf::Vertex(sf::Vector2f(m_to_px*pB.x, m_to_px*pB.y), sf::Color::Black);
369 window.draw(edge, 2, sf::Lines);
370 p1.setPosition(m_to_px*pA.x-3,pA.y*m_to_px-3);
371 p2.setPosition(m_to_px*pB.x-3,pB.y*m_to_px-3);
380 jointEdge = m_rightWheel->GetJointList();
381 joint = jointEdge->joint;
382 pA = joint->GetAnchorA();
383 pB = joint->GetAnchorB();
385 edge[0] = sf::Vertex(sf::Vector2f(pA.x*m_to_px, m_to_px*pA.y), sf::Color::Black);
386 edge[1] = sf::Vertex(sf::Vector2f(m_to_px*pB.x, m_to_px*pB.y), sf::Color::Black);
388 p1.setPosition(m_to_px*pA.x-3,pA.y*m_to_px-3);
389 p2.setPosition(m_to_px*pB.x-3,pB.y*m_to_px-3);
391 window.draw(edge, 2, sf::Lines);
395 if(!(jointEdge->next ==
nullptr)){
396 joint = jointEdge->next->joint;
397 pA = joint->GetAnchorA();
398 pB = joint->GetAnchorB();
400 edge[0] = sf::Vertex(sf::Vector2f(pA.x*m_to_px, m_to_px*pA.y), sf::Color::Black);
401 edge[1] = sf::Vertex(sf::Vector2f(m_to_px*pB.x, m_to_px*pB.y), sf::Color::Black);
403 p1.setPosition(m_to_px*pA.x-3,pA.y*m_to_px-3);
404 p2.setPosition(m_to_px*pB.x-3,pB.y*m_to_px-3);
406 window.draw(edge, 2, sf::Lines);
410 if(!(jointEdge->next->next ==
nullptr)){
411 joint = jointEdge->next->next->joint;
412 pA = joint->GetAnchorA();
413 pB = joint->GetAnchorB();
415 edge[0] = sf::Vertex(sf::Vector2f(pA.x*m_to_px, m_to_px*pA.y), sf::Color::Black);
416 edge[1] = sf::Vertex(sf::Vector2f(m_to_px*pB.x, m_to_px*pB.y), sf::Color::Black);
417 p1.setPosition(m_to_px*pA.x-3,pA.y*m_to_px-3);
418 p2.setPosition(m_to_px*pB.x-3,pB.y*m_to_px-3);
420 window.draw(edge, 2, sf::Lines);
426 if(!(jointEdge->prev ==
nullptr)){
427 joint = jointEdge->prev->joint;
428 pA = joint->GetAnchorA();
429 pB = joint->GetAnchorB();
431 edge[0] = sf::Vertex(sf::Vector2f(pA.x*m_to_px, m_to_px*pA.y), sf::Color::Black);
432 edge[1] = sf::Vertex(sf::Vector2f(m_to_px*pB.x, m_to_px*pB.y), sf::Color::Black);
433 window.draw(edge, 2, sf::Lines);
434 p1.setPosition(m_to_px*pA.x-3,pA.y*m_to_px-3);
435 p2.setPosition(m_to_px*pB.x-3,pB.y*m_to_px-3);
444 if (m_movingSide == RIGHT){
445 m_rightWheelMotor->EnableMotor(
false);
449 m_leftWheelMotor->EnableMotor(
true);
450 m_leftWheelMotor->SetMotorSpeed(m_angularSpeed);
451 m_leftWheelMotor->SetMaxMotorTorque(30.0f);
455 m_leftWheelMotor->EnableMotor(
false);
459 m_rightWheelMotor->EnableMotor(
true);
460 m_rightWheelMotor->SetMotorSpeed(m_angularSpeed);
461 m_rightWheelMotor->SetMaxMotorTorque(30.0f);
472 b2Vec2 strength(0,0);
479 if (m_movingSide == RIGHT){
480 center.Set(m_robotParameters.wheel_distance/2,0);
481 strength.Set((m_robotBody->GetWorldVector(b2Vec2(0,v))).x, (m_robotBody->GetWorldVector(b2Vec2(0,v))).y);
482 m_robotBody->ApplyForceToCenter( strength,
true);
486 else if (m_movingSide == LEFT){
488 center.Set(-m_robotParameters.wheel_distance/2,0);
489 strength.Set((m_robotBody->GetWorldVector(b2Vec2(0,-v))).x, (m_robotBody->GetWorldVector(b2Vec2(0,-v))).y);
490 m_robotBody->ApplyForceToCenter( strength,
true);
497 b2Vec2 strength(0,0);
503 double vel = m_robotBody->GetAngularVelocity();
505 double velChange = m_angularSpeed - vel;
506 double impulse = m_robotBody->GetInertia() * velChange *10;
510 m_robotBody->ApplyAngularImpulse( impulse,
true);
515 m_rightWheelMotor->EnableMotor(
false);
519 m_leftWheelMotor->EnableMotor(
false);
524 m_rightWheelMotor->EnableMotor(
true);
525 m_rightWheelMotor->SetMotorSpeed(0.0);
526 m_rightWheelMotor->SetMaxMotorTorque(30.0f);
528 double angle = m_rightWheelMotor->GetJointAngle();
529 m_rightWheelMotor->EnableLimit(
true);
530 m_rightWheelMotor->SetLimits( angle-0.1, angle+0.1 );
534 m_leftWheelMotor->EnableMotor(
true);
535 m_leftWheelMotor->SetMotorSpeed(0.0);
536 m_leftWheelMotor->SetMaxMotorTorque(30.0f);
538 double angle = m_leftWheelMotor->GetJointAngle();
539 m_leftWheelMotor->EnableLimit(
true);
540 m_leftWheelMotor->SetLimits( angle-0.01, angle+0.01 );
545 m_rightWheelMotor->EnableMotor(
false);
547 m_rightWheelMotor->SetMaxMotorTorque(500.0f);
549 m_referenceAngleJoint = m_rightWheelMotor->GetJointAngle();
550 m_rightWheelMotor->EnableLimit(
true);
551 m_rightWheelMotor->SetLimits( m_referenceAngleJoint-limit_angle_rad, m_referenceAngleJoint+limit_angle_rad );
555 m_leftWheelMotor->EnableMotor(
false);
557 m_leftWheelMotor->SetMaxMotorTorque(500.0f);
559 m_referenceAngleJoint = m_leftWheelMotor->GetJointAngle();
560 m_leftWheelMotor->EnableLimit(
true);
561 m_leftWheelMotor->SetLimits( m_referenceAngleJoint-limit_angle_rad, m_referenceAngleJoint+limit_angle_rad );
566 m_rightWheelMotor->EnableLimit(
false);
570 m_leftWheelMotor->EnableLimit(
false);
575 void Robot::grip(b2Contact* contact, b2Body* otherBody,
double m_to_px){
577 b2WorldManifold worldManifold;
578 contact->GetWorldManifold(&worldManifold);
579 b2Body* bodyA = contact->GetFixtureA()->GetBody();
580 b2Body* bodyB = contact->GetFixtureB()->GetBody();
583 double xB = otherBody->GetLocalPoint(worldManifold.points[0]).x;
584 double yB = otherBody->GetLocalPoint(worldManifold.points[0]).y;
588 if(bodyA==m_leftWheel || bodyB==m_leftWheel)
592 if(!m_movingSide == LEFT){
593 printf(
"error contact side\n");
596 wheel_center.Set(0, 0);
597 double xA = m_leftWheel->GetLocalPoint(worldManifold.points[0]).x;
598 double yA = m_leftWheel->GetLocalPoint(worldManifold.points[0]).y;
599 m_leftGripJointDef.bodyA = m_leftWheel;
600 m_leftGripJointDef.bodyB = otherBody;
601 m_leftGripJointDef.collideConnected =
true;
602 m_leftGripJointDef.localAnchorA.Set(xA,yA);
603 m_leftGripJointDef.localAnchorB.Set(xB,yB);
605 m_shapeLeft.setRadius(10);
606 m_shapeLeft.setFillColor(sf::Color(100, 250, 50));
607 m_shapeLeft.setPosition(m_to_px*worldManifold.points[0].x-10,worldManifold.points[0].y*m_to_px-10);
610 else if(bodyA == m_rightWheel || bodyB == m_rightWheel)
613 if(m_movingSide == LEFT){
615 m_movingSide = RIGHT;
618 wheel_center.Set(0, 0);
619 double xA = m_rightWheel->GetLocalPoint(worldManifold.points[0]).x;
620 double yA = m_rightWheel->GetLocalPoint(worldManifold.points[0]).y;
621 m_rightGripJointDef.bodyA = m_rightWheel;
622 m_rightGripJointDef.bodyB = otherBody;
623 m_rightGripJointDef.collideConnected =
true;
624 m_rightGripJointDef.localAnchorA.Set(xA,yA);
625 m_rightGripJointDef.localAnchorB.Set(xB,yB);
627 m_shapeRight.setRadius(10);
628 m_shapeRight.setFillColor(sf::Color(100, 250, 50));
629 m_shapeRight.setPosition(m_to_px*worldManifold.points[0].x-10,worldManifold.points[0].y*m_to_px-10);
637 m_normal[0] = sf::Vertex(sf::Vector2f(xB*m_to_px, m_to_px*yB), sf::Color(200, 200, 150));
638 m_normal[1] = sf::Vertex(sf::Vector2f(m_to_px*(xB+(worldManifold.normal).x), m_to_px*(yB+(worldManifold.normal).y)), sf::Color(200, 200, 150));
643 if(!contactOnGripSide(contact)){
647 b2WorldManifold worldManifold;
648 contact->GetWorldManifold(&worldManifold);
649 b2Body* bodyA = contact->GetFixtureA()->GetBody();
650 b2Body* bodyB = contact->GetFixtureB()->GetBody();
654 int numPoints = contact->GetManifold()->pointCount;
657 double xB = otherBody->GetLocalPoint(worldManifold.points[0]).x;
658 double yB = otherBody->GetLocalPoint(worldManifold.points[0]).y;
662 b2Body* movingWheel =
getWheel(m_movingSide);
664 if(bodyA==movingWheel || bodyB==movingWheel)
669 wheel_center.Set(0, 0);
670 double xA = movingWheel->GetLocalPoint(worldManifold.points[0]).x;
671 double yA = movingWheel->GetLocalPoint(worldManifold.points[0]).y;
673 double x = otherBody->GetLocalVector(worldManifold.normal).x;
674 double y = otherBody->GetLocalVector(worldManifold.normal).y;
676 if(m_movingSide == LEFT){
677 m_leftGripJointDef.bodyB = movingWheel;
678 m_leftGripJointDef.bodyA = otherBody;
679 m_leftGripJointDef.collideConnected =
true;
680 m_leftGripJointDef.localAnchorB.Set(xA,yA);
681 m_leftGripJointDef.localAnchorA.Set(xB,yB);
688 m_leftGripJointDef.enableLimit =
true;
689 m_leftGripJointDef.lowerTranslation = 0;
690 m_leftGripJointDef.upperTranslation = 0.001;
694 m_shapeLeft.setRadius(10);
695 m_shapeLeft.setFillColor(sf::Color(100, 250, 50));
696 m_shapeLeft.setPosition(m_to_px*(worldManifold.points[0].x+worldManifold.normal.x)-10,(worldManifold.points[0].y+worldManifold.normal.y)*m_to_px-10);
702 m_rightGripJointDef.bodyB = movingWheel;
703 m_rightGripJointDef.bodyA = otherBody;
704 m_rightGripJointDef.collideConnected =
true;
705 m_rightGripJointDef.localAnchorB.Set(xA,yA);
706 m_rightGripJointDef.localAnchorA.Set(xB,yB);
712 m_rightGripJointDef.localAxisA.Set(x, y);
714 m_rightGripJointDef.enableLimit =
true;
715 m_rightGripJointDef.lowerTranslation = 0;
716 m_rightGripJointDef.upperTranslation = 0.001;
720 m_shapeRight.setRadius(10);
721 m_shapeRight.setFillColor(sf::Color(100, 250, 50));
722 m_shapeRight.setPosition(m_to_px*(worldManifold.points[0].x+worldManifold.normal.x)-10,(worldManifold.points[0].y+worldManifold.normal.y)*m_to_px-10);
732 b2ContactEdge* contactList = m_leftWheel->GetContactList();
734 if((contactList ==
nullptr))
739 b2Contact* contact = contactList->contact;
741 b2Body* bodyA = contact->GetFixtureA()->GetBody();
742 b2Body* bodyB = contact->GetFixtureB()->GetBody();
743 b2World* world = bodyA->GetWorld();
745 if(!(bodyA==m_robotBody || bodyB==m_robotBody ))
748 b2WorldManifold worldManifold;
749 contact->GetWorldManifold(&worldManifold);
751 double xA = bodyA->GetLocalPoint(worldManifold.points[0]).x;
752 double yA = bodyA->GetLocalPoint(worldManifold.points[0]).y;
753 double xB = bodyB->GetLocalPoint(worldManifold.points[0]).x;
754 double yB = bodyB->GetLocalPoint(worldManifold.points[0]).y;
755 m_leftGripJointDef.bodyA = bodyA;
756 m_leftGripJointDef.bodyB = bodyB;
757 m_leftGripJointDef.collideConnected =
true;
758 m_leftGripJointDef.localAnchorA.Set(xA,yA);
759 m_leftGripJointDef.localAnchorB.Set(xB,yB);
761 m_leftGripJointDef.enableLimit =
true;
762 m_leftGripJointDef.lowerTranslation = 0;
763 m_leftGripJointDef.upperTranslation = 0.001;
765 m_leftGripJointDef.localAxisA.Set(0.0f, -1.0f);
769 contact = contactList->next->contact;
771 bodyA = contact->GetFixtureA()->GetBody();
772 bodyB = contact->GetFixtureB()->GetBody();
774 if(!(bodyA==m_robotBody || bodyB==m_robotBody ))
776 b2WorldManifold worldManifold;
777 contact->GetWorldManifold(&worldManifold);
779 double xA = bodyA->GetLocalPoint(worldManifold.points[0]).x;
780 double yA = bodyA->GetLocalPoint(worldManifold.points[0]).y;
781 double xB = bodyB->GetLocalPoint(worldManifold.points[0]).x;
782 double yB = bodyB->GetLocalPoint(worldManifold.points[0]).y;
783 m_leftGripJointDef.bodyA = bodyA;
784 m_leftGripJointDef.bodyB = bodyB;
785 m_leftGripJointDef.collideConnected =
true;
786 m_leftGripJointDef.localAnchorA.Set(xA,yA);
787 m_leftGripJointDef.localAnchorB.Set(xB,yB);
789 m_leftGripJointDef.enableLimit =
true;
790 m_leftGripJointDef.lowerTranslation = 0;
791 m_leftGripJointDef.upperTranslation = 0.001;
793 m_leftGripJointDef.localAxisA.Set(0.0f, -1.0f);
797 if(!(&m_leftGripJointDef==
nullptr)){
798 world->CreateJoint( &m_leftGripJointDef );
802 contactList = m_rightWheel->GetContactList();
803 contact = contactList->contact;
805 bodyA = contact->GetFixtureA()->GetBody();
806 bodyB = contact->GetFixtureB()->GetBody();
808 if(!(bodyA==m_robotBody || bodyB==m_robotBody ))
811 b2WorldManifold worldManifold;
812 contact->GetWorldManifold(&worldManifold);
814 double xA = bodyA->GetLocalPoint(worldManifold.points[0]).x;
815 double yA = bodyA->GetLocalPoint(worldManifold.points[0]).y;
816 double xB = bodyB->GetLocalPoint(worldManifold.points[0]).x;
817 double yB = bodyB->GetLocalPoint(worldManifold.points[0]).y;
818 m_rightGripJointDef.bodyA = bodyA;
819 m_rightGripJointDef.bodyB = bodyB;
820 m_rightGripJointDef.collideConnected =
true;
821 m_rightGripJointDef.localAnchorA.Set(xA,yA);
822 m_rightGripJointDef.localAnchorB.Set(xB,yB);
824 m_rightGripJointDef.enableLimit =
true;
825 m_rightGripJointDef.lowerTranslation = 0;
826 m_rightGripJointDef.upperTranslation = 0.001;
828 m_rightGripJointDef.localAxisA.Set(0.0f, -1.0f);
832 contact = contactList->next->contact;
834 bodyA = contact->GetFixtureA()->GetBody();
835 bodyB = contact->GetFixtureB()->GetBody();
837 if(!(bodyA==m_robotBody || bodyB==m_robotBody ))
839 b2WorldManifold worldManifold;
840 contact->GetWorldManifold(&worldManifold);
842 double xA = bodyA->GetLocalPoint(worldManifold.points[0]).x;
843 double yA = bodyA->GetLocalPoint(worldManifold.points[0]).y;
844 double xB = bodyB->GetLocalPoint(worldManifold.points[0]).x;
845 double yB = bodyB->GetLocalPoint(worldManifold.points[0]).y;
846 m_rightGripJointDef.bodyA = bodyA;
847 m_rightGripJointDef.bodyB = bodyB;
848 m_rightGripJointDef.collideConnected =
true;
849 m_rightGripJointDef.localAnchorA.Set(xA,yA);
850 m_rightGripJointDef.localAnchorB.Set(xB,yB);
852 m_rightGripJointDef.enableLimit =
true;
853 m_rightGripJointDef.lowerTranslation = 0;
854 m_rightGripJointDef.upperTranslation = 0.001;
856 m_rightGripJointDef.localAxisA.Set(0.0f, -1.0f);
860 if(!(&m_rightGripJointDef==
nullptr)){
861 world->CreateJoint( &m_rightGripJointDef );
866 bool Robot::contactOnGripSide(b2Contact* contact){
868 double angle =(
getBody()->GetAngle());
872 angle = fmod(angle, (2*PI));
875 b2WorldManifold worldManifold;
876 contact->GetWorldManifold(&worldManifold);
877 b2Body* bodyA = contact->GetFixtureA()->GetBody();
878 b2Body* bodyB = contact->GetFixtureB()->GetBody();
880 b2Body* movingWheel =
getWheel(m_movingSide);
882 if(bodyA==movingWheel || bodyB==movingWheel)
885 double x0 = movingWheel->GetLocalPoint(worldManifold.points[0]).x;
886 double y0 = movingWheel->GetLocalPoint(worldManifold.points[0]).y;
887 double y = y0*cos(angle)- x0*sin(angle);
888 if(m_movingSide == RIGHT){
904 b2JointEdge* jointList = m_rightWheel->GetJointList();
905 b2Joint* joint = jointList->joint;
906 for (
int i=0; i<6; i++){
908 if(!(joint == m_rightWheelMotor)){
912 if((jointList->next ==
nullptr)){
915 jointList = jointList->next;
916 joint = jointList->joint;
921 b2JointEdge* jointList = m_leftWheel->GetJointList();
922 b2Joint* joint = jointList->joint;
923 for (
int i=0; i<6; i++){
925 if(!(joint == m_leftWheelMotor)){
929 if((jointList->next ==
nullptr)){
932 jointList = jointList->next;
933 joint = jointList->joint;
940 bool rightGrabbed =
false;
941 bool leftGrabbed =
false;
943 b2JointEdge* jointList = m_rightWheel->GetJointList();
944 b2Joint* joint = jointList->joint;
946 for (
int i=0; i<6; i++){
948 if(!(joint == m_rightWheelMotor || joint == m_currentGripJoint || joint ==
m_previousGripJoint)){
953 if((jointList->next ==
nullptr)){
956 jointList = jointList->next;
957 joint = jointList->joint;
961 jointList = m_leftWheel->GetJointList();
962 joint = jointList->joint;
964 for (
int i=0; i<6; i++){
966 if(!(joint == m_leftWheelMotor || joint == m_currentGripJoint || joint ==
m_previousGripJoint)){
971 if((jointList->next ==
nullptr)){
974 jointList = jointList->next;
975 joint = jointList->joint;
978 m_isGrabbed = (leftGrabbed || rightGrabbed);
979 return (leftGrabbed || rightGrabbed);
984 return m_leftWheelMotor;
987 return m_rightWheelMotor;
993 return m_nbJointLeft;
996 return m_nbJointRight;
1021 m_contact = contact;
1031 m_angularSpeed = speed;
1038 double a = m_robotBody->GetAngle();
1045 double bl = 2*m_robotParameters.wheel_radius + m_robotParameters.wheel_distance;
1058 return m_leftGripJointDef;
1061 return m_rightGripJointDef;
1066 b2Vec2 pos = m_robotBody->GetPosition();
1077 return m_rightWheel;
1094 m_leftWheel->GetWorld()->DestroyBody( m_leftWheel );
1095 m_rightWheel->GetWorld()->DestroyBody( m_rightWheel );
1096 m_robotBody->GetWorld()->DestroyBody( m_robotBody );
void drawGripJoint(sf::RenderWindow &window, double m_to_px)
Implement the Robot class and define enum linked to a robot object: e_state and side.
void drawJoint(sf::RenderWindow &window, double m_to_px)
void allowMotorRotation(side s)
void setSpeed(double speed)
void blockMotorRotation(side s)
Implementation of the Configuration class used to parse the configuration file and the sConfig struct...
b2PrismaticJointDef getJointDef(side s)
b2Body * getWheel(side s)
void moveBodyWithImpulse()
void limitMotorRotation(side s, double limit_angle_rad)
void createBody(b2World *world, config::sRobot m_robotParameters, double posX, double posY, double angle=0)
void resetNbJoint(side s)
void grip(b2Contact *contact, b2Body *otherBody, double m_to_px)
void incrementNbJoint(side s)
void setContact(bool contact)
b2RevoluteJoint * getMotor(side s)
bool gripSide(b2Contact *contact, b2Body *otherBody, double m_to_px)
void drawBody(sf::RenderWindow &window, double m_to_pix)
void setState(e_state state)
b2PrismaticJoint * m_previousGripJoint
void turnOffMotor(side s)