Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: SFML and Box2D cannot move a body  (Read 309 times)

0 Members and 1 Guest are viewing this topic.

BreadMonika

  • Newbie
  • *
  • Posts: 5
    • View Profile
SFML and Box2D cannot move a body
« on: October 25, 2023, 05:30:31 am »
Hi,
I'm working with SFML and Box2D for physics, the problem is that I'm not able to move the body that I created, I'm using the function ApplyLinearImpulse in order to move the body, but it does not seem to be working, I would greatly appreciate some help with this. Here is my code:

namespace sfdd
{
    const float SCALE = 32.f;
}
int main(int argc, char** argv)
{
    //Setup SFML
        sf::RenderWindow window(sf::VideoMode(1024, 768, 32), "Box2D - SFML Debug Draw Test");
        window.setFramerateLimit(60);

        b2World world(b2Vec2(0.f, 10.f));
        world.SetAllowSleeping(true);

        /* Initialize SFML Debug Draw */
        SFMLDebugDraw debugDraw(window);

        world.SetDebugDraw(&debugDraw);

        /* Set initial flags for what to draw */
        debugDraw.SetFlags(b2Draw::e_shapeBit); //Only draw shapes

        /* constants for time step and physics accuracy */
        const int velocityIterations = 6;
        const int positionIterations = 2;

        /* Create the bounding box */
        b2BodyDef boundingBoxDef;
        boundingBoxDef.type = b2_staticBody;
        float xPos = (window.getSize().x / 2.f) / sfdd::SCALE;
        float yPos = 0.5f;
        boundingBoxDef.position.Set(xPos, yPos);

        b2Body* boundingBoxBody = world.CreateBody(&boundingBoxDef);

        b2PolygonShape boxShape;
        boxShape.SetAsBox((window.getSize().x) / sfdd::SCALE, 0.5f, b2Vec2(0.f, 0.f), 0.f);
        boundingBoxBody->CreateFixture(&boxShape, 1.0); //Top

        yPos = (window.getSize().y) / sfdd::SCALE - 1.f;
        boxShape.SetAsBox((window.getSize().x) / sfdd::SCALE, 0.5f, b2Vec2(0.f, yPos), 0.f);
        boundingBoxBody->CreateFixture(&boxShape, 1.f); //Bottom

        xPos -= 0.5f;
        boxShape.SetAsBox(0.5f, (window.getSize().y) / sfdd::SCALE, b2Vec2(-xPos, 0.f), 0.f);
        boundingBoxBody->CreateFixture(&boxShape, 1.f);//Left

        boxShape.SetAsBox(0.5f, (window.getSize().y) / sfdd::SCALE, b2Vec2(xPos, 0.f), 0.f);
        boundingBoxBody->CreateFixture(&boxShape, 1.f);//Right
       
        b2BodyDef bodyDef;
        bodyDef.type = b2_dynamicBody;
        bodyDef.position.Set(20, 10);
        bodyDef.fixedRotation = true;
        b2Body* ground = world.CreateBody(&bodyDef); //This is not the body of the bounding box
        b2PolygonShape ps;
        ps.SetAsBox(5, 5);
        ground->CreateFixture(&ps, 1.f);

        sf::Clock deltaClock; //Clock used to measure FPS
        sf::Time deltaTime;
    // This is our little game loop.
    while (window.isOpen())
    {
        // Check if it's time to go
        sf::Event event;

        while (window.pollEvent(event))
        {
                        float force;
                        b2Vec2 currentVel = ground->GetLinearVelocity();

                        if (event.type == sf::Event::Closed)
                        {
                                window.close();
                        }
                        else if (event.type == sf::Event::KeyPressed)
                        {
                                if (event.key.code == sf::Keyboard::Left)
                                {
                                        if (currentVel.x > -8.0) {
                                                force = -4.f * deltaTime.asSeconds(); // incremental acceleration to maximum speed
                                                std::cout << currentVel.x << ", " << force << ", " << deltaTime.asSeconds() << std::endl;
                                                ground->ApplyLinearImpulse(b2Vec2(force, 0.f), ground->GetLocalCenter(), true);
                                        }
                                }
                        }
        }
        window.clear();

        world.Step(deltaTime.asSeconds(), velocityIterations, positionIterations);

        world.DebugDraw();

        window.display();
        deltaTime = deltaClock.restart();

    }

    return 0;
}
 

kojack

  • Sr. Member
  • ****
  • Posts: 317
  • C++/C# game dev teacher.
    • View Profile
Re: SFML and Box2D cannot move a body
« Reply #1 on: October 29, 2023, 10:07:25 am »
Have you tried making the force really large? -4.f * deltaTime.asSeconds() at 60Hz is going to be a small number, and using that for a one off impulse on a key pressed event may not be enough to overcome friction.