title: Colliding shapes in python
body: <p>My plan is to create a "simple" 2-player minigame game (for ex. sumo and race). </p>
  
  <p>My goal would be to efficiently implement collisions (my current code can deal only with simple wall physics and movement of the objects) with square, circle (and triangle?) shaped objects that can be either part of the environment (for ex. "rocks" or immovable obstacles) or part of the user controlled items (for ex. "cars" or pushable obstacles). It would be also nice to know how mass could also be accounted for in the collisions.</p>
  
  <p>There are two aspects I need help with:</p>
  
  <ol>
  <li><p>Different types of dynamic collisions between two moving objects (that have a mass and 2D vector)
  (the physics part not the detection). </p></li>
  <li><p>Making sure everything that needs to collide, collides fast enough (so that my slow computer could still render more than 40-60 frames per second) , and according to the specific rules (or if it would be possible then according to one rule?). So that it would not also be hard to manage the objects that need to be collided (add,  remove, modify and so on). </p></li>
  </ol>
  
  <p>Or should I just implement two types of collision for ex. static + dynamic circle and dynamic + dynamic circle?</p>
  
  <pre><code>def checkcollisions(object1, object2):
      # x is the current x position
      # y is the current y position
      # angle is the current vector angle (calculated from x and y with pythagoros
      # speed is the length of the vector
      dx = object1.x - object2.x
      dy = object1.y - object2.y
  
      dist = hypot(dx, dy)
      if dist &lt; object1.radius + object2.radius:
          angle = atan2(dy, dx) + 0.5 * pi
          total_mass = object1.mass + object2.mass
          '''''http://www.petercollingridge.co.uk/pygame-physics-simulation/mass'''''
          if (0.79 &lt;= object1.angle &lt; 2.36 or 0.79-2*pi &lt;= object1.angle &lt; 2.36-2*pi) or (3.93 &lt;= object1.angle &lt; 5.5 or 3.93-2*pi &lt;= object1.angle &lt; 5.5-2*pi) and ((0.79 &lt;= object2.angle &lt; 2.36 or 0.79-2*pi &lt;= object2.angle &lt; 2.36-2*pi) or (3.93 &lt;= object2.angle &lt; 5.5 or 3.93-2*pi &lt;= object2.angle &lt; 5.5-2*pi)):
              (object2angle, object2speed) = vectorsum((object2.angle, object2.speed*(object2.mass-object1.mass)/total_mass), (angle+pi, 2*object1.speed*object1.mass/total_mass))
              (object1angle, object1speed) = vectorsum((object1.angle, object1.speed*(object1.mass-object2.mass)/total_mass), (angle, 2*object2.speed*object2.mass/total_mass))
          else:
              '''''https://en.wikipedia.org/wiki/Elastic_collision'''''
              CONTACT_ANGLE = angle
              x = (((object1.speed * cos(object1.angle - CONTACT_ANGLE) * (object1.mass-object2.mass)+ 2*object2.mass*object2.speed*cos(object2.angle - CONTACT_ANGLE))/total_mass)*cos(CONTACT_ANGLE))+object1.speed*sin(object1.angle - CONTACT_ANGLE)*cos(CONTACT_ANGLE + 0.5 * pi)
              y = (((object1.speed * cos(object1.angle - CONTACT_ANGLE) * (object1.mass-object2.mass)+ 2*object2.mass*object2.speed*cos(object2.angle - CONTACT_ANGLE))/total_mass)*cos(CONTACT_ANGLE))+object1.speed*sin(object1.angle - CONTACT_ANGLE)*sin(CONTACT_ANGLE + 0.5 * pi)
              object1angle = pi/2 - atan2(y, x)
              object1speed = hypot(x, y)
  
              x = (((object2.speed * cos(object2.angle - CONTACT_ANGLE)*(object2.mass-object1.mass)+2*object1.mass*object1.speed*cos(object1.angle - CONTACT_ANGLE))/total_mass)*cos(CONTACT_ANGLE))+object2.speed*sin(object2.angle - CONTACT_ANGLE)*cos(CONTACT_ANGLE + 0.5 * pi)
              y = (((object2.speed * cos(object2.angle - CONTACT_ANGLE)*(object2.mass-object1.mass)+2*object1.mass*object1.speed*cos(object1.angle - CONTACT_ANGLE))/total_mass)*cos(CONTACT_ANGLE))+object2.speed*sin(object2.angle - CONTACT_ANGLE)*sin(CONTACT_ANGLE + 0.5 * pi)
              object2angle = pi/2 - atan2(y, x)
              object2speed = hypot(x, y)
  
          (object2.angle, object2.speed) = (object2angle, object2speed)
          (object1.angle, object1.speed) = (object1angle, object1speed)
          object1.speed *= 0.999
          object2.speed *= 0.999
  
          overlap = 0.5*(object1.radius + object2.radius - dist+1)
          object1.x += sin(angle)*overlap
          object1.y -= cos(angle)*overlap
          object2.x -= sin(angle)*overlap
          object2.y += cos(angle)*overlap
  
  '''''http://www.petercollingridge.co.uk/pygame-physics-simulation/mass'''''
  
  
  def vectorsum(vectorx, vectory):  # Every array's first number is the degree from 0, the second is speed
      x = sin(vectory[0]) * vectory[1] + sin(vectorx[0]) * vectorx[1]
      y = cos(vectory[0]) * vectory[1] + cos(vectorx[0]) * vectorx[1]  # Calculating new vectors from anle and lenght
      angle = pi / 2 - atan2(y, x)  # Calculating the degree
      speed = hypot(x, y)  # Calculating the speed
      return angle, speed
  </code></pre>
  
  <p>(I'm just a beginner with Python (or English) so keep that in mind please.)</p>