WorldViz User Forum  

Go Back   WorldViz User Forum > Vizard

Reply
 
Thread Tools Rating: Thread Rating: 2 votes, 5.00 average. Display Modes
  #1  
Old 05-23-2013, 03:20 AM
4711 4711 is offline
Member
 
Join Date: Jan 2010
Posts: 15
I wrote some code myself to solve the problem:

Code:
import math
import viz


# --- REQUIRED FUNCTIONS --- #

def Angle2Euler( angle ):
	
	euler = 0
	
	# Not defined for negative values
	if angle < 0:
		return 0
	
	# Calculate 'euler' depending on quadrant
	else:
		if angle <= 90:
			# Q1
			euler = 90 - angle
		elif angle <= 270:
			# Q2 & Q3
			euler = -( angle-90 )
		else:
			# Q4
			euler = 360 - angle + 90
	
	return euler


def Cart2Pol( x, y, unit='rad' ):
	
	# Radius
	r = math.sqrt( math.pow(x,2) + math.pow(y,2) )
	
	# Angle in [rad]
	t = math.atan2( y ,x )
	
	# Angle in [deg]
	if unit.lower() == 'deg':
		t = math.degrees( math.atan(float(y)/float(x)) )
		if x < 0:
			t += 180 # Q2 & Q3
		elif x > 0 and y < 0:
			t += 360 # Q4
	
	return r, t


def LookAt( Target ):

	# Get current position and orientation
	Pos = viz.MainView.getPosition()
	Ori = viz.MainView.getEuler()
	
	# Define position as 2D point 'P'
	P = [Pos[0], Pos[2]]
	
	# Define target as 2D point 'T'
	T = [Target[0], Target[2]]
	
	# Shift 'P' into origin (= 'P0') and
	# move 'T' accordingly (= 'T0')
	T0 = [T[0]-P[0], T[1]-P[1]]

	# Get location of 'T0' as polar angle
	angle = Cart2Pol( T0[0], T0[1], 'deg' )[1]
	
	# Convert angle to Vizard's Euler style
	euler = Angle2Euler( angle )
	
	# Create rotation action towards target
	rotation = vizact.spinTo( euler=[euler,0,0], time=2 )
	
	# Apply rotation to MainView
	viz.MainView.runAction( rotation )


# --- AN EXAMPLE --- #

# Go!
viz.add('court.ive')
viz.go()

# Add a box behind the MainView
box = viz.add('box.wrl')
box.setPosition([-5,1.7,-10])

# Look at the box
LookAt( box.getPosition() )
This may not be the easiest or most elegant way, but it works!
Reply With Quote
Reply

Tags
fluent, rotation, viewpoint


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
load a texture on a object of a viewpoint Darkmax Vizard 1 11-12-2010 11:52 AM
limiting object rotation Philippe Vizard 5 10-07-2010 04:31 PM
How to make an object fixed in viewpoint coordinate system? Xianshi Xie Vizard 1 06-22-2009 11:31 AM
Object rotation Andrey Vizard 4 06-13-2007 02:49 PM
Child Object Rotation paulgoldberg Vizard 5 09-05-2006 11:33 AM


All times are GMT -7. The time now is 09:03 PM.


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