Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_Elastic_Collision.py ZZZ the deflection is often not mirroring properly
- from tkinter import *
- import math
- import random
- window = Tk()
- window.title("Elastic Collision")
- canvas = Canvas(window, width=500, height=500)
- canvas.pack()
- circles = []
- colors = ['red', 'blue', 'green', 'yellow', 'purple']
- diameters = [47, 99, 149, 180, 299]
- for i in range(5):
- x = random.randint(0, 500)
- y = random.randint(0, 500)
- vx = random.randint(-5, 5)
- vy = random.randint(-5, 5)
- circles.append([x, y, vx, vy, diameters[i] / 2, colors[i]])
- def update():
- for i in range(len(circles)):
- for j in range(i+1, len(circles)):
- # Calculate distance between centers of circles
- dx = circles[i][0] - circles[j][0]
- dy = circles[i][1] - circles[j][1]
- distance = (dx**2 + dy**2)**0.5
- # Calculate overlap between circles
- if distance < circles[i][4] + circles[j][4]:
- # Calculate the angle of the collision
- angle = math.atan2(dy, dx)
- # Calculate the mass ratio of the two circles
- m1 = circles[i][4]**2
- m2 = circles[j][4]**2
- mass_ratio = m2 / (m1 + m2)
- # Calculate the initial velocity of the two circles
- u1 = (circles[i][2] * math.cos(angle)) + (circles[i][3] * math.sin(angle))
- u2 = (circles[j][2] * math.cos(angle)) + (circles[j][3] * math.sin(angle))
- # Calculate the final velocities of the two circles after the collision
- v1 = (u1 * (m1 - m2) + 2 * m2 * u2) / (m1 + m2)
- v2 = (u2 * (m2 - m1) + 2 * m1 * u1) / (m1 + m2)
- # Update the velocities of the two circles
- circles[i][2] = (v1 * math.cos(angle)) - (circles[i][3] * math.sin(angle))
- circles[i][3] = (v1 * math.sin(angle)) + (circles[i][3] * math.cos(angle))
- circles[j][2] = (v2 * math.cos(angle)) - (circles[j][3] * math.sin(angle))
- circles[j][3] = (v2 * math.sin(angle)) + (circles[j][3] * math.cos(angle))
- # Update the positions of the two circles to prevent overlap
- overlap = (circles[i][4] + circles[j][4]) - distance
- circles[i][0] += overlap * math.cos(angle)
- circles[i][1] += overlap * math.sin(angle)
- circles[j][0] -= overlap * math.cos(angle)
- circles[j][1] -= overlap * math.sin(angle)
- # Update position of circles using their velocities
- for circle in circles:
- circle[0] += circle[2]
- circle[1] += circle[3]
- # Check if circle has reached the edge of the canvas and reverse its velocity if necessary
- if circle[0] <= circle[4]:
- circle[2] *= -1
- circle[0] = circle[4]
- elif circle[0] >= 500 - circle[4]:
- circle[2] *= -1
- circle[0] = 500 - circle[4]
- if circle[1] <= circle[4]:
- circle[3] *= -1
- circle[1] = circle[4]
- elif circle[1] >= 500 - circle[4]:
- circle[3] *= -1
- circle[1] = 500 - circle[4]
- while True:
- # Clear the canvas
- canvas.delete("all")
- # Draw circles and update their positions
- for circle in circles:
- canvas.create_oval(circle[0] - circle[4], circle[1] - circle[4], circle[0] + circle[4], circle[1] + circle[4], fill=circle[5])
- # Check for and handle collisions
- update()
- # Refresh the window
- window.update()
- # Pause for a short amount of time
- window.after(20)
- window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement