Thread: Timing
View Single Post
  #2  
Old 10-18-2007, 10:24 AM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
Here is a sample script that waits for the left mouse button to be clicked then moves a ball while printing out the current time in milliseconds and its current position. Let me know if anything is unclear.
Code:
import viz
viz.go()

START_POS = [-4,2,10]
STOP_POS = [4,2,10]

ball = viz.add('ball.wrl')
ball.translate(START_POS)

def RecordTimer():
	"""Prints out current time in milliseconds followed by ball position"""
	currentTime = viz.tick()
	pos = ball.getPosition()
	print '%d %.4f %.4f %.4f'%(currentTime * 1000,pos[0],pos[1],pos[2])

import viztask

def RunSimulation():
	
	#Repeat simulation indefinitely
	while True:
		
		#Wait for left mouse button to be pressed
		yield viztask.waitMouseDown(viz.MOUSEBUTTON_LEFT)
		
		#Place ball in starting position
		ball.translate(START_POS)
		
		#Start timer to record ball position
		recordTimer = vizact.onupdate(0,RecordTimer)
		
		#Wait for ball to finish move action
		yield viztask.runAction(ball,vizact.moveTo(STOP_POS,speed=2))
		
		#Stop timer
		vizact.removeEvent(recordTimer)
	
viztask.schedule( RunSimulation() )

#Disable mouse navigation
viz.mouse(0)
Reply With Quote