Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.02 KB | None | 0 0
  1. collDistance = (self.DIAMETER + particle.DIAMETER) / 2
  2.         dx = particle.position.x - self.position.x
  3.         dy = particle.position.y - self.position.y
  4.         e = self.ELASTICITY * particle.ELASTICITY
  5.         r = self.DIAMETER / 2
  6.         distance = math.hypot(dx, dy)
  7.  
  8.         if distance < collDistance:
  9.             #if particle in self.collision:
  10.              #   return True
  11.             #self.collision.add(particle)
  12.             vp1 = self.velocity.x * dx / distance + self.velocity.y * dy / distance
  13.             vp2 = particle.velocity.x * dx / distance + particle.velocity.y * dy / distance
  14.  
  15.             dt = (self.DIAMETER - distance)/(vp1 - vp2)
  16.  
  17.             self.position.x     -= self.velocity.x * dt
  18.             self.position.y     -= self.velocity.y * dt
  19.             particle.position.x -= particle.velocity.x * dt
  20.             particle.position.y -= particle.velocity.y * dt
  21.  
  22.             dx = particle.position.x - self.position.x
  23.             dy = particle.position.y - self.position.y
  24.  
  25.             distance = math.sqrt(dx * dx + dy * dy)
  26.             ax = dx / distance
  27.             ay = dy / distance
  28.  
  29.             va1 = (self.velocity.x * ax + self.velocity.y * ay)
  30.             vb1 = (-self.velocity.x * ay + self.velocity.y * ax)
  31.  
  32.             va2 = (particle.velocity.x * ax + particle.velocity.y * ay)
  33.             vb2 = (-particle.velocity.x * ay + particle.velocity.y * ax)
  34.  
  35.             vaP1 = va1 + (1 + self.ELASTICITY) * (va2 - va1) / (1 + self.mass / particle.mass)
  36.             vaP2 = va2 + (1 + self.ELASTICITY) * (va1 - va2) / (1 + particle.mass / self.mass)
  37.  
  38.             self.velocity.x = vaP1 * ax - vb1 * ay
  39.             self.velocity.y = vaP1 * ay + vb1 * ax
  40.             particle.velocity.x = vaP2 * ax - vb2 * ay
  41.             particle.velocity.y = vaP2 * ay + vb2 * ax
  42.  
  43.             self.position.x += self.velocity.x * dt
  44.             self.position.y += self.velocity.y * dt
  45.             particle.position.x += particle.velocity.x * dt
  46.             particle.position.y += particle.velocity.y * dt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement