View Single Post
  #2  
Old 03-13-2009, 10:25 AM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
Here is a sample function that takes any sensor object and calculates the speed of it:
Code:
import viz
viz.go()

sensor = viz.add('testtrack_all.dls')

def CalculateSpeed(obj):
	
	#Get current and last position
	current_pos = obj.getPosition()
	last_pos = getattr(obj,'last_pos',None)
	
	if last_pos is None:
		#This is first call, so save position and set speed to 0
		obj.last_pos = current_pos
		obj.speed = 0.0
	else:
		#Compute speed using last position
		obj.speed = vizmat.Distance(current_pos,last_pos) / viz.elapsed()
		obj.last_pos = current_pos
		
	print obj.speed
	
vizact.ontimer(0,CalculateSpeed,sensor)
Reply With Quote