#include <SFML\Graphics.hpp>
#include <Box2D\Box2D.h>
#define RAD 0.0174532925199432957f
int main()
{
//Creo mi ventana
sf::RenderWindow window(sf::VideoMode(800, 600), "Dynamic 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
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set( 100.f , 200.f );
bodyDef.angle = 0;
b2Body *body = world.CreateBody( &bodyDef );
//esto cambia la posicion y el angulo del body
body->SetTransform(b2Vec2(200, 300), 1);
//Cambiamos la velocidad lineal y la velocidad del angulo
body->SetLinearVelocity( b2Vec2( -5 , -5 ) );
body->SetAngularVelocity( -90 * RAD );
//se crea un box shape
b2PolygonShape dynamicBox;
dynamicBox.SetAsBox( 1.f , 1.f );
//Creamos un fixture para darle mas dinamismo
b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicBox;
fixtureDef.density = 1.f;
//Creamos la fixture
body->CreateFixture(&fixtureDef);
//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(body->GetPosition().x, body->GetPosition().y);
window.draw( sprite );
//mostrar en pantalla
window.display();
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
}
return 0;
}
Herramientas Para el Desarrollo de Videojuegos y Lenguajes de Programación en Español
miércoles, 25 de marzo de 2015
Estableciendo propiedades a body
Suscribirse a:
Enviar comentarios (Atom)
No hay comentarios:
Publicar un comentario