Dear :
我用box2d创建多边形时,有多个fixture、PolygonShape。
在更新物理世界时,需要获取该body的position,但是取到的却是(0,0),而不是屏幕位置。
请问正确获取位置的姿势是什么?
// 创建多边形:
b2Body* createPoly(
b2World* world,
const std::vector<std::vector<Point> >& trian, // 坐标是像素
void *userData,
bool isStatic
)
{
b2BodyDef bodyDef;
bodyDef.type = isStatic ? b2_staticBody : b2_dynamicBody;
bodyDef.position.Set(0, 0);
b2Body* body = world->CreateBody(&bodyDef);
if (userData) body->SetUserData(userData);
b2FixtureDef fixtureDef;
fixtureDef.density = 3.f;
fixtureDef.restitution = 0.2f;
fixtureDef.friction = 0.3f;
for (size_t i = 0; i < trian.size(); ++i)
{
const std::vector<Point> &v = trian;
b2PolygonShape pshape;
b2Vec2* ar = new b2Vec2;
int count = (int)points2VecArray(v, ar); // 这里做了像素和米的转换
pshape.Set(ar, count);
fixtureDef.shape = &pshape;
body->CreateFixture(&fixtureDef);
delete] ar;
}
return body;
}
// 更新物理世界:
for (auto b = m_world->GetBodyList(); false && b; b = b->GetNext())
{
if (b->GetType() == b2_dynamicBody)
{
if (b->GetUserData())
{
auto s = (Node*)b->GetUserData();
s->setPosition(b->GetPosition().x * PTM_RATIO, // 做自由落体时 x都是0?
b->GetPosition().y * PTM_RATIO);
s->setRotation(b->GetAngle());
}
}
}