2012-01-14 61 views
2

我已经写了一些代码,让任意大小的矩形与基于网格的地形设置(用于平台游戏)相冲突。我做的方式是这样的:修复地面碰撞代码中的“打嗝”?

For each tile the rectangle intersects with, do: 
    Calculate the primary axis that this tile is on with respect to the rectangle 
    Calculate the interpenetration of this tile into the rectangle along the primary axis (factoring in previous position offsets from other tiles) 
    If this tile is solid, add that interpenetration to a total collision resolution vector 
Adjust the rectangle's position by the total calculated collision resolution vector 

除非我碰到我的矩形被刚刚超过两个贴片的边界被拉入地随机“挂起起坐”这工作得很好,我的代码决定它需要通过在X轴上推动它来解决碰撞问题,从而停止矩形的运动,除非手动将其从地形中移出以克服它。

我试过一次只解决一个轴上的碰撞(所以如果Y轴分辨率最大,它会忽略任何x轴碰撞分辨率,反之亦然),但是当矩形正在被抖动时会导致抖动压在一个角落(因为这是一种情况,实际上需要同时解决两个轴)。

总之,我可以用什么方法一次解决这两个问题?

回答

0

这是一个非常难的问题,因为它在某些情况下具有无限的交互作用。

要的速度和准确性之间进行选择:

1.增加相互作用计数器为每个对象(矩形)

    碰撞检测之前
  • 所有计数器复位为零

2.如果任何碰撞检测到碰撞的所有对象的增量计数器

3.if计数器值超过限制值停止计算该对象的交互作用

请注意,如果被迫但不会挂断,此方法也可能会造成一些打嗝。