Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if distance <= self.radius + other.radius:
- # push both out of overlap
- overlap = abs(self.radius - distance + other.radius)
- collision_angle = math.atan2(dy, dx)
- self.x -= math.cos(collision_angle) * overlap/2
- self.x -= math.sin(collision_angle) * overlap/2
- other.x += math.cos(collision_angle) * overlap/2
- other.x += math.sin(collision_angle) * overlap/2
- # calculate normals at collision point
- collision_x = self.x + self.radius
- collision_y = self.y + self.radius
- normal_self_dx = abs(self.x - collision_x)
- normal_self_dy = abs(self.y - collision_y)
- normal_self_angle = math.atan2(normal_self_dy, normal_self_dx)
- normal_self_x = math.cos(normal_self_angle) * self.radius
- normal_self_y = math.sin(normal_self_angle) * self.radius
- normal_other_dx = abs(other.x - collision_x)
- normal_other_dy = abs(other.y - collision_y)
- normal_other_angle = math.atan2(normal_other_dy, normal_other_dx)
- normal_other_x = math.cos(normal_other_angle) * other.radius
- normal_other_y = math.sin(normal_other_angle) * other.radius
- # get crossproduct (= angles)
- self_vx = math.cos(math.radians(self.angle)) * self.speed
- self_vy = math.sin(math.radians(self.angle)) * self.speed
- bounce_angle = normal_self_x*self_vy - normal_self_y*self_vx
- self.angle = bounce_angle
- other_vx = math.cos(math.radians(other.angle)) * other.speed
- other_vy = math.sin(math.radians(other.angle)) * other.speed
- bounce_angle = normal_other_x*other_vy - normal_other_y*other_vx
- other.angle = bounce_angle
Add Comment
Please, Sign In to add comment