Message
Message
hpp>
#include <Box2D/Box2D.h>
#include <Box2D/b2_time_of_impact.h>
#include <iostream>
int main() {
// Configuración de SFML
sf::RenderWindow window(sf::VideoMode(800, 600), "Bala");
window.setFramerateLimit(60);
// Configuración de Box2D
b2Vec2 gravity(0.0f, -10.0f);
b2World world(gravity);
b2PolygonShape groundBox;
groundBox.SetAsBox(50.0f, 1.0f);
b2FixtureDef groundFixtureDef;
groundFixtureDef.shape = &groundBox;
groundFixtureDef.density = 0.0f;
groundBody->CreateFixture(&groundFixtureDef);
// Crear el proyectil
b2BodyDef projectileBodyDef;
projectileBodyDef.type = b2_dynamicBody;
projectileBodyDef.position.Set(0.0f, 10.0f);
b2Body* projectileBody = world.CreateBody(&projectileBodyDef);
b2PolygonShape projectileBox;
projectileBox.SetAsBox(1.0f, 1.0f);
b2FixtureDef projectileFixtureDef;
projectileFixtureDef.shape = &projectileBox;
projectileFixtureDef.density = 1.0f;
projectileFixtureDef.friction = 0.3f;
projectileBody->CreateFixture(&projectileFixtureDef);
b2DistanceOutput distOutput;
b2SimplexCache cache;
// reset cache
cache.count = 0;
b2Sweep sweepA;
sweepA.c0 = projectileBody->GetPosition();
sweepA.a0 = projectileBody->GetAngle();
sweepA.c = projectileBody->GetWorldCenter() + projectileBody-
>GetLinearVelocity();
sweepA.a = sweepA.a0;
sweepA.localCenter.SetZero();
b2Sweep sweepB;
sweepB.c0 = groundBody->GetPosition();
sweepB.a0 = groundBody->GetAngle();
sweepB.c = groundBody->GetWorldCenter() + groundBody->GetLinearVelocity();
sweepB.a = sweepB.a0;
sweepB.localCenter.SetZero();
b2TOIInput toiInput;
toiInput.proxyA.Set(&projectileBox, 0);
toiInput.proxyB.Set(&groundBox, 0);
toiInput.sweepA = sweepA;
toiInput.sweepB = sweepB;
toiInput.tMax = tMax;
b2TOIOutput toiOutput{};
b2TimeOfImpact(&toiOutput, &toiInput);
// Limpiar la ventana
window.clear(sf::Color::White);
// Mostrar la ventana
window.display();
}
return 0;
}