User:Dneilans
From GeoMod
- Here is my ball bouncing off of two walls. It took me a minute to figure out the two if statements in respect to -velocity always switches direction. Its not so much an absolute + is right and - is left, but more of a switch.
from visual import *
floor = box(length=8, height=0.5, width=8, color=color.blue) wallleft = box(pos=(-4,4,0), length=.5, height=8, width=4, color=color.blue) wallright = box(pos=(4,4,0), length=.5, height=8, width=4, color=color.blue)
ball = sphere(pos=(0,8,0), color=color.red) ball.velocity = vector(-2,1,0)
dt = 0.0001 while 1:
rate = 100
ball.pos = ball.pos + ball.velocity*dt
if ball.y < 1:
ball.velocity.y = -ball.velocity.y
else:
ball.velocity.y = ball.velocity.y - 9.8*dt
if ball.x < -4 :
ball.velocity.x = -ball.velocity.x
if ball.x > 4:
ball.velocity.x = -ball.velocity.x

