Tuesday, October 25, 2011

Python Tutorial

from visual.graph import *

#define values
ball = sphere(pos=(-5,2,-1), radius=.5, color=color.red)
ball2= sphere(pos=(5,2,-1), radius=.5, color=color.orange)
wallR = box(pos=(6,0,0), size=(.5,12,12), color=color.green)
wallL = box(pos=(-6,0,0), size=(.5,12,12), color=color.green)
wallF = box(pos=(0,0,-6), size=(12,12,.5), color=color.green)
wallT = box(pos=(0,6,0), size=(12,.5,12), color=color.green)
wallB = box(pos=(0,-6,0), size=(12,.5,12), color=color.green)
dt = 0.02


ball.velocity = vector(2,3,-5)
ball.trail = curve(color=ball.color)
bv = arrow(pos = ball.pos, axis = ball.velocity, color=color.yellow)

ball2.velocity = vector(-2,3,-5)
ball2.trail = curve(color=ball2.color)
bv2 = arrow(pos = ball2.pos, axis = ball2.velocity, color=color.yellow)

while(1==1):
    rate(50)
 
  
    ball.acceleration = vector(0,-9.81,0)
    ball.pos = ball.pos + ball.velocity*dt + .5*ball.acceleration*dt*dt
    ball.velocity = ball.velocity + ball.acceleration*dt

    ball2.acceleration = vector(0,-9.81,0)
    ball2.pos = ball2.pos + ball2.velocity*dt + .5*ball2.acceleration*dt*dt
    ball2.velocity = ball2.velocity + ball2.acceleration*dt

    if ball.x >= (wallR.x - .75):
        ball.velocity.x = -ball.velocity.x
    if ball.x <= (wallL.x + .75):
        ball.velocity.x = -ball.velocity.x
    if ball.y >= (wallT.y - .75):
        ball.velocity.y = -ball.velocity.y
    if ball.y <= (wallB.y + .75):
        ball.velocity.y = -ball.velocity.y
    if ball.z <= (wallF.z +.75):
        ball.velocity.z = -ball.velocity.z
    if ball.z >= 5.5:
        ball.velocity.z = -ball.velocity.z

    if ball2.x >= (wallR.x - .75):
        ball2.velocity.x = -ball2.velocity.x
    if ball2.x <= (wallL.x + .75):
        ball2.velocity.x = -ball2.velocity.x
    if ball2.y >= (wallT.y - .75):
        ball2.velocity.y = -ball2.velocity.y
    if ball2.y <= (wallB.y + .75):
        ball2.velocity.y = -ball2.velocity.y
    if ball2.z <= (wallF.z +.75):
        ball2.velocity.z = -ball2.velocity.z
    if ball2.z >= 5.5:
        ball2.velocity.z = -ball2.velocity.z

    if ball.pos.x == ball2.pos.x:
        ball.velocity.x= -ball.velocity.x
        ball2.velocity.x= -ball2.velocity.x
  
      
    bv.pos = ball.pos
    bv.axis = ball.velocity/2
    ball.trail.append(pos = ball.pos)
    bv2.pos = ball2.pos
    bv2.axis = ball2.velocity/2
    ball2.trail.append(pos = ball2.pos)
  
------------------------------------------------------------------

No comments:

Post a Comment