1
0

Car.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. # include "Car.h"
  2. Car::Car()
  3. : m_bodyCar( 0x0 ), m_bodyWheelFwd( 0x0 ), m_bodyWheelBack( 0x0 ),
  4. m_motorAxe( 0x0 ), m_fwdAxe( 0x0 ),
  5. m_imgCar( 0x0 ), m_imgWheel( 0x0 ),
  6. m_cmd( FREE ),
  7. m_currentTorque( 0.0f ), m_currentSpeed( 0.0f ),
  8. m_goTorque( 0.7f ), m_minTorque( 0.0025f ), m_maxSpeed( 60.0f )
  9. {
  10. // Constructeur
  11. m_currentTorque = m_minTorque ;
  12. m_currentSpeed = 0.0f ;
  13. }
  14. Car::~Car()
  15. {
  16. if ( !m_imgCar )
  17. SDL_FreeSurface( m_imgCar );
  18. if ( !m_imgWheel )
  19. SDL_FreeSurface( m_imgWheel );
  20. }
  21. void Car::init( b2World &world, float x, float y, float angle )
  22. {
  23. // Images
  24. m_imgCar = SDL_LoadBMP("Truck.bmp");
  25. m_imgWheel = SDL_LoadBMP("Wheel.bmp");
  26. SDL_SetColorKey( m_imgCar, SDL_SRCCOLORKEY, SDL_MapRGBA( m_imgCar->format, 0, 0, 0, 255 ) );
  27. SDL_SetColorKey( m_imgWheel, SDL_SRCCOLORKEY, SDL_MapRGBA( m_imgWheel->format, 0, 0, 0, 255 ) );
  28. // Dimensions
  29. float wtruck, htruck, rwheel;
  30. wtruck = (float)m_imgCar->w / MULTI / 2 ;
  31. htruck = (float)m_imgCar->h / MULTI / 2 ;
  32. rwheel = (float)m_imgWheel->h / MULTI / 2 ;
  33. // Définition body
  34. b2BodyDef bodyDef;
  35. b2PolygonShape dynamicBox;
  36. b2FixtureDef fixtureDef;
  37. bodyDef.type = b2_dynamicBody;
  38. // Roue arrière
  39. bodyDef.userData.pointer = (uintptr_t) m_imgWheel ;
  40. bodyDef.position.Set( x - wtruck + rwheel, y + htruck + rwheel );
  41. b2CircleShape dynamicCircle;
  42. dynamicCircle.m_radius = rwheel ;
  43. fixtureDef.shape = &dynamicCircle;
  44. fixtureDef.density = 0.24f;
  45. fixtureDef.friction = 15.0f;
  46. m_bodyWheelBack = world.CreateBody(&bodyDef);
  47. m_bodyWheelBack->CreateFixture(&fixtureDef);
  48. // Roue avant
  49. bodyDef.position.Set( x + wtruck - rwheel, y + htruck + rwheel );
  50. dynamicCircle.m_radius = rwheel ;
  51. fixtureDef.shape = &dynamicCircle;
  52. fixtureDef.density = 0.24f;
  53. fixtureDef.friction = 15.0f;
  54. m_bodyWheelFwd = world.CreateBody(&bodyDef);
  55. m_bodyWheelFwd->CreateFixture(&fixtureDef);
  56. // Carrosserie
  57. bodyDef.userData.pointer = (uintptr_t) m_imgCar ;
  58. bodyDef.position.Set( x, y );
  59. dynamicBox.SetAsBox( wtruck, htruck );
  60. fixtureDef.shape = &dynamicBox;
  61. fixtureDef.density = 1.0f;
  62. fixtureDef.friction = 0.2f;
  63. m_bodyCar = world.CreateBody( &bodyDef );
  64. m_bodyCar->CreateFixture( &fixtureDef );
  65. // Création pivot avant
  66. b2RevoluteJointDef myJointDef;
  67. myJointDef.collideConnected = false ;
  68. myJointDef.Initialize( m_bodyWheelFwd, m_bodyCar, m_bodyWheelFwd->GetPosition() );
  69. m_fwdAxe = (b2RevoluteJoint*)world.CreateJoint(&myJointDef);
  70. // Création pivot arrière
  71. myJointDef.collideConnected = false ;
  72. myJointDef.Initialize( m_bodyWheelBack, m_bodyCar, m_bodyWheelBack->GetPosition() );
  73. myJointDef.maxMotorTorque = 0.05f;
  74. myJointDef.motorSpeed = 0.0f;
  75. myJointDef.enableMotor = true;
  76. m_motorAxe = (b2RevoluteJoint*)world.CreateJoint(&myJointDef);
  77. // Fin
  78. return ;
  79. }
  80. void Car::drive( command cmd )
  81. {
  82. m_cmd = cmd ;
  83. }
  84. void Car::update()
  85. {
  86. // Update state
  87. if ( m_cmd == FREE )
  88. {
  89. m_currentSpeed = 0.0f ;
  90. m_currentTorque = m_minTorque ;
  91. }
  92. else if ( m_cmd == BREAK )
  93. {
  94. m_currentSpeed = 0.0f ;
  95. m_currentTorque = m_goTorque ;
  96. }
  97. else if ( m_cmd == GO )
  98. {
  99. m_currentSpeed = -m_maxSpeed ;
  100. m_currentTorque = m_goTorque ;
  101. }
  102. else if ( m_cmd == REVERSE )
  103. {
  104. m_currentSpeed = m_maxSpeed ;
  105. m_currentTorque = m_goTorque ;
  106. }
  107. // Update motor
  108. m_motorAxe->SetMotorSpeed( m_currentSpeed );
  109. m_motorAxe->SetMaxMotorTorque( m_currentTorque );
  110. }
  111. b2Vec2 Car::GetPosition()
  112. {
  113. return m_bodyCar->GetPosition();
  114. }