WorldViz User Forum  

Go Back   WorldViz User Forum > Vizard

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 03-03-2008, 12:18 PM
Jerry Jerry is offline
Member
 
Join Date: Jun 2004
Posts: 105
velocity conversion

I am moving an object using the vizact.move command which takes
meters/sec as an argument for speed.
How would I convert the meters/sec velocity to a screenpixels/sec velocity?
Reply With Quote
  #2  
Old 03-03-2008, 12:51 PM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
If the object is added to the screen (viz.ORTHO), then the units will already by in pixels/sec. If the object is added to the world (viz.WORLD), then it gets more complicated. Is your object part of the screen or world?
Reply With Quote
  #3  
Old 03-03-2008, 12:55 PM
Jerry Jerry is offline
Member
 
Join Date: Jun 2004
Posts: 105
The object is in the world. I know it's more complicated - that's
why I know you have the answer.
Reply With Quote
  #4  
Old 03-03-2008, 12:57 PM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
I'm assuming that the viewpoint is fixed while the object is moving? Otherwise it will be REALLY complicated
Reply With Quote
  #5  
Old 03-03-2008, 01:45 PM
Jerry Jerry is offline
Member
 
Join Date: Jun 2004
Posts: 105
Yes, the viewpoint is stationary.
Reply With Quote
  #6  
Old 03-03-2008, 05:27 PM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
Here is a script that creates an action that is similar to vizact.goto() except the speed is in pixels/sec. Run the script and press space bar to run the action.
Code:
import viz
viz.go()

class MovePixelSpeedAction(viz.ActionClass):
	
	def convertToPixels(self,pos):
		screen = self.window.worldToScreen(pos)
		screen[0] *= self.size[0]
		screen[1] *= self.size[1]
		screen[2] = 0.0
		return screen
	
	def intersectLine(self,line1,line2):
		o1 = viz.Vector(line1.getBegin())
		o2 = viz.Vector(line2.getBegin())
		d1 = viz.Vector(line1.getDir(),normalize=True)
		d2 = viz.Vector(line2.getDir(),normalize=True)
		c = d1 ^ d2
		clen = c.length()
		if clen == 0.0:
			return o1
		
		#Compute determinant
		v = o2 - o1
		d = (v[0] * (d2[1]*c[2] - d2[2]*c[1])) - (v[1] * (d2[0]*c[2]-d2[2]*c[0])) + (v[2] * (d2[0]*c[1]-d2[1]*c[0]))
		
		d1.setLength(d / (clen * clen))
		return o1 + d1
	
	def begin(self,object):

		self.window = viz.MainWindow
		self.size = self.window.getSize(viz.WINDOW_PIXELS)

		#Get begin/end positions
		beginVal = object.getPosition()
		endVal = vizact._paramlist_(self._actiondata_.pos,object)
		
		#Convert to pixel coordinates
		beginScreenVal = self.convertToPixels(beginVal)
		endScreenVal = self.convertToPixels(endVal)
		
		#Compute distance to travel
		dist = vizmat.Distance(beginScreenVal,endScreenVal)
		if dist == 0.0:
			self.end(object)
			return
			
		#Get velocity
		vel = vizact._paramval_(self._actiondata_.vel,object)
		
		#Calculate duration
		self.duration = dist / vel
		
		#Initialize variables
		self.elapsed = 0.0
		self.endVal = endVal
		self.beginScreenVal = beginScreenVal
		self.endScreenVal = endScreenVal
		self.worldLine = viz.Line(begin=beginVal,end=endVal)
		
	def update(self,elapsed,object):
		
		self.elapsed += elapsed
		p = self.elapsed / self.duration
		if p >= 1.0:
			self._overtime_ = self.elapsed - self.duration
			object.translate(self.endVal)
			self.end(object)
			return
			
		#Interpolate screen pos from screen velocity
		screenPos = vizmat.Interpolate(self.beginScreenVal,self.endScreenVal,p)
	
		#Convert screen location to world vector
		line = self.window.screenToWorld( [screenPos[0]/self.size[0],screenPos[1]/self.size[1]] )
		
		#Intersect vector with world vector of movement path
		pos = self.intersectLine(self.worldLine,line)
		object.translate(pos)
		
def movePixelSpeed(pos,vel):
	bla = viz.ActionData()
	bla.pos = pos
	bla.vel = vel
	bla.actionclass = MovePixelSpeedAction
	return bla
	
START_POS = (0,0,8)
STOP_POS = (0,4,30)

ball = viz.add('ball.wrl',pos=START_POS)

def RunAction():
	ball.translate(START_POS)
	ball.runAction(movePixelSpeed(STOP_POS,100))
	
vizact.onkeydown(' ',RunAction)
Reply With Quote
  #7  
Old 03-03-2008, 06:11 PM
Jerry Jerry is offline
Member
 
Join Date: Jun 2004
Posts: 105
We are all humbled by your brilliance (I mean it!)
Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Face Conversion Integrity nickyee Vizard 3 09-08-2004 08:38 PM
angle conversion DSparrow Vizard 3 04-21-2004 11:41 AM


All times are GMT -7. The time now is 09:44 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Copyright 2002-2023 WorldViz LLC