from visual import *
ball= sphere(pos= (-3,3,3), radius= .5, color=color.blue)
wallR = box(pos=(6,0,0), size =(.1,12,12), color=color.red)
wallS = box(pos=(-6,0,0), size =(.1,12,12), color=color.red)
wallT = box(pos=(0,6,0), size =(12,.1,12), color=color.red)
wallU = box(pos=(0,-6,0), size =(12,.1,12), color=color.red)
wallbak = box(pos=(0,0,-6), size= (12,12,.1), color=color.red)
dt = 0.6
g = .00982
ball.velocity = vector (.1,.1,.5)
bv= (arrow(pos=ball.pos,axis=ball. velocity,color=color.yellow))
ball.trail=curve(color=ball. color)
while(1==1):
ball.pos=ball.pos+ball. velocity*dt
rate(150)
if ball.x>wallR.x-0.5:
ball.velocity.x=-ball. velocity.x
if ball.x>wallS.x-0.5:
ball.velocity.x=-ball. velocity.x
if ball.x>wallT.x-0.5:
ball.velocity.x=-ball. velocity.x
if ball.x>wallU.x-0.5:
ball.velocity.x=-ball. velocity.x
if ball.x>wallbak.x-0.5:
ball.velocity.x=-ball. velocity.x
if ball.z> 5.5:
ball.velocity.z=-ball. velocity.x
if ball.y>-5.5:
ball.velocity.y=ball. velocity.y-g
bv.pos = ball.pos
bv.axis=ball.velocity
ball.trail.append(pos=ball. pos)
there above i have posted this beautiful code.
from the first line i have accessed a lib that allows me to displace visual effects.
the first 10 lines of the code was to define the ball its position and initial speed.
the next 5 lines was made so that we can track the balls velocity with an arrow and the have a line follow the ball to show the balls trail
with in the whole loop we have statements to define the box, stating that if the position of the ball was to become too close to the the wall the velocity is shot backwards creating this "bounce" effect
when the last few lines stating the gravity of the system and how it effects the ball as it bounces, every time it bounce it loses energy and is lowered
No comments:
Post a Comment