Im trying to get a box2d simulation printing out x & y floats to the screen in a similar fashion to the helloWorld example included with the library. I've managed to both build and link to the library.
I've got a class defining a ball that should drop from a point on the screen then fall. But when I try to get the velocity I can't access the members data.
objects.h contents
class Ball {
public:
bool m_contacting;
b2Body* m_body;
float m_radius;
public:
// Ball class constructor
Ball(b2World* m_world, float radius) {
m_contacting = false;
m_body = NULL;
m_radius = radius;
//set up dynamic body, store in class variable
b2BodyDef myBodyDef;
myBodyDef.type = b2_dynamicBody;
myBodyDef.position.Set(0, 20);
m_body = m_world->CreateBody(&myBodyDef);
//add circle fixture
b2CircleShape circleShape;
circleShape.m_p.Set(0, 0);
circleShape.m_radius = m_radius; //use class variable
b2FixtureDef myFixtureDef;
myFixtureDef.shape = &circleShape;
myFixtureDef.density = 1;
myFixtureDef.restitution = 0.83f;
m_body->CreateFixture(&myFixtureDef);
m_body->SetUserData( this );
m_body->SetGravityScale(5);//cancel gravity (use -1 to reverse gravity, etc)
}
~Ball(){}
};
Instantiation - Ball should now be in simulation
Ball* ball = new Ball(&world, 1);
balls.push_back(ball);
Attempt to print the position and angle of the body.
b2Vec2 position = m_body->GetPosition();
float32 angle = m_body->GetAngle();
printf("%4.2f %4.2f %4.2f\n", position.x, position.y, angle);
The error message declares that m_body is not declared in the scope. Which seems plain enough, if I define a body in the world like b2Body* body; and test against that the code compiles and runs, but then segfaults because I've passed an empty reference. So how can I get access to the properties of my class instance and print them out.
I've tried using b2Vec2 position = Ball::m_body->GetPosition(); & b2Vec2 position = balls->GetPosition(); but no joy.