我看过 Peter Colling Ridge 的精彩教程
http://www.petercollingridge.co.uk/pygame-physics-simulation/
我正在扩展 PyParticles 脚本
代码可以在网站上免费获得,我使用的是 PyParticles4.py
教程中使用的类
粒子类
具有半径、质量、速度、位置的圆形二维物体
Spring 类
绑定(bind) 2 个对象(粒子)并使用 Hooke's law 的 Spring (F = -kx) 确定它们之间的相互作用
环境类
粒子相互作用的环境
我想知道我是否可以使用 2 个粒子并制作一个具有特定长度的“杆”类(如本教程中的 Spring 类)并且不允许粒子靠近(指定) ) 长度。
另外,
对每个粒子施加一个力(需要时),如果一个粒子被拉向左侧,另一个粒子也会向左拉,但实际上......
就像使用钢棒将 2 种不同类型的球(从中心)连接起来一样,但是在 2-d..
而且我不想使用第 3 方模块
提前致谢..
编辑/更新:
试图应用约束定理(失败)
这是代码:
class Rod:
def __init__(self, p1, p2, length=50):
self.p1 = p1
self.p2 = p2
self.length = length
def update(self):
'Updates The Rod and Particles'
# Temp store of co-ords of Particles involved
x1 = self.p1.x
x2 = self.p2.x
###### Same for Y #######
y1 = self.p1.y
y2 = self.p2.y
# Calculation of d1,d2,d3 and final values (x2,y2)
# from currently known values(x1,y1)...
# From Constraint algorithm(see @HristoIliev's comment)
dx1 = x2 - x1
dy1 = y2 - y1
# the d1, d2, d3
d1 = math.hypot(dx1,dy1)
d2 = abs(d1)
d3 = (d2-self.length)/d2
x1 = x1 + 0.5*d1*d3
x2 = x2 - 0.5*d1*d3
y1 = y1 + 0.5*d1*d3
y2 = y1 - 0.5*d1*d3
# Reassign next positions
self.p1.x = x1
self.p2.x = x2
###### Same for Y #######
self.p1.y = y1
self.p2.y = y2
最佳答案
2D 中的杆具有 3 个自由度(2 个速度/位置 + 1 个旋转/角频率)。
我会表示中心的位置,它以通常的方式被力修改,并使用旋转(为简单起见,关于系统的中心)变量计算粒子的位置。
旋转由力修改
ang_accel = F * r * sin (angle(F,r)) / (2*M * r^2)
在哪里
ang_accel
是角加速度
F
是作用在特定球上的力,所以有 2 torques * 加起来是因为有两个力加起来(向量方向)以更新中心的位置。
r
是长度的一半
angle(F,r)
是力矢量与半径矢量(从中心到受力粒子)的夹角,
这样
F * r * sin (angle(F,r))
是 torque关于中心,和
2*M * r^2
是 moment of inertia两点绕心系统。
关于 python 球物理模拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14137475/