title: python ball physics simulation
body: <p>I have seen the great tutorial by Peter Colling Ridge on<br>
  <a href="http://www.petercollingridge.co.uk/pygame-physics-simulation/" rel="nofollow">http://www.petercollingridge.co.uk/pygame-physics-simulation/</a><br>
  and I am extending the PyParticles script<br>
  The code is available on the site(for free), I am using PyParticles4.py</p>
  
  <h2>Classes used in the tutorial</h2>
  
  <p><strong>The Particle Class</strong><br>
  Circular 2d objects with radius,mass,velocity,location<br>
  <strong>The Spring Class</strong><br>
  A spring that binds 2 objects (Particles) and uses the <a href="https://www.google.co.in/search?q=Hooke%27s%20law" rel="nofollow"><strong>Hooke's law</strong></a>  (F = -kx) to determine the interaction between them<br>
  <strong>The Environment Class</strong><br>
  The Environment where the Particles interact</p>
  
  <p>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.<br>
  <strong>Also,</strong><br>
  Appling a force (when needed) to each Particle such that if one is pulled toward the left, so does the other, but Realistically..<br>
  Much like if a 2 different types of balls were joined(from the center) using a steel rod, but in 2-d..<br>
  And I <strong>don't want to use 3rd party modules</strong>  </p>
  
  <p>Thanks in advance.. </p>
  
  <p><strong>EDIT/UPDATE:</strong><br>
  Tried to apply constraint theorem (it failed)<br>
  Here's the code: </p>
  
  <pre><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
  </code></pre>