Guest User

Untitled

a guest
Jun 22nd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. if distance <= self.radius + other.radius:
  2. # push both out of overlap
  3. overlap = abs(self.radius - distance + other.radius)
  4. collision_angle = math.atan2(dy, dx)
  5. self.x -= math.cos(collision_angle) * overlap/2
  6. self.x -= math.sin(collision_angle) * overlap/2
  7. other.x += math.cos(collision_angle) * overlap/2
  8. other.x += math.sin(collision_angle) * overlap/2
  9.  
  10. # calculate normals at collision point
  11. collision_x = self.x + self.radius
  12. collision_y = self.y + self.radius
  13.  
  14. normal_self_dx = abs(self.x - collision_x)
  15. normal_self_dy = abs(self.y - collision_y)
  16. normal_self_angle = math.atan2(normal_self_dy, normal_self_dx)
  17. normal_self_x = math.cos(normal_self_angle) * self.radius
  18. normal_self_y = math.sin(normal_self_angle) * self.radius
  19.  
  20. normal_other_dx = abs(other.x - collision_x)
  21. normal_other_dy = abs(other.y - collision_y)
  22. normal_other_angle = math.atan2(normal_other_dy, normal_other_dx)
  23. normal_other_x = math.cos(normal_other_angle) * other.radius
  24. normal_other_y = math.sin(normal_other_angle) * other.radius
  25.  
  26. # get crossproduct (= angles)
  27. self_vx = math.cos(math.radians(self.angle)) * self.speed
  28. self_vy = math.sin(math.radians(self.angle)) * self.speed
  29. bounce_angle = normal_self_x*self_vy - normal_self_y*self_vx
  30. self.angle = bounce_angle
  31.  
  32. other_vx = math.cos(math.radians(other.angle)) * other.speed
  33. other_vy = math.sin(math.radians(other.angle)) * other.speed
  34. bounce_angle = normal_other_x*other_vy - normal_other_y*other_vx
  35. other.angle = bounce_angle
Add Comment
Please, Sign In to add comment