Wednesday, September 19, 2012

Simulate Circular Motion using Visual Python


This little code below show how to move an object in VPython. There’s two object in program, a box and a sphere. The latter object move in circular using simple sin and cos formula. 

I set the angular speed 10 and radius of the motion (not the sphere) is 10. I use label to show sphere’s x and y position. 

Here the result





Here the code

from visual import *

kotak = box(pos=vector(0, 0, 0), length=14, height=14, width=1, color = (0.7,0.7,0.7))

ball=sphere(color=color.green,radius=5)
ball.pos=vector(10,10,0)

timestep = 0.05
a=10
t = .0
omega= 10
px = label(pos=(0,-1,0), text='x = %1.5f' % pi)
py = label(pos=(0,+1,0), text='y = %1.5f' % pi)
while (1==1):
    rate(100)
    ball.pos.x = a*sin(omega*t)
    ball.pos.y = a*cos(omega*t)
    px.text = 'x = %1.5f' % ball.pos.x
    py.text = 'y = %1.5f' % ball.pos.y
    t=t+0.01