在 C++ 中,我在我的小型体素游戏中实现了一个小型碰撞检测和响应系统。现在只有AABB vs Voxel(体素是一个立方体,所以它也是一个AABB)。
实际上,除了一个特殊的例外,如果玩家 (AABB) 以 45 度角精确地移动到体素的边缘,则所有的工作都很好,那么玩家就会快速进出。在下面的图像中,您可以看到它。
现在我想知道是什么导致了这个问题,但我找不到任何逻辑错误!?所以这里我目前如何在伪代码中做到这一点:
float xNew = xCurrent + xVelocity * deltaTime;
float yNew = yCurrent + yVelocity * deltaTime;
float zNew = zCurrent + zVelocity * deltaTime;
float xCorrected = xNew;
float yCorrected = yNew;
float zCorrected = zNew;
// If the player is moving to right side.
if (xNew < xCurrent)
{
%DO THE FOLLOWING FOR EACH OF THE 4 CORNERS OF THE PLAYER AABB,%
%ON THE RIGHT FACE, UNTIL ONE COLLISION OCCURS%
{
float x = xNew;
float y = yCurrent + %CORNER_Y_OFFSET%;
float z = zCurrent + %CORNER_Z_OFFSET%;
if (getVoxelAt(x, y, z).isSolid())
{
xCorrected = (unsigned int)x + 1;
// Abbort and check z axis.
break;
}
}
}
else if(...)
// Check for the opposite direction...
// If the player is moving to front side.
if (zNew < zCurrent)
{
%DO THE FOLLOWING FOR EACH OF THE 4 CORNERS OF THE PLAYER AABB,%
%ON THE RIGHT FACE, UNTIL ONE COLLISION OCCURS%
{
float x = xCurrent + %CORNER_X_OFFSET%;
float y = yCurrent + %CORNER_Y_OFFSET%;
float z = zNew;
if (getVoxelAt(x, y, z).isSolid())
{
zCorrected = (unsigned int)z + 1;
// Abbort and check Y axis.
break;
}
}
}
else if(...)
// Check for the opposite direction...
// Y axis check, not important for now....
// Apply corrected positions.
xCurrent = xCorrected;
yCurrent = yCorrected;
zCurrent = zCorrected;
编辑:
Z 轴,而不是屏幕截图上的 Y 轴。我的错。