实现二维弹性碰撞方程的问题
Problems implementing equation for 2d elastic collision
所以我正在尝试制作一个二维球物理模拟器。在我尝试向前推进并实现视觉效果之前,我正在测试物理将如何工作,并且我坚持实现这个 equation。这是我到目前为止所做的:
import math
b1m = 50 # ball 1 mass
b2m = 40 # ball 2 mass
b1vx = 35 # ball 1 velocity x
b1vy = 10 # ball 1 velocity y
b2vx = -15 # ball 2 velocity x
b2vy = -40 # ball 2 velocity y
b1v = math.sqrt(abs(b1vx) + abs(b1vy)) # ball 1 velocity | calculates overall velocity/speed
b2v = math.sqrt(abs(b2vx) + abs(b2vy)) # ball 2 velocity | calculates overall velocity/speed
b1ma = math.degrees(math.atan2(b1vy, b1vx)) % 360 # ball 1 movement angle | calculates the angle at
which the ball is travelling
b2ma = math.degrees(math.atan2(b2vy, b2vx)) % 360 # ball 2 movement angle | calculates the angle at
which the ball is travelling
ca = 31.5 # contact angle
b1vx = (b1v * math.cos(b1ma - ca) * (b1m - b2m) + 2 * b2m * math.cos(b2ma - ca)) / (b1m + b2m) *
math.cos(ca) + b1v * math.sin(b1ma - ca) * math.cos(ca + (math.pi/2))
b1vy = (b1v * math.cos(b1ma - ca) * (b1m - b2m) + 2 * b2m * math.cos(b2ma - ca)) / (b1m + b2m) *
math.sin(ca) + b1v * math.sin(b1ma - ca) * math.sin(ca + (math.pi/2))
print(b1vx) # print to check if correct values | correct value should be -17.11
print(b1vy) # print to check if correct values | correct value should be 21.94
# according to http://www.sciencecalculators.org/mechanics/collisions/
我不确定出了什么问题,但它为我分配的变量产生了错误的结果。我尝试将角度转换为弧度,因为我知道那是 python 默认值,我尝试稍微编辑方程式,看看操作顺序是否被搞乱了。但是还是没有结果。我基于 this website 所需的值,它显示了基于用户给定变量的二维球模拟。非常感谢任何帮助!
感谢您提出有趣的问题,我喜欢研究它。
您的代码中存在 3 个问题:
- 两个球的总速度取决于平方分量之和的平方根(勾股定理)。您的代码中的各个组件未平方
- 在计算结果速度的公式中,分子中缺少一个分量:b2v(在
2* m2 * v2 * cos(θ2−φ)
中)
math.cos
和 math.sin
bith 以 弧度 中的 angular 值作为输入。
通过修复这三个问题,我能够重现预期值。然而,在输入这个答案时,我注意到 y 方向的速度有错误的符号。第二个球的 y 分量也是如此。我再次检查方程式,但没能发现另一个错误。但是,如果您查看所链接网站的动画,您会发现球正在向动画的左下角移动,因此 x 和 y 似乎为负。我玩了不同的输入值,球的运动方向永远不适合它呈现的 y 速度。除非我遗漏了一些明显的符号反转,否则这可能是该网站上的错误。
代码如下:
import math
b1m = 50 # ball 1 mass
b2m = 40 # ball 2 mass
b1vx = 35 # ball 1 velocity x
b1vy = 10 # ball 1 velocity y
b2vx = -15 # ball 2 velocity x
b2vy = -40 # ball 2 velocity y
#1 square root of squared components
b1v = math.sqrt(b1vx**2 + b1vy**2) # ball 1 velocity | calculates overall velocity/speed
b2v = math.sqrt(b2vx**2 + b2vy**2) # ball 2 velocity | calculates overall velocity/speed
# ball 1 movement angle | calculates the angle at which the ball is travelling
b1ma = math.atan2(b1vy, b1vx)
# ball 2 movement angle | calculates the angle at which the ball is travelling
b2ma = math.atan2(b2vy, b2vx)
ca = math.radians(31.5) # contact angle
#3 component missing in second component of numerator of fraction (b2v)
b1vx = ((b1v * math.cos(b1ma - ca) * (b1m - b2m) + 2 * b2m * b2v * math.cos(b2ma - ca)) / (b1m + b2m)) * math.cos(ca) + b1v * math.sin(b1ma - ca) * math.cos(ca + (math.pi/2))
b1vy = ((b1v * math.cos(b1ma - ca) * (b1m - b2m) + 2 * b2m * b2v * math.cos(b2ma - ca)) / (b1m + b2m)) * math.sin(ca) + b1v * math.sin(b1ma - ca) * math.sin(ca + (math.pi/2))
b2vx = ((b2v * math.cos(b2ma - ca) * (b2m - b1m) + 2 * b1m * b1v * math.cos(b1ma - ca)) / (b2m + b1m)) * math.cos(ca) + b2v * math.sin(b2ma - ca) * math.cos(ca + (math.pi/2))
b2vy = ((b2v * math.cos(b2ma - ca) * (b2m - b1m) + 2 * b1m * b1v * math.cos(b1ma - ca)) / (b2m + b1m)) * math.sin(ca) + b2v * math.sin(b2ma - ca) * math.sin(ca + (math.pi/2))
print(b1vx) # print to check if correct values | correct value should be -17.11
print(b1vy) # print to check if correct values | correct value should be 21.94
print(b2vx)
print(b2vy)
# according to http://www.sciencecalculators.org/mechanics/collisions/
所以我正在尝试制作一个二维球物理模拟器。在我尝试向前推进并实现视觉效果之前,我正在测试物理将如何工作,并且我坚持实现这个 equation。这是我到目前为止所做的:
import math
b1m = 50 # ball 1 mass
b2m = 40 # ball 2 mass
b1vx = 35 # ball 1 velocity x
b1vy = 10 # ball 1 velocity y
b2vx = -15 # ball 2 velocity x
b2vy = -40 # ball 2 velocity y
b1v = math.sqrt(abs(b1vx) + abs(b1vy)) # ball 1 velocity | calculates overall velocity/speed
b2v = math.sqrt(abs(b2vx) + abs(b2vy)) # ball 2 velocity | calculates overall velocity/speed
b1ma = math.degrees(math.atan2(b1vy, b1vx)) % 360 # ball 1 movement angle | calculates the angle at
which the ball is travelling
b2ma = math.degrees(math.atan2(b2vy, b2vx)) % 360 # ball 2 movement angle | calculates the angle at
which the ball is travelling
ca = 31.5 # contact angle
b1vx = (b1v * math.cos(b1ma - ca) * (b1m - b2m) + 2 * b2m * math.cos(b2ma - ca)) / (b1m + b2m) *
math.cos(ca) + b1v * math.sin(b1ma - ca) * math.cos(ca + (math.pi/2))
b1vy = (b1v * math.cos(b1ma - ca) * (b1m - b2m) + 2 * b2m * math.cos(b2ma - ca)) / (b1m + b2m) *
math.sin(ca) + b1v * math.sin(b1ma - ca) * math.sin(ca + (math.pi/2))
print(b1vx) # print to check if correct values | correct value should be -17.11
print(b1vy) # print to check if correct values | correct value should be 21.94
# according to http://www.sciencecalculators.org/mechanics/collisions/
我不确定出了什么问题,但它为我分配的变量产生了错误的结果。我尝试将角度转换为弧度,因为我知道那是 python 默认值,我尝试稍微编辑方程式,看看操作顺序是否被搞乱了。但是还是没有结果。我基于 this website 所需的值,它显示了基于用户给定变量的二维球模拟。非常感谢任何帮助!
感谢您提出有趣的问题,我喜欢研究它。 您的代码中存在 3 个问题:
- 两个球的总速度取决于平方分量之和的平方根(勾股定理)。您的代码中的各个组件未平方
- 在计算结果速度的公式中,分子中缺少一个分量:b2v(在
2* m2 * v2 * cos(θ2−φ)
中) math.cos
和math.sin
bith 以 弧度 中的 angular 值作为输入。
通过修复这三个问题,我能够重现预期值。然而,在输入这个答案时,我注意到 y 方向的速度有错误的符号。第二个球的 y 分量也是如此。我再次检查方程式,但没能发现另一个错误。但是,如果您查看所链接网站的动画,您会发现球正在向动画的左下角移动,因此 x 和 y 似乎为负。我玩了不同的输入值,球的运动方向永远不适合它呈现的 y 速度。除非我遗漏了一些明显的符号反转,否则这可能是该网站上的错误。
代码如下:
import math
b1m = 50 # ball 1 mass
b2m = 40 # ball 2 mass
b1vx = 35 # ball 1 velocity x
b1vy = 10 # ball 1 velocity y
b2vx = -15 # ball 2 velocity x
b2vy = -40 # ball 2 velocity y
#1 square root of squared components
b1v = math.sqrt(b1vx**2 + b1vy**2) # ball 1 velocity | calculates overall velocity/speed
b2v = math.sqrt(b2vx**2 + b2vy**2) # ball 2 velocity | calculates overall velocity/speed
# ball 1 movement angle | calculates the angle at which the ball is travelling
b1ma = math.atan2(b1vy, b1vx)
# ball 2 movement angle | calculates the angle at which the ball is travelling
b2ma = math.atan2(b2vy, b2vx)
ca = math.radians(31.5) # contact angle
#3 component missing in second component of numerator of fraction (b2v)
b1vx = ((b1v * math.cos(b1ma - ca) * (b1m - b2m) + 2 * b2m * b2v * math.cos(b2ma - ca)) / (b1m + b2m)) * math.cos(ca) + b1v * math.sin(b1ma - ca) * math.cos(ca + (math.pi/2))
b1vy = ((b1v * math.cos(b1ma - ca) * (b1m - b2m) + 2 * b2m * b2v * math.cos(b2ma - ca)) / (b1m + b2m)) * math.sin(ca) + b1v * math.sin(b1ma - ca) * math.sin(ca + (math.pi/2))
b2vx = ((b2v * math.cos(b2ma - ca) * (b2m - b1m) + 2 * b1m * b1v * math.cos(b1ma - ca)) / (b2m + b1m)) * math.cos(ca) + b2v * math.sin(b2ma - ca) * math.cos(ca + (math.pi/2))
b2vy = ((b2v * math.cos(b2ma - ca) * (b2m - b1m) + 2 * b1m * b1v * math.cos(b1ma - ca)) / (b2m + b1m)) * math.sin(ca) + b2v * math.sin(b2ma - ca) * math.sin(ca + (math.pi/2))
print(b1vx) # print to check if correct values | correct value should be -17.11
print(b1vy) # print to check if correct values | correct value should be 21.94
print(b2vx)
print(b2vy)
# according to http://www.sciencecalculators.org/mechanics/collisions/