【发布时间】:2012-03-09 01:44:37
【问题描述】:
我正在尝试为我的乒乓球游戏实施简单的连续碰撞检测,但我不确定我是否正确实施或理解这一点。 AFAIR 连续碰撞检测用于快速移动的物体,这些物体可能会绕过另一个物体绕过正常的碰撞检测。
所以我尝试的是,因为我拥有的唯一快速移动的对象是一个球,我只需要球的位置、它的移动速度以及我们要比较的对象的位置。
据此,我认为最好例如,如果球的移动速度表明它正在向左移动,我会将它的最左侧边界与另一个对象的最右侧边界进行比较。从这里我将逐步通过将移动速度添加到球的最左侧边界并进行比较以确保它大于右侧边界的其他对象。这将表明没有左右碰撞。
我有一些工作,但不幸的是,球开始正常弹跳一段时间,然后它就像在什么都没有的情况下击中桨一样。
我有点迷茫,任何帮助将不胜感激!
static bool CheckContinuousCollision(PActor ball, PRect ballRect, PActor other, PRect otherRect)
{
PVector ballMoveSpeed;
int ballXLimit;
int ballYLimit;
ballMoveSpeed = ball.moveSpeed;
// We are moving left
if ( sgn(ball.moveSpeed.x) < 0 )
{
ballXLimit = std.math.abs(ballMoveSpeed.x) / 2;
for ( int i = 0; i <= ballXLimit; i++ )
{
if ( ballRect.Left < otherRect.Right && otherRect.Left < ballRect.Left)
{
return true;
}
ballRect.Left -= i;
}
}
//We are moving right
if ( sgn(ball.moveSpeed.x) > 0)
{
ballXLimit = std.math.abs(ballMoveSpeed.x) / 2;
for ( int i = 0; i < ballXLimit; i ++ )
{
if ( ballRect.Right > otherRect.Left && ballRect.Right < otherRect.Right )
{
return true;
}
ballRect.Right += i;
}
}
// we are not moving
if ( sgn(ball.moveSpeed.x) == 0)
{
return false;
}
}
【问题讨论】:
标签: math 2d collision-detection