123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- // Basiques
- # include <iostream>
- # include <cmath>
- # include <vector>
- // SDL
- # include <SDL/SDL.h>
- # undef main
- # include <SDL/SDL_rotozoom.h>
- # include <SDL/SDL_gfxPrimitives.h>
- // Box2D
- # include <box2d/box2d.h>
- # define MULTI 100.0f
- // Prototypes
- # include "creater.h"
- void createBloc( b2World &world, std::vector<b2Body*> &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<b2Body*> &tbody, std::vector<b2Joint*> &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<b2Body*> &tbody, std::vector<b2Joint*> &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);
- }
|