Jump to content

  • Log In with Google      Sign In   
  • Create Account


Hello Box2D not working. What am I doing wrong?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 jonbonazza   Members   -  Reputation: 121

Like
0Likes
Like

Posted 11 March 2012 - 08:09 PM

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?
Co-founder/Lead Programmer
Bonafide Software, L.L.C.
Fairmont, WV 26554 US

Sponsor:

#2 bollµ   Members   -  Reputation: 354

Like
0Likes
Like

Posted 12 March 2012 - 01:06 AM

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 :]

a WIP 2d game engine: https://code.google.com/p/modulusengine/

English is not my first language, so do feel free to correct me :)


#3 jonbonazza   Members   -  Reputation: 121

Like
0Likes
Like

Posted 12 March 2012 - 10:10 AM

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.
Co-founder/Lead Programmer
Bonafide Software, L.L.C.
Fairmont, WV 26554 US

#4 jonbonazza   Members   -  Reputation: 121

Like
0Likes
Like

Posted 12 March 2012 - 12:33 PM

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.
Co-founder/Lead Programmer
Bonafide Software, L.L.C.
Fairmont, WV 26554 US

#5 bollµ   Members   -  Reputation: 354

Like
0Likes
Like

Posted 12 March 2012 - 10:32 PM

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 Posted Image

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 Posted Image

a WIP 2d game engine: https://code.google.com/p/modulusengine/

English is not my first language, so do feel free to correct me :)





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS