计算两个运动球将碰撞的x/y点 [英] Calculate x/y point that 2 moving balls will collide
问题描述
我正在尝试使(基本上)是一个简单的台球游戏,并且希望能够预测一杆击中另一个球后的去向.
I'm trying to make what is (essentially) a simple pool game, and would like to be able to predict where a shot will go once it hits another ball.
我相信第一部分是计算母球是否会击中任何东西,如果击中,它将碰撞到哪里.我可以计算直线和一个球的碰撞点,但是不能计算两个球.
The first part is, I believe, to calculate if the cueball will hit anything, and if it does, where it collides. I can work out collision points for a line and a ball, but not 2 balls.
因此,给定2个球的x/y位置和速度,我如何计算它们碰撞的点?
So given the x/y positions and velocities of 2 balls, how do I calculate the point at which they collide?
(PS:我知道我可以通过计算整个过程中每一步的两个球之间的距离来做到这一点,但我希望有一些更优雅,更理想的东西.)
(PS: Im aware I can do this by calculating the distance between the two balls every step along the way, but I was hoping for something a little more elegant and optimal.)
设置示例:尝试计算红点
Example of setup: trying to calculate the red dot
http://dl.dropbox.com/u/6202117/circle.PNG
推荐答案
一些注意事项:
- 两个半径为
r
的球碰撞时,其中心相距2r
. - 您的第一个球可以假定是直线移动(嗯,是第一个近似值,但以此为起点),您可以找到该路径与从第一个球到第二个球的方向之间的夹角
alpha
. - 你知道固定球的中心,不是吗?
- When two balls, each of radius
r
collide their centers are2r
apart. - Your first ball can be assumed to travel in a straight line (well, first approximation, but start with this), and you can find the angle,
alpha
between this path and the direction from the first ball to the second. - You know the center of the stationary ball, no?
现在您要做一些几何了.
Now you have some geometry to do.
进行此构造:
- 将第一个(移动的)球的当前中心标记为点
A
. - 将固定球的中心标记为点
B
. - 构造线段
AB
. - 从
A
沿运动方向构造射线R
. - 在
B
周围构造半径为2r
的圆. - 从
B
垂直于R
的线段中删除一个线段,称为交点C
. - 您知道距离
AB
,并且可以找到AB
和R
之间的夹角alpha
,根据《锡涅斯定律》可以找到长度BC
. - 从该长度确定是否有0、1或2个解决方案.如果为0或1,就完成了.
- 构造点
D
,使圆与R
的交点更靠近A,然后再次使用正弦定律找到距离AD. - 碰撞点是
BD
的中点
- Mark the current center of the first (moving) ball as point
A
. - Mark the center of the stationary ball as point
B
. - Construct line segment
AB
. - Construct the ray,
R
, fromA
in the direction of movement. - Construct a circle of radius
2r
aroundB
. - Drop a segment from
B
perpendicular toR
call the point of intersectionC
. - You know the distance
AB
and you can find the anglealpha
betweenAB
andR
, with the Law of Sines find the length ofBC
. - From that length determine if there are 0, 1 or 2 solutions. If there are 0 or 1 you are done.
- Construct point
D
where the circle meetsR
closer to A, and use the Law of Sines again to find the distance AD. - The point of collision is the midpoint of
BD
现在您已经了解了一切.
and now you know everything.
以此为基础构建高效的代码.
Constructing efficient code from this is left as an exercise.
顺便说一句-如果两个球都在移动,则此构造将不起作用,但是您可以将其转换为一个静止的框架,以这种方式求解,然后再转换回去.只需确保在逆向转换后 后检查解决方案是否在允许的区域内即可.
BTW-- This construction won't work if both balls are moving, but you can transform into a frame where one is stationary, solve it that way, then transform back. Just be sure to check that the solution is in the allowed area after the reverse transformation...
/物理学家不能不发表这样的评论.我试图抗拒.我真的做了.
/ Physicists can't not make comments like this. I tried to resist. I really did.
这篇关于计算两个运动球将碰撞的x/y点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!