![]()  | 
	
| 
		 
			 
			#1  
			
			
			
			
			
		 
		
	 | 
|||
		
		
  | 
|||
| 
		
	
		
		
			
			 
				
				Calculating Speed with sensor
			 
			
			
			Hello, 
		
		
		
		
		
		
		
		
	
	Right now I am trying to figure out how to calculate speed with a sensor. I know that speed is the rate of change with respect to time. So change of position / time. I know I need to get the position(pos) of the sensor first. Then i have to have a def: that will check for its new pos then do the calculation - (new pos - old pos) / time. Then after that I need to start over and set the new pos to old pos. The problem im having is trying to establish a pos before the def: starts running. Whenever I test for the old position i keep getting 0.0, 0.0, 0.0. So its going to look something like this: Code: 
	
import viz
viz.go()
PORT_PPT = 6
for x in range(1):
     #Create sensors
     ppt1 = viz.add('vizppt.dls')
     ppt2 = viz.add('vizppt.dls')
     ball1 = viz.add('white_vall.wrl')
     ball1.scale(1,1,1)
     ballLink1 = viz.link(ppt1, ball1)
     ball2 = viz.add('white_vall.wrl')
     ball2.scale(1,1,1)
     ballLink2 = viz.link(ppt2, ball2)
 
     #controls distance in virtual world
     ballLink1.postScale([8,1,13],target = viz.LINK_FULL_OP)
     ballLink2.postScale([8,1,13],target = viz.LINK_FULL_OP)
#I need to check for ball1 pos before here
def speed():
      #need to check for new pos for ball1 here
      #need to somehow get old pos of ball1 so i can do the equation 
      #new pos - old pos /2(=1/2 second)
      if speed > 1:
           #some reaction happens in here
      #need to set old pos = new pos
vizact.ontimer(.5, speed)
Thanks  | 
| 
		 
			 
			#2  
			
			
			
			
			
		 
		
	 | 
|||
		
		
  | 
|||
| 
		
	
		
		
		
		 
			
			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)
 | 
![]()  | 
	
	
		
  | 
	
		
  | 
			 
			Similar Threads
		 | 
	||||
| Thread | Thread Starter | Forum | Replies | Last Post | 
| Creating a Vizard Sensor Plugin | farshizzo | Plug-in development | 25 | 08-01-2019 01:24 AM | 
| speed on animation path | whj | Vizard | 8 | 11-17-2008 08:41 PM | 
| wiimote and sensor bar | masaki | Vizard | 1 | 03-06-2008 04:07 PM | 
| Multiple Copies of same sensor plugin | RedSpikeyThing | Plug-in development | 2 | 02-12-2008 03:10 PM | 
| using sensor data in Physics1.py | Eunice | Vizard | 4 | 01-03-2006 06:18 AM |