miércoles, 25 de marzo de 2015

KinematicBody en Box2D


#include <SFML\Graphics.hpp>
#include <Box2D\Box2D.h>

#define RAD 0.0174532925199432957f

int main()
{
 //Creo mi ventana
 sf::RenderWindow window(sf::VideoMode(800, 600), "Kinematic Body");
 window.setFramerateLimit( 60 );
 
 //para empezar la simulacion del mundo con esto simula la fisica
 float32 timeStep = 1.0f / 60.f;
 int32 velocityIterations = 8;
 int32 positionIterations = 3;

 //defino mi vector de gravedad
 b2Vec2 gravity( 0.f , 10.f );

 //creo mi mundo
 b2World world( gravity );

 //definimos un bodydef para dynamicbody
 b2BodyDef bodyDefDynamic;
 bodyDefDynamic.type = b2_dynamicBody;
 bodyDefDynamic.position.Set(150.f, 50.f);
 bodyDefDynamic.angle = 0;

 //definimos un bodydef para staticbody
 b2BodyDef bodyDefStatic;
 bodyDefStatic.type = b2_staticBody;
 bodyDefStatic.position.Set(100.f, 450.f);
 bodyDefStatic.angle = 0;

 //definimos un bodydef para kinematicbody
 b2BodyDef bodyDefKinematic;
 bodyDefKinematic.type = b2_kinematicBody;
 bodyDefKinematic.position.Set(150.f, 250.f);
 bodyDefKinematic.angle = 1;

 //creamos los body
 b2Body *dynamicBody = world.CreateBody(&bodyDefDynamic);
 b2Body *staticBody = world.CreateBody(&bodyDefStatic);
 b2Body *kinematicBody = world.CreateBody(&bodyDefKinematic);

 //se crea un box shape para dynamicBox
 b2PolygonShape dynamicBox;
 dynamicBox.SetAsBox(1.f, 1.f);

 //se crea un box shape para staticBox
 b2PolygonShape staticBox;
 staticBox.SetAsBox(1.f, 1.f);

 //se crea un box shape para kinematicBox
 b2PolygonShape kinematicBox;
 kinematicBox.SetAsBox(1.f, 1.f);

 //Creamos un fixture para darle mas dinamismo
 b2FixtureDef fixtureDefDynamic;
 fixtureDefDynamic.shape = &dynamicBox;
 fixtureDefDynamic.density = 1.f;

 b2FixtureDef fixtureDefStatic;
 fixtureDefStatic.shape = &staticBox;
 fixtureDefStatic.density = 1.f;

 b2FixtureDef fixtureDefKinematic;
 fixtureDefKinematic.shape = &kinematicBox;
 fixtureDefKinematic.density = 1.f;

 //Creamos la fixture
 dynamicBody->CreateFixture(&fixtureDefDynamic);

 staticBody->CreateFixture(&fixtureDefStatic);

 kinematicBody->CreateFixture(&fixtureDefKinematic);

 kinematicBody->SetLinearVelocity(b2Vec2(0, 10 )); 
 kinematicBody->SetAngularVelocity(360 * RAD);

 //Creo mi textura
 sf::Texture textura;
 textura.loadFromFile("box1.png");
 
 //creo mi gameloop
 while (window.isOpen())
 { 

  //lo inicio en el gameloop.
  world.Step(timeStep, velocityIterations, positionIterations);

  //limpio la ventana
  window.clear(sf::Color::White);

  //Creo mi Sprite
  sf::Sprite sprite;
  sprite.setTexture( textura );
  sprite.setOrigin(16.f, 16.f);
  sprite.setPosition(dynamicBody->GetPosition().x, dynamicBody->GetPosition().y);
  window.draw( sprite );

  sf::Sprite sprite1;
  sprite1.setTexture(textura);
  sprite1.setOrigin(16.f, 16.f);
  sprite1.setPosition(staticBody->GetPosition().x, staticBody->GetPosition().y);
  window.draw(sprite1);

  sf::Sprite sprite2;
  sprite2.setTexture(textura);
  sprite2.setOrigin(16.f, 16.f);
  sprite2.setPosition(kinematicBody->GetPosition().x, kinematicBody->GetPosition().y);
  window.draw(sprite2);

  //mostrar en pantalla
  window.display();

  sf::Event event;

  while (window.pollEvent(event))
  {
   if (event.type == sf::Event::Closed)
    window.close();
  }
 }

 return 0;
} 
 
 

No hay comentarios:

Publicar un comentario