creater.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Basiques
  2. # include <iostream>
  3. # include <cmath>
  4. # include <vector>
  5. // SDL
  6. # include <SDL/SDL.h>
  7. # undef main
  8. # include <SDL/SDL_rotozoom.h>
  9. # include <SDL/SDL_gfxPrimitives.h>
  10. // Box2D
  11. # include <box2d/box2d.h>
  12. # define MULTI 100.0f
  13. // Prototypes
  14. # include "creater.h"
  15. void createBloc( b2World &world, std::vector<b2Body*> &tbody, SDL_Surface* blc, float x, float y )
  16. {
  17. // Définition
  18. b2BodyDef bodyDef;
  19. bodyDef.type = b2_dynamicBody;
  20. bodyDef.userData.pointer = (uintptr_t) blc;
  21. bodyDef.position.Set( x, y );
  22. // Shape
  23. b2PolygonShape dynamicBox;
  24. dynamicBox.SetAsBox( (float)blc->w * 2.0f / MULTI / 2 , (float)blc->h * 2.0f / MULTI / 2 );
  25. // Fixture
  26. b2FixtureDef fixtureDef;
  27. fixtureDef.shape = &dynamicBox;
  28. fixtureDef.density = 1.0f;
  29. fixtureDef.friction = 0.3f;
  30. // Push
  31. tbody.push_back( 0x0 );
  32. tbody.back() = world.CreateBody(&bodyDef);
  33. tbody.back()->CreateFixture(&fixtureDef);
  34. }
  35. void link2ByDist( b2World &world, std::vector<b2Body*> &tbody, std::vector<b2Joint*> &tjoint )
  36. {
  37. if ( tbody.size() < 2 )
  38. return ;
  39. b2Vec2 posA, posB;
  40. b2DistanceJointDef myDistJointDef;
  41. myDistJointDef.collideConnected = true ;
  42. myDistJointDef.bodyA = tbody[ tbody.size() - 2];
  43. myDistJointDef.bodyB = tbody[ tbody.size() - 1];
  44. posA = myDistJointDef.bodyA->GetPosition();
  45. posB = myDistJointDef.bodyB->GetPosition();
  46. myDistJointDef.length = ( posA - posB ).Length() / 2.0f;
  47. myDistJointDef.stiffness = 4.0f ;
  48. myDistJointDef.damping = 0.5f ;
  49. tjoint.push_back( 0x0 );
  50. tjoint.back() = world.CreateJoint(&myDistJointDef);
  51. }
  52. void link3ByDist( b2World &world, std::vector<b2Body*> &tbody, std::vector<b2Joint*> &tjoint )
  53. {
  54. if ( tbody.size() < 3 )
  55. return ;
  56. b2Vec2 posA, posB;
  57. b2DistanceJointDef myDistJointDef;
  58. myDistJointDef.collideConnected = true ;
  59. myDistJointDef.stiffness = 4.0f ;
  60. myDistJointDef.damping = 0.5f ;
  61. // Joint 1
  62. myDistJointDef.bodyA = tbody[ tbody.size() - 2];
  63. myDistJointDef.bodyB = tbody[ tbody.size() - 1];
  64. posA = myDistJointDef.bodyA->GetPosition();
  65. posB = myDistJointDef.bodyB->GetPosition();
  66. myDistJointDef.length = ( posA - posB ).Length() / 2.0f;
  67. tjoint.push_back( 0x0 );
  68. tjoint.back() = world.CreateJoint(&myDistJointDef);
  69. // Joint 2
  70. myDistJointDef.bodyA = tbody[ tbody.size() - 3];
  71. myDistJointDef.bodyB = tbody[ tbody.size() - 1];
  72. posA = myDistJointDef.bodyA->GetPosition();
  73. posB = myDistJointDef.bodyB->GetPosition();
  74. myDistJointDef.length = ( posA - posB ).Length() / 2.0f;
  75. tjoint.push_back( 0x0 );
  76. tjoint.back() = world.CreateJoint(&myDistJointDef);
  77. // Joint 3
  78. myDistJointDef.bodyA = tbody[ tbody.size() - 3];
  79. myDistJointDef.bodyB = tbody[ tbody.size() - 2];
  80. posA = myDistJointDef.bodyA->GetPosition();
  81. posB = myDistJointDef.bodyB->GetPosition();
  82. myDistJointDef.length = ( posA - posB ).Length() / 2.0f;
  83. tjoint.push_back( 0x0 );
  84. tjoint.back() = world.CreateJoint(&myDistJointDef);
  85. }