c++ - Accessing property of class instance -
im trying box2d simulation printing out x & y floats screen in similar fashion helloworld example included library. i've managed both build , link library.
i've got class defining ball should drop point on screen fall. when try velocity can't access 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 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( ); m_body->setgravityscale(5);//cancel gravity (use -1 reverse gravity, etc) } ~ball(){} };
instantiation - ball should in simulation
ball* ball = new ball(&world, 1); balls.push_back(ball);
attempt print position , angle of 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 m_body not declared in scope. seems plain enough, if define body in world b2body* body; , test against code compiles , runs, segfaults because i've passed empty reference. how can access properties of class instance , print them out.
i've tried using b2vec2 position = ball::m_body->getposition(); & b2vec2 position = balls->getposition(); no joy.
m_body member of ball class , trying access without using ball object. need following gain access
ball->m_body->getposition();
or access ball stored in vector(assuming using c++11)
for(auto& b : balls) { (*b).m_body->getposition(); }
or
for(int = 0; < balls.size(); ++i) { ball* b = balls[i]; b->m_body()->getposition(); }
ideally should not using raw pointers , should instead do
ball ball(&world, 1) ball.m-body->getposition();
or @ least smart pointers(unique_ptr) etc.
Comments
Post a Comment