问题描述
|
在零重力太空游戏中检测碰撞时遇到很多麻烦。希望这张图片可以帮助我解释一下:
http://i.stack.imgur.com/f7AHO.png
白色矩形是连接了b2PolygonShape固定装置的静态物体,如下所示:
// Create the line physics body definition
b2BodyDef wallBodyDef;
wallBodyDef.position.Set(0.0f,0.0f);
// Create the line physics body in the physics world
wallBodyDef.type = b2_staticBody; // Set as a static body
m_Body = world->CreateBody(&wallBodyDef);
// Create the vertex array which will be used to make the physics shape
b2Vec2 vertices[4];
vertices[0].Set(m_Point1.x,m_Point1.y); // Point 1
vertices[1].Set(m_Point1.x + (sin(angle - 90*(float)DEG_TO_RAD)*m_Thickness),m_Point1.y - (cos(angle - 90*(float)DEG_TO_RAD)*m_Thickness)); // Point 2
vertices[2].Set(m_Point2.x + (sin(angle - 90*(float)DEG_TO_RAD)*m_Thickness),m_Point2.y - (cos(angle - 90*(float)DEG_TO_RAD)*m_Thickness)); // Point 3
vertices[3].Set(m_Point2.x,m_Point2.y); // Point 3
int32 count = 4; // Vertex count
b2PolygonShape wallShape; // Create the line physics shape
wallShape.Set(vertices,count); // Set the physics shape using the vertex array above
// Define the dynamic body fixture
b2FixtureDef fixtureDef;
fixtureDef.shape = &wallShape; // Set the line shape
fixtureDef.density = 0.0f; // Set the density
fixtureDef.friction = 0.0f; // Set the friction
fixtureDef.restitution = 0.5f; // Set the restitution
// Add the shape to the body
m_Fixture = m_Body->CreateFixture(&fixtureDef);
m_Fixture->SetUserData(\"Wall\");[/code]
您必须相信我,这样才能在图像中形成形状。物理模拟效果完美,播放器(小三角形)与身体碰撞时达到了像素完美的精度。但是,当我尝试确定何时发生碰撞时,我遇到了一个问题,这样我就可以消除健康状况,而不必进行其他操作。我为此使用的代码如下:
/*------ Check for collisions ------*/
if (m_Physics->GetWorld()->GetContactCount() > 0)
{
if (m_Physics->GetWorld()->GetContactList()->GetFixtureA()->GetUserData() == \"Player\" &&
m_Physics->GetWorld()->GetContactList()->GetFixtureB()->GetUserData() == \"Wall\")
{
m_Player->CollideWall();
}
}
我知道可能有更好的方法来进行冲突,但是我只是一个初学者,而且还没有发现任何地方可以解释如何很好地进行监听器和回调,使我理解。我的问题是,只要玩家身体进入上方的紫色框,GetContactCount()就会显示联系人。显然,将创建一个包含白色矩形的矩形边界框。
我尝试过将灯具做成EdgeShape,并且发生了同样的事情。有人知道这里发生了什么吗?我真的很想撞钉,这样我就可以继续其他事情了。非常感谢您的帮助。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)