Skip to content

Instantly share code, notes, and snippets.

@dannystaple
Created June 28, 2020 20:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dannystaple/3b6d404bb198bdbc341f8e343e37d368 to your computer and use it in GitHub Desktop.
Save dannystaple/3b6d404bb198bdbc341f8e343e37d368 to your computer and use it in GitHub Desktop.
import vpython
import random
box_thickness = 0.2
box_size = 20
half_size = box_size / 2
vpython.box(length=box_thickness, width=box_size, height=box_size, opacity=0.2, pos=vpython.vector(half_size, 0, 0))
vpython.box(length=box_thickness, width=box_size, height=box_size, opacity=0.2, pos=vpython.vector(-half_size, 0, 0))
vpython.box(length=box_size, width=box_size, height=box_thickness, opacity=0.2, pos=vpython.vector(0, half_size, 0))
vpython.box(length=box_size, width=box_size, height=box_thickness, opacity=0.2, pos=vpython.vector(0, -half_size, 0))
vpython.box(length=box_size, width=box_thickness, height=box_size, opacity=0.2, pos=vpython.vector(0, 0, half_size))
vpython.box(length=box_size, width=box_thickness, height=box_size, opacity=0.2, pos=vpython.vector(0, 0, -half_size))
box_boundary = half_size - box_thickness / 2 - 1
ball_colors = [vpython.color.red, vpython.color.blue, vpython.color.green]
rand_range_start = -box_boundary * 10
rand_range_end = box_boundary * 10
def make_pos():
return random.randrange(rand_range_start, rand_range_end) / 10.0
def check_ball_bounds(ball, direction):
ball.pos += direction
if ball.pos.x >= box_boundary or ball.pos.x <= -box_boundary:
direction.x = - direction.x
if ball.pos.y >= box_boundary or ball.pos.y <= -box_boundary:
direction.y = - direction.y
if ball.pos.z >= box_boundary or ball.pos.z <= -box_boundary:
direction.z = - direction.z
speed = 0.9
balls = [
(
vpython.sphere(
radius=1,
color=random.choice(ball_colors),
pos=vpython.vector(make_pos(), make_pos(), make_pos())
),
vpython.vector(random.choice([speed, -speed]), random.choice([speed, -speed]), random.choice([speed, -speed]))
)
for n in range(139)
]
while True:
vpython.rate(40)
for ball, direction in balls:
check_ball_bounds(ball,direction)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment