Python ball physics simulation

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

Classes used in the textbook

Particle class
Round 2d objects with radius, mass, speed, location
Spring Class
A Spring, which binds 2 objects (Particles) and uses Hooke's law (F = -kx) to determine the interaction between them
Environment class
The environment in which particles interact

I was wondering if I can use 2 particles and create a class "Rod" (for example, the Spring class in the tutorial), which had a certain length and did not allow particles to come closer, go further than this (specified) length.
Besides,
Applying an effort (when necessary) to each Particle so that if one is pulled to the left, then the other, but Realistically ..
It is very likely that two different types of balls were connected (from the center) using a steel rod, but in 2 ..
And I do not want to use third-party modules

Thanks in advance.

EDIT / UPDATE:
Tried to apply the constraint theorem (it failed)
Here is 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 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 
+7
source share
1 answer

The bar in 2D has 3 degrees of freedom (2 speeds / positions + 1 revolution / angular frequency).
I would imagine the position of the center, which was modified by forces in the usual way and calculated the position of particles by rotation (for simplicity, relative to the center of the system). The rotation changes forces to

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

Where

ang_accel - angular acceleration

F is the force acting on a particular ball, so there are 2 torques * that add up, because there are two forces that add up (vector) to update the center position.

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

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

+3
source

All Articles