我有一个动态主体与一个动态主体碰撞,并显示了一个简单的联系人侦听器类,如下所示:
void myContactListener::BeginContact(b2Contact* contact)
{
void* bodyUserData = contact->GetFixtureA()->GetBody()->GetUserData();
if (bodyUserData)
{
static_cast<EnemyEntity*>(bodyUserData)->startContact();
}
bodyUserData = contact->GetFixtureB()->GetBody()->GetUserData();
if (bodyUserData)
{
static_cast<EnemyEntity*>(bodyUserData)->endContact();
}
}
void myContactListener::EndContact(b2Contact* contact)
{
void* bodyUserData = contact->GetFixtureA()->GetBody()->GetUserData();
if (bodyUserData)
static_cast<EnemyEntity*>(bodyUserData)->endContact();
bodyUserData = contact->GetFixtureB()->GetBody()->GetUserData();
if (bodyUserData)
static_cast<EnemyEntity*>(bodyUserData)->endContact();
}
每当我将两个动态物体彼此移动时,总是会在第一次检测到碰撞以及停止碰撞。但是,如果我尝试再次碰撞它们,则第一次后通常不会检测到任何碰撞。有时确实如此。我该怎么做才能解决此问题?我的代码中没有其他内容触及任何Box2D代码。
这是我的联系人侦听器头文件:
class myContactListener : public b2ContactListener
{
private:
public:
// Contact listener methods
void BeginContact(b2Contact* contact);
void EndContact(b2Contact* contact);
};
和EnemyEntity Box2D方法:
void EnemyEntity::createStarBox2DCollision(b2World *world)
{
enemyType = EnemyType::Star;
def.type = b2_dynamicBody;
def.position.Set(1000, 1000);
body = world->CreateBody(&def);
box.SetAsBox(rectShapeSize.x / 2, rectShapeSize.y / 2);
fixtureDef.shape = &box;
fixtureDef.density = 1.0;
fixtureDef.friction = 0.3;
body->CreateFixture(&fixtureDef);
body->SetUserData(this);
}
答案 0 :(得分:1)
看起来你应该有
void myContactListener::BeginContact(b2Contact* contact)
{
...
static_cast<EnemyEntity*>(bodyUserData)->startContact();
}
}
而不是
void myContactListener::BeginContact(b2Contact* contact)
{
...
static_cast<EnemyEntity*>(bodyUserData)->endContact();
}
}
对于两个实体,而不仅仅是对A