6
我試圖爲我的pong遊戲實現簡單的連續碰撞檢測,但是我不確定我正在執行或理解此權限。 AFAIR連續碰撞檢測用於可能通過另一個物體的快速移動物體,從而繞過正常的碰撞檢測。2D連續碰撞檢測
那麼我想是因爲只有快速移動的物體我有一個球我只需要球的位置,它的移動速度,並且我們是在比較對象的位置。
從這個我想這將是最好的,例如如果球的移動速度表示,它正在向左移動,我會比較它的最左邊的結合到最右邊界其他物體。從這裏我將通過將移動速度添加到球的最左邊界並進行比較以確保其大於其他對象的右邊界。這將表明沒有左右碰撞。
我有事有些工作,但遺憾的是,球開始了一段時間,通常反彈則充當如果它擊中槳時,無所不有。
我有點失落,任何幫助,將不勝感激!
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;
}
}
哦,你是對的!我一直在努力工作太久。我忘記了不檢查Y尺寸的副作用。 – RedShft 2012-03-09 04:34:51