Box2d body's velocity slows down without any force applied

36 views Asked by At

I'm working on a SFML and Box2d project and I've stumbled with a problem related to the linear velocity of bodies, I've created a simplified version of the project and the problem still persists.

This is the code:

int main()
{    
    sf::RenderWindow window(sf::VideoMode(800.f, 800.f), "SPACE BATTLE");

    sf::Event evnt;

    sf::Clock clock;
    
    b2BodyDef bodyDefinition;
    bodyDefinition.type = b2_dynamicBody;
    b2Body* body = world.CreateBody(&bodyDefinition);

    b2PolygonShape bodyShape;
    bodyShape.SetAsBox(2.f, 2.f);

    b2FixtureDef bodyFixture;
    bodyFixture.shape = &bodyShape;
    bodyFixture.density = 1;
    bodyFixture.friction = 0.3;

    body->CreateFixture(&bodyFixture);


    body->SetLinearVelocity(b2Vec2(0.f, 5000.f));

    window.setFramerateLimit(60);

    while (window.isOpen())
    {
        while (window.pollEvent(evnt))
        {

            if (evnt.type == sf::Event::Closed)
            {
                window.close();
            }
        }

        auto time = clock.getElapsedTime();

        world.Step(time.asSeconds(), 6, 2);

        clock.restart();
    }
}

After the first call to world.Step(), if I try to get the linear velocity of the body, it will be totally different than what I set before body->SetLinearVelocity(b2Vec2(0.f, 5000.f));, for example, sometimes I get 522.23f. These values can vary but they are always different from what I set the linear velocity to before the main loop starts. Why is this happening?

0

There are 0 answers