我正在开发一个简单的 2D 游戏,但在碰撞检测和响应方面遇到了问题。我正在使用改编自这篇文章的代码:http ://www.gamedev.net/page/resources/_/technical/game-programming/swept-aabb-collision-detection-and-response-r3084
我已经尝试对代码进行许多更改,但我似乎无法让它按预期运行。我的算法试图检测单个移动实体和一个或多个静态块之间的碰撞。它使用以下逻辑:
1. 宽相碰撞检测:
创建一个封装实体运动的 AABB,然后检测与静态块的 AABB 的重叠。将这些碰撞事件添加到列表中,并按影响时间对该列表进行排序。
2.窄相碰撞检测:
计算每个碰撞事件的碰撞时间和碰撞法线,并将实体移动到每个静态块之外的适当位置。使用碰撞法线将实体滑动到正确的位置。
这是我用来计算碰撞事件的影响时间的代码:
float Collision::collisionTime(QVector2D &normal)
{
// _one is our moving entity, _two is a static block
// Calculate the distance to entry and distance to exit
QVector2D distToEntry, distToExit;
QVector2D velocity = _one->velocity();
if (velocity.x() > 0)
{
distToEntry.setX(_two->rect().left() - _one->rect().right());
distToExit.setX( _two->rect().right() - _one->rect().left());
}
else
{
distToEntry.setX(_two->rect().right() - _one->rect().left());
distToExit.setX( _two->rect().left() - _one->rect().right());
}
if (velocity.y() > 0)
{
distToEntry.setY(_two->rect().top() - _one->rect().bottom());
distToExit.setY( _two->rect().bottom() - _one->rect().top());
}
else
{
distToEntry.setY(_two->rect().bottom() - _one->rect().top());
distToExit.setY( _two->rect().top() - _one->rect().bottom());
}
// Calculate the entry and exit times.
QVector2D entry, exit;
if (velocity.x() == 0)
{
entry.setX(-std::numeric_limits<float>::infinity());
exit.setX(std::numeric_limits<float>::infinity());
}
else
{
entry.setX(distToEntry.x() / velocity.x());
exit.setX(distToExit.x() / velocity.x());
}
if (velocity.y() == 0)
{
entry.setY(-std::numeric_limits<float>::infinity());
exit.setY(std::numeric_limits<float>::infinity());
}
else
{
entry.setY(distToEntry.y() / velocity.y());
exit.setY(distToExit.y() / velocity.y());
}
if (entry.x() > 1.0)
{
entry.setX(-std::numeric_limits<float>::infinity());
}
if (entry.y() > 1.0)
{
entry.setY(-std::numeric_limits<float>::infinity());
}
// Find the earliest / latest times of collision.
float entryTime = std::max(entry.x(), entry.y());
float exitTime = std::min(exit.x(), exit.y());
// If there was no collision...
if ((entryTime > exitTime) ||
(entry.x() < 0 && entry.y() < 0))
{
normal = QVector2D(0, 0);
return 1.0;
}
// If there was a collision,
// set the proper normal and return.
if (entry.x() >= entry.y())
{
if (distToEntry.x() < 0)
{
normal = QVector2D(1.0, 0);
}
else
{
normal = QVector2D(-1.0, 0);
}
}
else
{
if (distToEntry.y() < 0)
{
normal = QVector2D(0, 1.0);
}
else
{
normal = QVector2D(0, -1.0);
}
}
return entryTime;
}
这是生成碰撞响应的代码:
bool Collision::resolve()
{
QVector2D collisionNormal;
// Calculate the collision time and normal.
float time = collisionTime(collisionNormal);
// Move to the point of collision.
_one->setPos(_one->pos().x() + _one->velocity().x() * time,
_one->pos().y() + _one->velocity().y() * time);
float remainingTime = 1.0 - time;
// If remainingTime > 0, there was a collision.
// Apply a slide effect.
if (remainingTime > 0)
{
float dotProduct = (_one->velocity().x() * collisionNormal.y() + _one->velocity().y() * collisionNormal.x()) * remainingTime;
_one->setVelocity(QVector2D(dotProduct * collisionNormal.y(), dotProduct * collisionNormal.x()));
return true;
}
return false;
}
现在,问题——在以下情况下,移动实体完全停止。我曾期望它在这两种情况下都会水平滑动。我一直无法确定是碰撞检测代码的问题,还是碰撞响应代码的问题:
1.
|¯¯¯¯¯¯¯¯|
| Entity |
| |
|________|
|¯¯¯¯¯¯¯¯| ⇲ velocity
| Block |
| |
|________|
2.
|¯¯¯¯¯¯¯¯|
| Entity |
| | ⇲ velocity
|________|
|¯¯¯¯¯¯¯¯‖¯¯¯¯¯¯¯¯|
| Block ‖ Block |
| ‖ |
|________‖________|