johnmahugu

python bouncing ball physics

Jun 3rd, 2015
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.66 KB | None | 0 0
  1. #!/usr/bin/python
  2. from visual import *
  3. import random
  4. from math import sqrt, atan2
  5.            
  6. def collisions(scene):
  7.     #check for colision against other balls
  8.     for a in range(0, len(scene.objects)):
  9.         for b in range(a+1, len(scene.objects)):
  10.             dx = scene.objects[a].pos.x - scene.objects[b].pos.x
  11.             dy = scene.objects[a].pos.y - scene.objects[b].pos.y
  12.             #quick and dirty check to find that objects that are not going to collide
  13.             if dx > 1: continue
  14.             if dy > 1: continue
  15.             #full collision check
  16.             distance = (dx*dx)+(dy*dy) # dont need sqrt atm as distance to check is 1
  17.             if(distance < 1):
  18.                 diff = scene.objects[a].pos-scene.objects[b].pos
  19.                 diff = diff/sqrt(diff.x**2 +diff.y**2)    
  20.                 a_comp= diff*(scene.objects[a].velocity.x * diff.x + scene.objects[a].velocity.y * diff.y)
  21.                 b_comp= diff*(scene.objects[b].velocity.x * diff.x + scene.objects[b].velocity.y * diff.y)
  22.                 scene.objects[a].velocity= scene.objects[a].velocity - a_comp + b_comp
  23.                 scene.objects[b].velocity= scene.objects[b].velocity - b_comp + a_comp                
  24.                 #place second ball so not touching first
  25.                 mid_point= (scene.objects[a].pos + scene.objects[b].pos)/2                                
  26.                 angle = math.atan2(dy, dx)
  27.                 scene.objects[a].pos= mid_point+diff/2*1.0005
  28.                 scene.objects[b].pos= mid_point-diff/2*1.0005
  29.  
  30.  
  31.     #check for collision against area bounds            
  32.     for check in scene.objects:
  33.         if(check.pos.x < -12):
  34.             check.velocity = vector(abs(check.velocity.x), check.velocity.y, 0)            
  35.         elif(check.pos.x > 12):
  36.             check.velocity = vector(-abs(check.velocity.x), check.velocity.y, 0)            
  37.         elif(check.pos.y < -12):
  38.             check.velocity = vector(check.velocity.x, abs(check.velocity.y), 0)
  39.         elif(check.pos.y > 12):
  40.             check.velocity = vector(check.velocity.x, -abs(check.velocity.y), 0)
  41.  
  42. random.seed()
  43. scene = display(title='Bouncy Balls', width=500, height=500, center=(0,0,0), background=(0,0,0))
  44. scene.autoscale = 0
  45. delta=0.1
  46. scene.range = (-25, -25, -25)
  47.  
  48. for i in range(0, 30):
  49.     ball = sphere(pos=(random.randint(-10, 10), random.randint(-10, 10), 0),color=(1, 1, 1),radius=0.5)
  50.     ball.speed = float(random.randint(1, 5))/2
  51.     ball.velocity = vector(cos(radians(i*60)), sin(radians(i*60)), 0)
  52.  
  53. while 1:
  54.     rate(60)
  55.     for ball in scene.objects:
  56.         ball.pos = ball.pos + (ball.velocity*delta*ball.speed)
  57.     collisions(scene)
Advertisement
Add Comment
Please, Sign In to add comment