// Basiques # include # include # include // SDL # include # undef main # include # include // Box2D # include # define MULTI 100.0f // Prototypes # include "creater.h" void createBloc( b2World &world, std::vector &tbody, SDL_Surface* blc, float x, float y ) { // Définition b2BodyDef bodyDef; bodyDef.type = b2_dynamicBody; bodyDef.userData.pointer = (uintptr_t) blc; bodyDef.position.Set( x, y ); // Shape b2PolygonShape dynamicBox; dynamicBox.SetAsBox( (float)blc->w * 2.0f / MULTI / 2 , (float)blc->h * 2.0f / MULTI / 2 ); // Fixture b2FixtureDef fixtureDef; fixtureDef.shape = &dynamicBox; fixtureDef.density = 1.0f; fixtureDef.friction = 0.3f; // Push tbody.push_back( 0x0 ); tbody.back() = world.CreateBody(&bodyDef); tbody.back()->CreateFixture(&fixtureDef); } void link2ByDist( b2World &world, std::vector &tbody, std::vector &tjoint ) { if ( tbody.size() < 2 ) return ; b2Vec2 posA, posB; b2DistanceJointDef myDistJointDef; myDistJointDef.collideConnected = true ; myDistJointDef.bodyA = tbody[ tbody.size() - 2]; myDistJointDef.bodyB = tbody[ tbody.size() - 1]; posA = myDistJointDef.bodyA->GetPosition(); posB = myDistJointDef.bodyB->GetPosition(); myDistJointDef.length = ( posA - posB ).Length() / 2.0f; myDistJointDef.stiffness = 4.0f ; myDistJointDef.damping = 0.5f ; tjoint.push_back( 0x0 ); tjoint.back() = world.CreateJoint(&myDistJointDef); } void link3ByDist( b2World &world, std::vector &tbody, std::vector &tjoint ) { if ( tbody.size() < 3 ) return ; b2Vec2 posA, posB; b2DistanceJointDef myDistJointDef; myDistJointDef.collideConnected = true ; myDistJointDef.stiffness = 4.0f ; myDistJointDef.damping = 0.5f ; // Joint 1 myDistJointDef.bodyA = tbody[ tbody.size() - 2]; myDistJointDef.bodyB = tbody[ tbody.size() - 1]; posA = myDistJointDef.bodyA->GetPosition(); posB = myDistJointDef.bodyB->GetPosition(); myDistJointDef.length = ( posA - posB ).Length() / 2.0f; tjoint.push_back( 0x0 ); tjoint.back() = world.CreateJoint(&myDistJointDef); // Joint 2 myDistJointDef.bodyA = tbody[ tbody.size() - 3]; myDistJointDef.bodyB = tbody[ tbody.size() - 1]; posA = myDistJointDef.bodyA->GetPosition(); posB = myDistJointDef.bodyB->GetPosition(); myDistJointDef.length = ( posA - posB ).Length() / 2.0f; tjoint.push_back( 0x0 ); tjoint.back() = world.CreateJoint(&myDistJointDef); // Joint 3 myDistJointDef.bodyA = tbody[ tbody.size() - 3]; myDistJointDef.bodyB = tbody[ tbody.size() - 2]; posA = myDistJointDef.bodyA->GetPosition(); posB = myDistJointDef.bodyB->GetPosition(); myDistJointDef.length = ( posA - posB ).Length() / 2.0f; tjoint.push_back( 0x0 ); tjoint.back() = world.CreateJoint(&myDistJointDef); }