答案 0 :(得分:1)
你可以沿着桌子上的球路走。使这一点变得简单的一点是球以45°的方向开始。因此,所有碰撞角度都是45°。因此,您只需要反转球的方向向量的一个分量。
这是一些C#示例代码。坐标系的原点位于表格的左下角。球位置在其左下角的边界框处测量:
int ballX = 0;
int ballY = 0;
int ballWidth = 5;
int tableWidth = 230;
int tableHeight = 130;
int directionX = 1;
int directionY = 1;
while(true)
{
//the distances that the ball could travel until it collides with a vertical or horizontal border, respectively
int travelDistanceX, travelDistanceY;
if (directionX > 0)
travelDistanceX = tableWidth - ballWidth - ballX;
else
travelDistanceX = ballX;
if (directionY > 0)
travelDistanceY = tableHeight - ballWidth - ballY;
else
travelDistanceY = ballY;
if(travelDistanceX == travelDistanceY)
{
//we found the target pocket:
Console.WriteLine("Target is located at {0}/{1}.", ballX + travelDistanceX * directionX, ballY + travelDistanceY * directionY);
break;
}
if(travelDistanceX < travelDistanceY)
{
//collision with the vertical borders
ballX += travelDistanceX * directionX;
ballY += travelDistanceX * directionY;
directionX *= -1;
}
else
{
//collision with the horizontal borders
ballX += travelDistanceY * directionX;
ballY += travelDistanceY * directionY;
directionY *= -1;
}
Console.WriteLine("Collision at {0}/{1}.", ballX, ballY);
}
代码产生以下路径:
Collision at 125/125.
Collision at 225/25.
Collision at 200/0.
Collision at 75/125.
Collision at 0/50.
Collision at 50/0.
Collision at 175/125.
Collision at 225/75.
Collision at 150/0.
Collision at 25/125.
Collision at 0/100.
Collision at 100/0.
Target is located at 225/125.
(225/125)
处的口袋(加上球宽以获得实际位置)为B。