【发布时间】:2014-03-06 12:37:37
【问题描述】:
我试图让 box2d 模拟以类似于库中包含的 helloWorld 示例的方式将 x 和 y 浮点数打印到屏幕上。我已经设法建立和链接到图书馆。
我有一个类定义一个球,它应该从屏幕上的一个点落下然后落下。但是当我尝试获得速度时,我无法访问成员数据。
objects.h 内容
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(){}
};
实例化 - Ball 现在应该在模拟中
Ball* ball = new Ball(&world, 1);
balls.push_back(ball);
尝试打印身体的位置和角度。
b2Vec2 position = m_body->GetPosition();
float32 angle = m_body->GetAngle();
printf("%4.2f %4.2f %4.2f\n", position.x, position.y, angle);
错误消息声明 m_body 未在范围内声明。这似乎很简单,如果我在世界上定义一个像 b2Body* body 这样的身体;并测试代码是否可以编译并运行,但随后会出现段错误,因为我传递了一个空引用。那么我怎样才能访问我的类实例的属性并将它们打印出来。
我尝试过使用 b2Vec2 position = Ball::m_body->GetPosition(); & b2Vec2 位置 = 球->GetPosition();但没有快乐。
【问题讨论】: