title: Collision equation for two 3d objects
body: <p>I'm trying a script in maya to see if i can get gravity and that lot working with spherical objects, and currently it works fine (tried making a moon and earth to scale and calibrated the gravity effect to that), but I'd like to try get it a step further and add collisions to make objects bounce off each other, but after looking around for ages yesterday, I found it's hard enough with 2d objects, never mind 3d.<br/><br/>
  I've managed to write a bit which can detect a collision, where it gets the distance between both objects and compares it against the radius of both objects, which seems to work (needs rewriting though), but I couldn't figure out what to do next. To be honest, I'm not even sure if it's possible to do with something like this without some advanced university knowledge which is why I eventually gave up :P<br/><br/>
  Just for an idea of how it works, here's the current stage of the main part - objSel[j] is the current selected object, allObjects is everything but the current selected object</p>
  
  <pre><code>def moveObjects(originalTime,objSel,objMultiplySize):
      moveAmounts = []
      crashed = False
      for j in range(len(objSel)):
  
          #get initial values
          originalVelocity = getVelocity(objSel[j],originalTime,objMultiplySize)
          objVolume = getVolume(objSel[j],objMultiplySize)
          allObjects = selectAllOtherObjects(objSel[j], objSel)
  
          #calculate gravity effect on object
          xDist = 0
          yDist = 0
          zDist = 0
          for i in range (0, len(allObjects) ):
              attraction = calcuateForce(objSel[j],allObjects[i],objMultiplySize)
              distanceFromObj = getDistance(allObjects[i],objSel[j],objMultiplySize)[1]
              xDist += distanceFromObj[0] * attraction / (objVolume*2.15*math.pi)
              yDist += distanceFromObj[1] * attraction / (objVolume*2.15*math.pi)
              zDist += distanceFromObj[2] * attraction / (objVolume*2.15*math.pi)
          gravityEffect = [xDist,yDist,zDist]
          newX = (originalVelocity[0]+gravityEffect[0])/objMultiplySize
          newY = (originalVelocity[1]+gravityEffect[1])/objMultiplySize
          newZ = (originalVelocity[2]+gravityEffect[2])/objMultiplySize
          newVelocity = [newX,newY,newZ]
          moveAmounts.append( newVelocity )
  
  
  
      #-----------this whole bit needs rewriting--------
  
      py.currentTime( originalTime + 1, edit = True, update = True)
      for j in range(len(moveAmounts)):
  
          #collision detection
          allObjects = selectAllOtherObjects(objSel[j], objSel)
          originalRadius = getRadius(objSel[j],objMultiplySize)
  
          for i in range (0, len(allObjects) ):
              objRadius = getRadius(allObjects[i],objMultiplySize)
              objDistance = getDistance(allObjects[i],objSel[j],objMultiplySize)
              if objDistance[0] &lt; objRadius + originalRadius:
                  force1 = moveAmounts[j][0]*objMultiplySize * objRadius
                  print "Crashed"
                  crashed = True
  
          if crashed != True:
  
              #move object
              py.move( objSel[j], float(moveAmounts[j][0]), float(moveAmounts[j][1]), float(moveAmounts[j][2]), relative = True )
              py.setKeyframe( objSel[j], attribute='translateX')
              py.setKeyframe( objSel[j], attribute='translateY')
              py.setKeyframe( objSel[j], attribute='translateZ')
  
          else:
  
              #dunno what to do here
              for i in range (0, len(allObjects) ):
                  mass1 = getVolume(allObjects[i],objMultiplySize)
                  velocity1 = getVelocity(allObjects[i],originalTime,objMultiplySize)
                  mass2 = getVolume(objSel[j],objMultiplySize)
                  velocity2 = getVelocity(objSel[j],originalTime,objMultiplySize)
                  m1v1m2v2X = mass1*velocity1[0] + mass2*velocity2[0]
                  m1v1m2v2Y = mass1*velocity1[1] + mass2*velocity2[1]
                  m1v1m2v2Z = mass1*velocity1[2] + mass2*velocity2[2]
                  totalMass = mass1+mass2
  </code></pre>