I have been playing VPython and I think it is very easy to create a physics
learning tool.
I'd like to know if there is any way to create a derived class from a
VPython object.
I tried to create a derived class, Ball from a shpere, as follows, but it
didn't work.
from visual import *
class Ball(sphere):
def __init__( self, pos, radius, color ):
shpere.__init( self, pos, radius, color )
self.name = 'Ball1'
def setName( self, name ):
self.name = name
def getName( self ):
return self.name
scene.width = scene.height = 500.
scene.x = scene.y = 0
scene.forward = ( -0.2, -0.5, -1 )
scene.autoscale = 0
scene.userzoom = 0
scene.range = ( 15, 15, 15 )
xmax = 10
dx = xmax / 5
grid = []
for x in arange( -xmax, xmax+dx, dx ):
grid.append( curve( pos = [(x, 0, -xmax), (x, 0, xmax)],
color = ( .7, .7, .7 ) ) )
for z in arange( -xmax, xmax+dx, dx ):
grid.append( curve( pos = [(-xmax, 0, z), (xmax, 0, z)],
color = (.7, .7, .7) ) )
ball = Ball(pos=(0,8,0), radius=0.5, color=color.red)
ball.velocity = vector(0,-1,0)
dt = 0.1
while ball.y > ball.radius:
rate(100)
ball.pos = ball.pos + ball.velocity*dt
ball.velocity.y = ball.velocity.y - 9.8*dt
wake = sphere( pos = ball.pos, radius = 0.1, color = color.blue )
When I ran the above script, I got the following error.
Traceback (innermost last)
File
"c:\yjlee\worx\dissertation\python-related\pythonworx\conceptualphysics\free
fall.py", line 3, in ?
class Ball(sphere):
TypeError: __init__() takes exactly 2 arguments (4 given)
I'm not sure if VPython object can be used a parent class because there is
no API document. (I couldn't find any information on the number of
parameters in the VPython object constructor.)
Thanks in advance.
Young-Jin Lee
|