在C ++中,我在我的小体素游戏中实现了一个小型碰撞检测和响应系统。现在只有AABB和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轴。 我的错误。
Screenshot example http://fs1.directupload.net/images/150722/ottekmg7.png Diagram of example http://fs1.directupload.net/images/150722/sldarmk4.png