I have SFML set up with a Box2D world that has gravity, but my bodies won't move. I std::cout'ed the position of the body in the game loop, but it's always at (0, 14)!

No errors or warnings, just no movement when there's supposed to be.

I'm quite new to Box2D, so I'm unsure if I'm doing this correctly. What mistake am I making?


#include <SFML\Graphics.hpp>
#include <Box2D\Box2D.h>
#include <iostream>
#include <vector>

#include "Box.h"

sf::RenderWindow window;

int main(int argc, char* argv[])
    // Initialize stuff here since the things here will only run on startup
    b2Vec2 gravity(0, 9.8f);
    b2World world = b2World(gravity, true);

    // Initialize entities
    b2BodyDef groundBodyDef;
    groundBodyDef.position.Set(0.0f, -10.0f);
    b2Body* groundBody = world.CreateBody(&groundBodyDef);

    b2PolygonShape groundBox;
    groundBox.SetAsBox(50.0f, 10.0f);
    groundBody->CreateFixture(&groundBox, 0.0f);

    Box box;
    box.init(&world, sf::Vector2f(0.0f, 14.0f), sf::Vector2f(15.0f, 15.0f), sf::Color::White);

    sf::RenderWindow window(sf::VideoMode(1200, 840), "Title");

    while (window.isOpen())

        sf::Event e;
        while (window.pollEvent(e))

            if (e.type == sf::Event::Closed)


        // Drawing

        world.Step(1 / 60, 8, 3);


        box.getSfShape().setPosition(sf::Vector2f(box.getBody()->GetPosition().x / 30.0f, box.getBody()->GetPosition().y / 30.0f));


        //std::cout << "X : " << box.getSfShape().getPosition().x << " Y : " << box.getSfShape().getPosition().y << std::endl;
        std::cout << "X : " << box.getBody()->GetPosition().x << " Y : " << box.getBody()->GetPosition().y << std::endl;





#pragma once

#include <Box2D\Box2D.h>
#include <SFML\Graphics.hpp>

class Box


    void init(b2World* world, sf::Vector2f position, sf::Vector2f dimensions, sf::Color color);

    b2Body* getBody() { return body; }
    b2Fixture* getFixture() { return fixture; }

    sf::RectangleShape getSfShape() { return sfmlShape; }

    b2Body* body = nullptr;
    b2Fixture* fixture = nullptr;

    sf::RectangleShape sfmlShape;



#include "Box.h"





void Box::init(b2World * world, sf::Vector2f position, sf::Vector2f dimensions, sf::Color color)
    // Creates a body definition
    b2BodyDef bodyDef;
    bodyDef.type = b2_dynamicBody;
    bodyDef.position.Set(position.x, position.y);

    // Create the body into the world
    body = world->CreateBody(&bodyDef);

    // Create the box for the body
    b2PolygonShape boxShape;
    boxShape.SetAsBox(dimensions.x / 2.0f, dimensions.y / 2.0f);

    // Create a fixture for the body
    b2FixtureDef fixtureDef;
    fixtureDef.shape = &boxShape;
    fixtureDef.density = 1.0f;
    fixtureDef.friction = 0.3f;
    fixture = body->CreateFixture(&fixtureDef);

    sfmlShape.setPosition(body->GetPosition().x / 30.0f, body->GetPosition().y / 30.0f);

1 Answer 1


1/60 in your world.Step call is an integer division, which truncates toward zero. The world is being told to step by 0 seconds every iteration of the loop, hence nothing moves.

Write 1.0f / 60.0f instead.

  • \$\begingroup\$ Ohhhh, thank you! Body is moving now! :) Though the sfmlShape is not. :/ \$\endgroup\$
    – BiiX
    Dec 13, 2015 at 19:42
  • \$\begingroup\$ I'm not sure if I should create a new post with my sfml shape not moving. Kinda new here, should I create a new post or just ask on this post? \$\endgroup\$
    – BiiX
    Dec 13, 2015 at 21:05
  • 1
    \$\begingroup\$ @BiiX No problem, happy to help. :) Each separate issue should go in its own question, so I'd say ask a new one. Additional tips: With debugging questions, try to keep your example as minimal as possible and show your debugging efforts so far (printouts, stuff you've ruled out, etc). (Just in case your other problem is harder to "just see" based on the code.) \$\endgroup\$
    – Anko
    Dec 13, 2015 at 21:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .