I feel like I'm asking a question that must have already been asked 327 times or so, but I couldn't find the answers. I started out trying to integrate Box2D into my existing game, but when I ran into this problem I toned it down to a simple program based on the "Hello Box2D" tutorial found in the manual. I quickly discovered that the problem was still there. Simply put, dynamic bodies don't seem to be effected by gravity or any other forces at all.
My assumption is always that I'm missing something, so if any of you are willing to take the time to go through this short program that would be awesome.
The output is sixty lines reading "0,4". I compiled my Box2D version(2.2.0) myself with gcc version 4.6.1. Any insight would be much appreciated.
My assumption is always that I'm missing something, so if any of you are willing to take the time to go through this short program that would be awesome.
Code:
#include <iostream>
#include <Box2D/Box2D.h>
using namespace std;
int main()
{
b2World world(b2Vec2(0.0f,-10.0f), true);
b2BodyDef groundDef;
groundDef.position.Set(0.0f,-10.0f);
b2Body * ground = world.CreateBody(&groundDef);
b2PolygonShape groundShape;
groundShape.SetAsBox(50.0f,10.0f);
ground->CreateFixture(&groundShape, 0.0f);
b2BodyDef boxDef;
boxDef.type = b2_dynamicBody;
boxDef.position.Set(0.0f,4.0f);
b2Body * box = world.CreateBody(&boxDef);
b2PolygonShape boxShape;
boxShape.SetAsBox(1.0f,1.0f);
b2FixtureDef fixDef;
fixDef.shape = &boxShape;
fixDef.density = 1.0f;
fixDef.friction = 0.3f;
box->CreateFixture(&fixDef);
for(int i = 0; i < 60; ++i)
{
world.Step(1/60, 6, 2);
cout << box->GetPosition().x << ',' << box->GetPosition().y << '\n';
}
return 0;
}
#include <Box2D/Box2D.h>
using namespace std;
int main()
{
b2World world(b2Vec2(0.0f,-10.0f), true);
b2BodyDef groundDef;
groundDef.position.Set(0.0f,-10.0f);
b2Body * ground = world.CreateBody(&groundDef);
b2PolygonShape groundShape;
groundShape.SetAsBox(50.0f,10.0f);
ground->CreateFixture(&groundShape, 0.0f);
b2BodyDef boxDef;
boxDef.type = b2_dynamicBody;
boxDef.position.Set(0.0f,4.0f);
b2Body * box = world.CreateBody(&boxDef);
b2PolygonShape boxShape;
boxShape.SetAsBox(1.0f,1.0f);
b2FixtureDef fixDef;
fixDef.shape = &boxShape;
fixDef.density = 1.0f;
fixDef.friction = 0.3f;
box->CreateFixture(&fixDef);
for(int i = 0; i < 60; ++i)
{
world.Step(1/60, 6, 2);
cout << box->GetPosition().x << ',' << box->GetPosition().y << '\n';
}
return 0;
}
The output is sixty lines reading "0,4". I compiled my Box2D version(2.2.0) myself with gcc version 4.6.1. Any insight would be much appreciated.