I am trying to animate infinitely a sequence of elements using animation paths. I want to be able to update the speed of the elements but I want them always at the same distance one from the others. My goal is to have a road translating toward a subject walking on a treadmill and to update the speed of translation according to the subject's speed. I took as example the code implemented into the Vizard examples folder. I have a first set of elements that translate and stop out of the view of the subject and a set of permanent elements that make an infinite loop of the animation path their are linked to. My problem is that my permanent elements either superimpose or let space between them (when the first element starts the loop of the animation again). I am using animation path events to animate the different animation paths I have.
I am probably wrongly implementing either the animation paths or the animation events. Here is the code I wrote. Can you tell me how can I improve it or if there is any other solution that I can implement?
Code:
import viz
import vizinfo
import vizact
viz.setMultiSample(4)
viz.fov(60)
viz.go()
#Add the ground plane
ground = viz.addChild('ground.osgb')
#Move the viewpoint back
viz.MainView.move([ 0, 0, -4])
#Initialize an array of control points
positions = [[ 2, 0, 0], [ 1, 0, 0], [ 0, 0, 0], [ -1, 0, 0], [ -2, 0, 0], [ -3, 0, 0], [ -4, 0, 0]]
for i in range(0, len(positions)):
# Define the first sequence of elements which will disappear after
# their animation
viz.startlayer(viz.QUADS)
viz.texCoord( 0, 1)
viz.vertex( -0.5, .005, -0.5)
viz.texCoord( 1, 1)
viz.vertex( -0.5, .005, 0.5)
viz.texCoord( 1, 0)
viz.vertex( 0.5, .005, 0.5)
viz.texCoord( 0, 0)
viz.vertex(0.5, .005, -0.5)
exec('QUAD' + str(i) + ' = viz.endLayer()')
exec('BALL' + str(i) + '= viz.addChild("beachball.osgb")')
exec('PATH' + str(i) + ' = viz.addAnimationPath()')
positions1 = positions[i :]
for x, pos in enumerate(positions1):
exec('PATH' + str(i) + '.addControlPoint( x + 1, pos = pos)')
# Add a control point on the second element of PATH0
if (i == 0):
exec('PATH' + str(i) + '.addEventAtControlPoint("positions", 1)')
#Link BALL and QUAD to PATH and play PATH
viz.link(eval('PATH' + str(i)) , eval('BALL' + str(i)))
viz.link(eval('PATH' + str(i)) , eval('QUAD' + str(i)))
exec('PATH' + str(i) + '.play()')
#Definition of permanent elements
viz.startlayer(viz.QUADS)
viz.texCoord( 0, 1)
viz.vertex( -0.5, .005, -0.5)
viz.texCoord( 1, 1)
viz.vertex( -0.5, .005, 0.5)
viz.texCoord( 1, 0)
viz.vertex( 0.5, .005, 0.5)
viz.texCoord( 0, 0)
viz.vertex(0.5, .005, -0.5)
exec('quad' + str(i) + ' = viz.endLayer()')
exec('ball' + str(i) + '= viz.addChild("beachball.osgb")')
exec('path' + str(i) + ' = viz.addAnimationPath()')
exec('positions1' + str(i) + '= positions')
for x, pos in enumerate(eval('positions1' + str(i) )):
#Add a ball at each control point and make it
#semi-transparent, so the user can see where the
#control points are
b = viz.addChild('beachball.osgb',cache=viz.CACHE_CLONE)
b.setPosition(pos)
b.alpha(0.2)
exec('path' + str(i) + '.addControlPoint( x + 1, pos = pos)')
#Set the initial loop mode to circular
exec('path' + str(i) + '.setLoopMode(viz.ON)')
# Add a control point at the second control point of path-i
exec('path' + str(i) + '.addEventAtControlPoint("positions", 1)')
# Link the ball and the quad to the path
viz.link(eval('path' + str(i)) , eval('ball' + str(i)))
viz.link(eval('path' + str(i)) , eval('quad' + str(i)))
def onPointReached():
# Trigger the start of the animation of path0
path0.play()
# Setup callbacks for the PATH0 events
vizact.onPathEvent(PATH0, "positions", onPointReached)
index = 1
def onPointReached1():
# Trigger the start of the following animation path
global index
eval('path' + str(index) + '.play()')
eval('path' + str(index - 1) + '.clearEvents()')
index += 1
# Setup callbacks for the path-i events
for i in range( 0, len(positions) - 1):
vizact.onPathEvent(eval('path' + str(i)) , "positions", onPointReached1)
#Setup path control panel
controlPanel = vizinfo.InfoPanel(text = None, title = 'Settings', icon = False)
slider_speed = controlPanel.addLabelItem('Speed', viz.addSlider())
slider_speed.set(0.1)
def changeSpeed(pos):
#Adjust the speed of the animation path
for i in range(0, len(positions)):
eval('path' + str(i) + '.setSpeed( pos * 10 )')
eval('PATH' + str(i) + '.setSpeed( pos * 10 )')
#Setup callbacks for slider events
vizact.onslider(slider_speed, changeSpeed)