PDA

View Full Version : Road Animation Path


vserchi
11-09-2015, 01:21 PM
Hi,
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?



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_CLON E)
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)

Jeff
11-10-2015, 04:04 PM
To get coding suggestions on the forum, it's best to create the simplest possible example that shows the issue.

vserchi
11-11-2015, 07:38 AM
Sorry my bad. Here is the simplest example I could think about. Even if I am interested only in the motion of the quads, I added also balls to show that my elements superimpose. When I attach a texture to the quads and they superimpose, the resulting effect is a flashing of the overlaid part. Do you have any suggestion?


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]]

#Define a variable to store the animation paths relative to each element
PATH = []

for i in range(0, len(positions)):
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)

quad = viz.endLayer()
ball= viz.addChild("beachball.osgb")

exec('path' + str(i) + ' = viz.addAnimationPath()')
PATH.append(eval('path' + str(i)))

for x, pos in enumerate(positions):
#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_CLON E)
b.setPosition(pos)
b.alpha(0.2)

PATH[i].addControlPoint( x + 1, pos = pos)

#Set the initial loop mode to circular
PATH[i].setLoopMode(viz.ON)

# Add a control point at the second control point of path-i
PATH[i].addEventAtControlPoint("positions", 1)

# Link the ball and the quad to the path
viz.link( PATH[i], ball)
viz.link( PATH[i], quad)

index = 1
def onPointReached1():
# Trigger the start of the following animation path
global index

if index < len(PATH):
PATH[index].play()

index += 1

# Setup callbacks for each animation path
for i in range( 0, len(PATH) - 1):
vizact.onPathEvent( PATH[i], "positions", onPointReached1)

#Play the first animation path
PATH[0].play()

#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(PATH)):
PATH[i].setSpeed( pos * 10 )

#Setup callbacks for slider events
vizact.onslider(slider_speed, changeSpeed)

Jeff
11-11-2015, 04:53 PM
If you're just interested in animating the textures another way of doing this is to modify the texture coordinates over time. See the 'applying textures example' in Teacher in a Book available from our downloads (http://www.worldviz.com/virtual-reality-software-documentation/) page. If you have 3ds Max, you could animate the textures (http://docs.worldviz.com/vizard/#max_uv_animations.htm), export the animation with the OSG model, and control the animation speed in Vizard.

vserchi
11-12-2015, 09:29 AM
Ok I will try that but there still something I do not understand. The texture has to be applied to something, right? In my case this something would be a sequence of quads on the ground. I want to simulate a road translating on the ground. To do that I define an animation path for each quad composing my road. I link each animation path to the others and play them (using viz.FOREVER as attribute to my animation path). If the quads superimpose or separate during the time my script runs, also the texture will do that. Is that correct?