0% found this document useful (0 votes)
8 views

Message

This document contains C++ code that simulates a projectile falling under gravity using the Box2D physics engine and renders it using SFML. It creates a static ground body, a dynamic projectile body, sets the projectile's initial velocity, and calculates the time of impact between the projectile and ground using time of impact and sweep tests in Box2D. It then runs the main loop to simulate physics in Box2D each frame and update the projectile's rendered position in SFML.

Uploaded by

Lilia Zamora
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Message

This document contains C++ code that simulates a projectile falling under gravity using the Box2D physics engine and renders it using SFML. It creates a static ground body, a dynamic projectile body, sets the projectile's initial velocity, and calculates the time of impact between the projectile and ground using time of impact and sweep tests in Box2D. It then runs the main loop to simulate physics in Box2D each frame and update the projectile's rendered position in SFML.

Uploaded by

Lilia Zamora
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <SFML/Graphics.

hpp>
#include <Box2D/Box2D.h>
#include <Box2D/b2_time_of_impact.h>
#include <iostream>

// Factor de conversión de metros a píxeles (ajusta según tu escala)


const float METERS_TO_PIXELS = 50.0f;

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);

// Crear un suelo estático


b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0.0f, 0.0f);
b2Body* groundBody = world.CreateBody(&groundBodyDef);

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);

// Inicializar la ventana de SFML


sf::CircleShape projectileShape(10.0f);
projectileShape.setFillColor(sf::Color::Red);

// Setear la velocidad inicial del proyectil


projectileBody->SetLinearVelocity(b2Vec2(10.0f, 0.0f));

// Calcular el tiempo de impacto con el suelo usando el método de barrido


(Sweep)
b2DistanceInput distInput;
distInput.proxyA.Set(&projectileBox, 0);
distInput.proxyB.Set(&groundBox, 0);
distInput.transformA = projectileBody->GetTransform();
distInput.transformB = groundBody->GetTransform();
distInput.useRadii = true;

b2DistanceOutput distOutput;
b2SimplexCache cache;

// reset cache
cache.count = 0;

// calculate the distance


b2Distance(&distOutput, &cache, &distInput);

// print the calculated distance


printf("Distance between bodies: %4.2f m\n", distOutput.distance);

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();

float tMax = 10.0f;

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);

float timeOfImpact = toiOutput.t;


printf("Time of impact: %.2f s\n", timeOfImpact);

// Bucle principal de SFML


while (window.isOpen()) {
// Manejar eventos de SFML
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}

// Simular el mundo Box2D (avanzar un paso de tiempo)


world.Step(1.0f / 60.0f, 6, 2);

// Actualizar la posición y la apariencia del proyectil en SFML


b2Vec2 position = projectileBody->GetPosition();
projectileShape.setPosition(position.x * METERS_TO_PIXELS,
window.getSize().y - position.y * METERS_TO_PIXELS);

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

// Dibujar el proyectil en la ventana de SFML


window.draw(projectileShape);

// Mostrar la ventana
window.display();
}

return 0;
}

You might also like