Advertisement
here2share

# tk_Elastic_Collision.py

Dec 19th, 2022
656
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.14 KB | None | 0 0
  1. # tk_Elastic_Collision.py ZZZ the deflection is often not mirroring properly
  2.  
  3. from tkinter import *
  4. import math
  5. import random
  6.  
  7. window = Tk()
  8. window.title("Elastic Collision")
  9. canvas = Canvas(window, width=500, height=500)
  10. canvas.pack()
  11.  
  12. circles = []
  13. colors = ['red', 'blue', 'green', 'yellow', 'purple']
  14. diameters = [47, 99, 149, 180, 299]
  15. for i in range(5):
  16.     x = random.randint(0, 500)
  17.     y = random.randint(0, 500)
  18.     vx = random.randint(-5, 5)
  19.     vy = random.randint(-5, 5)
  20.     circles.append([x, y, vx, vy, diameters[i] / 2, colors[i]])
  21.  
  22. def update():
  23.     for i in range(len(circles)):
  24.         for j in range(i+1, len(circles)):
  25.             # Calculate distance between centers of circles
  26.             dx = circles[i][0] - circles[j][0]
  27.             dy = circles[i][1] - circles[j][1]
  28.             distance = (dx**2 + dy**2)**0.5
  29.                
  30.             # Calculate overlap between circles
  31.             if distance < circles[i][4] + circles[j][4]:
  32.                 # Calculate the angle of the collision
  33.                 angle = math.atan2(dy, dx)
  34.                
  35.                 # Calculate the mass ratio of the two circles
  36.                 m1 = circles[i][4]**2
  37.                 m2 = circles[j][4]**2
  38.                 mass_ratio = m2 / (m1 + m2)
  39.                
  40.                 # Calculate the initial velocity of the two circles
  41.                 u1 = (circles[i][2] * math.cos(angle)) + (circles[i][3] * math.sin(angle))
  42.                 u2 = (circles[j][2] * math.cos(angle)) + (circles[j][3] * math.sin(angle))
  43.                
  44.                 # Calculate the final velocities of the two circles after the collision
  45.                 v1 = (u1 * (m1 - m2) + 2 * m2 * u2) / (m1 + m2)
  46.                 v2 = (u2 * (m2 - m1) + 2 * m1 * u1) / (m1 + m2)
  47.                
  48.                 # Update the velocities of the two circles
  49.                 circles[i][2] = (v1 * math.cos(angle)) - (circles[i][3] * math.sin(angle))
  50.                 circles[i][3] = (v1 * math.sin(angle)) + (circles[i][3] * math.cos(angle))
  51.                 circles[j][2] = (v2 * math.cos(angle)) - (circles[j][3] * math.sin(angle))
  52.                 circles[j][3] = (v2 * math.sin(angle)) + (circles[j][3] * math.cos(angle))
  53.                
  54.                 # Update the positions of the two circles to prevent overlap
  55.                 overlap = (circles[i][4] + circles[j][4]) - distance
  56.                 circles[i][0] += overlap * math.cos(angle)
  57.                 circles[i][1] += overlap * math.sin(angle)
  58.                 circles[j][0] -= overlap * math.cos(angle)
  59.                 circles[j][1] -= overlap * math.sin(angle)
  60.  
  61.     # Update position of circles using their velocities
  62.     for circle in circles:
  63.         circle[0] += circle[2]
  64.         circle[1] += circle[3]
  65.         # Check if circle has reached the edge of the canvas and reverse its velocity if necessary
  66.         if circle[0] <= circle[4]:
  67.             circle[2] *= -1
  68.             circle[0] = circle[4]
  69.         elif circle[0] >= 500 - circle[4]:
  70.             circle[2] *= -1
  71.             circle[0] = 500 - circle[4]
  72.         if circle[1] <= circle[4]:
  73.             circle[3] *= -1
  74.             circle[1] = circle[4]
  75.         elif circle[1] >= 500 - circle[4]:
  76.             circle[3] *= -1
  77.             circle[1] = 500 - circle[4]
  78.  
  79. while True:
  80.     # Clear the canvas
  81.     canvas.delete("all")
  82.  
  83.     # Draw circles and update their positions
  84.     for circle in circles:
  85.         canvas.create_oval(circle[0] - circle[4], circle[1] - circle[4], circle[0] + circle[4], circle[1] + circle[4], fill=circle[5])
  86.  
  87.     # Check for and handle collisions
  88.     update()
  89.  
  90.     # Refresh the window
  91.     window.update()
  92.  
  93.     # Pause for a short amount of time
  94.     window.after(20)
  95.  
  96. window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement