Spring 2008 Example 1
From GeoMod
Program for a falling ball. You can directly copy and post the code.
The ball starts at position x=1, y=0, z=10 and falls until it hits the x-axis (x=0).
Using the velosity and acceleration equations
- v2 = v1 + g * (dt)
- s2 = s1 + v2 * dt
from visual import *
#changing the scene view and lighting (you don't have to worry about this.
scene.forward = (0, -0.5,1)
scene.up = (0,0,1)
scene.forward = (0, 1,0)
scene.lights = [vector(0, -10, 0)]
#show x, y and z axis
xaxis = curve(pos=[ (10, 0, 0), (0, 0,0)], color=(1,0,0))
yaxis = curve(pos=[ (0, 10, 0), (0, 0,0)], color=(0,1,0))
zaxis = curve(pos=[ (0, 0, 10), (0, 0,0)], color=(0,0,1))
#create the ball
ball = sphere(pos=(1,0,10), color=(1,0,0))
#define constants and initial conditions
g = -9.8
dt = 0.01
vel = 0.0
t = 0
#loop to bounce the ball
while (ball.pos.z > 0):
rate(20)
t = t + dt
print t
vel = vel + g * dt
ball.pos.z = ball.pos.z + vel * dt

