ball in a box

p>Download: source archive

This program derived from the demo bounce2.py. Motion is confined to an xy-plane.

You can see a problem with wall penetration in the right screenshot below. This effect is more apparent when you reduce the frame rate (so the ball travels further between frames.) In this case, the frame rate was reduced to 8.

References

VPython

Screenshots

   

collide1.py


01: from visual import *
02: 
03: side = 40
04: thk = 1
05: s2 = 2*side - thk
06: s3 = 2*side + thk
07: gray_red = (1.0,0.7,0.7)
08: gray_blue = (0.7,0.7,1.0)
09: wallR = box (pos=( side, 0, 0), length=thk, height=s2,  width=s3,  color = gray_red )
10: wallL = box (pos=(-side, 0, 0), length=thk, height=s2,  width=s3,  color = gray_red )
11: wallB = box (pos=(0, -side, 0), length=s3,  height=thk, width=s3,  color = gray_blue)
12: wallT = box (pos=(0,  side, 0), length=s3,  height=thk, width=s3,  color = gray_blue)
13: 
14: #scene = display.get_selected()
15: scene.background = (0.5,0.5,0.5)
16: 
17: ball = sphere (color = color.green, radius = 8)
18: ball.mass = 1.0
19: ball.v = vector (-1.5, -2.3, 0.0)
20: ball.z = 30
21: 
22: side = side - thk*0.5 - ball.radius
23: 
24: # Experiment with different values of fps to show sphere penetration of the walls
25: fps = 100
26: dt = 50.0/fps
27: t=0.0
28: while 1:
29:   rate(fps)
30:   t = t + dt
31:   ball.pos = ball.pos + (ball.v)*dt
32:   if not (side > ball.x > -side):
33:     ball.v.x = -ball.v.x
34:   if not (side > ball.y > -side):
35:     ball.v.y = -ball.v.y


Maintained by John Loomis, updated Sun Apr 06 10:42:58 2008