Hello Box2D not working. What am I doing wrong?

This topic is 2773 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Ok, so I set out to add box2d to my project, but I couldn't for the life of me get it to work. After hours of frustration, I decided to try to run the hello box2d from the documentation, but when I do, the body's position doesn't change, even though gravity should be taking it's tole.

my code for the hello Box2d looks like this:

int main() { b2World world(b2Vec2(0.0, -9.8)); b2BodyDef bodyDef; bodyDef.position.Set(3.0, 3.0); bodyDef.angle = 0.0f; bodyDef.type = b2_dynamicBody; b2PolygonShape shape; shape.SetAsBox(10, 10); b2FixtureDef fixtureDef; fixtureDef.shape = &shape; fixtureDef.density = 1.0f; fixtureDef.friction = 0.3f; body = world.CreateBody(&bodyDef); body->CreateFixture(&fixtureDef); while(true) { world.Step(1/60, 10, 8); std::cout << body->GetPosition().y << std::endl; //world.ClearForces(); } }

What am I doing wrong?

Share on other sites
you're not calling world->Step, which allows box2d to simulate the world.

so, call world->Step(stepTime , 20, 20);
step time depends on your target FPS, if your FPS is 60, step it by (1.0 /60.0)

Tl;DR; call world->Step(1.0 /60.0 , 20, 20) within the while loop
Hope that helps :]

Share on other sites
I actually Am calling world->Step() inside of the while loop at the bottom. With that said, I found the problem, which was very stupid. when I called world->Step(), I passed 1/60 as the first parameter and since 1 and 60 are both integer literals, the result of the division is also an integer, which in this case would be 0. After changing that to 1.0f/60.0f, It worked.

After I got that working, I went on to make the same corrections in my project, but it's still not working. I even went as far as to temorarily change the b2FixtureDef and b2BodyDef settings to the same ones I used in the working example, and to no avail. I would provide some code, but everything is spread across so many different files that I don't think you guys would want to mess with it. I'll continue to try to debug and keep you all updated with my findings. If I really can't figure it out after a few days, I'll post the code and hope for a response as a last resort.

Share on other sites
Hmm... I seemed to have figured it out, though not really sure why it didn't work before. What I did to fix it was move the world initialization out of the scene manager's ( I know, I know. The name is bad, but w/e) constructor and into the Start() method which is called at the beginning of a scene.

I do have one more question, however. To be sure I am doing things right, Box2D considers the center of the window as the origin, correct? So, if I had an 800x600 window, then the origin of the physics world in pixels would be (400, 300)? I know that Box2D works in meters and not pixels, but I want to be sure I have the origin correct for the conversions.

Share on other sites
you can consider the origin to be whatever you want. I, for example, consider (0,0) to be bottom left.It's just a matter of how you handle the coordinate system, and how your Renderer draws it. If you want the oject to be at the center, then, while drawing, you'll probably say (center + pos). To consider (0,0) as the bottom left position, just use Pos.

I personally prefer the bottom-left coordinate system, because it makes things easy

And also, it's a good idea to keep some conversion constants : Meters2Pixels and Pixels2Metres to adapt to different resolutions and to have finer control over the simulation.
Cheers!
PS- PM me if you have some doubts or something

• Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 11
• 23
• 36
• 16
• 75