Python球物理模拟 [英] python ball physics simulation

查看:369
本文介绍了Python球物理模拟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在
上看到了彼得·科林·里奇(Peter Colling Ridge)的精彩教程 http://www.petercollingridge.co.uk/pygame-physics-simulation/
我正在扩展PyParticles脚本
该代码可在网站上免费获得,我正在使用PyParticles4.py

I have seen the great tutorial by Peter Colling Ridge on
http://www.petercollingridge.co.uk/pygame-physics-simulation/
and I am extending the PyParticles script
The code is available on the site(for free), I am using PyParticles4.py

粒子类
具有半径,质量,速度,位置的圆形2d对象
春季班
绑定2个对象(粒子)并使用 胡克定律的弹簧 (F = -kx)确定它们之间的相互作用
环境类
粒子相互作用的环境

The Particle Class
Circular 2d objects with radius,mass,velocity,location
The Spring Class
A spring that binds 2 objects (Particles) and uses the Hooke's law (F = -kx) to determine the interaction between them
The Environment Class
The Environment where the Particles interact

我想知道我是否可以使用2个粒子并创建一个"Rod"类(如本教程中的Spring类),该类具有特定的长度,并且不允许粒子更靠近(指定了)长度.
也是
在每个粒子上施加力(如果需要),使得如果一个粒子向左拉,另一个粒子也是如此,但实际上..
就像使用钢棒将2种不同类型的球(从中心)连接在一起一样,但是在2-d中..
而且我不想使用第三方模块

I was wondering if I could to use 2 Particles and make a 'Rod' class (like the Spring class in the tutorial) that had a specific length and didn't allow the particles to come closer go further than that (specified) length.
Also,
Appling a force (when needed) to each Particle such that if one is pulled toward the left, so does the other, but Realistically..
Much like if a 2 different types of balls were joined(from the center) using a steel rod, but in 2-d..
And I don't want to use 3rd party modules

谢谢.

编辑/更新:
试图应用约束定理(失败)
这是代码:

EDIT/UPDATE:
Tried to apply constraint theorem (it failed)
Here's the code:

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个旋转/角频率).
我将表示通过力以常规方式修改的中心位置,并使用旋转(为简单起见,以系统中心为中心)变量来计算粒子的位置.
旋转是通过力通过以下方式修改的:

A rod in 2D has 3 degrees of freedom (2 velocities/positions + 1 rotation/angular freq).
I would represent the position of the center which is modified by forces in the usual way and calculate the position of the particles using the rotation (for simplicity, about the center of the system) variable.
The rotation is modified by forces by

ang_accel = F * r * sin (angle(F,r)) / (2*M * r^2)

哪里

ang_accel是角加速度

F是作用在特定球上的力,因此有2个扭矩 *之所以累加,是因为有两个力(矢量方向)累加起来以更新中心的位置.

F is a force acting on a particular ball so there is 2 torques* that add up as there is two forces that add up (vector-wise) in order to update the position of the center.

r是长度的一半
angle(F,r)是力矢量和半径矢量之间的角度(从中心到受力的粒子),

r is half of the length
angle(F,r) is the angle between the force vector and the radius vector (from the center to the particle that suffers the force),

这样
F * r * sin (angle(F,r))是关于中心的扭矩,并且
2*M * r^2是围绕中心的两个点的系统的惯性矩.

So that
F * r * sin (angle(F,r)) is the torque about the center, and
2*M * r^2 is the moment of inertia of the system of two points around the center.

这篇关于Python球物理模拟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆