PDA

View Full Version : 3d viewpoint movement


spacefarer
06-30-2006, 02:22 PM
I just tried the script for R2.5 as shown below which was given by Farshid, but it doesn't work with R3. Could anyone tell what's wrong with it, please?

import viz
viz.go()

#Add a ground
viz.add('tut_ground.wrl')

#Get main viewpoint object
view = viz.get(viz.MAIN_VIEWPOINT)

#Create a variable that will tell whether viewpoing is currently moving/turning
view.animating = 0

#Variable that holds current destination value
final = [0,0,0,0]

def rotateview(x,y,z,angle):
#Make sure view is not animating
if not view.animating:
#Compute rotation of turning body 90 degrees to right
xform = viz.Transform(view.get(viz.BODY_MAT))
xform.preRot(x,y,z,angle)
final[:] = xform.getRot()[:]
#Start animating viewpoint
view.spinmask(viz.BODY_ORI) #Spin the body orientation
view.spinto(final,2,viz.TIME) #Spin for 2 seconds
view.animating = 1

def onkeydown(key):
if key == viz.KEY_UP:
#Make sure view is not animating
if not view.animating:
pos = view.get(viz.HEAD_POS)
trans = view.get(viz.BODY_LOOK)
#Round values to nearest integer
final[0] = int(round(pos[0] + trans[0]))
final[1] = int(round(pos[1] + trans[1]))
final[2] = int(round(pos[2] + trans[2]))
#Start animating viewpoint
view.goto(final,1,viz.TIME)
view.animating = 1
elif key == '6': #yaw right
rotateview(0,1,0,90)
elif key == '4': #yaw left
rotateview(0,1,0,-90)
elif key == '8': #pitch up
rotateview(1,0,0,-90)
elif key == '2': #pitch down
rotateview(1,0,0,90)
elif key == '9': #roll right
rotateview(0,0,1,-90)
elif key == '7': #roll left
rotateview(0,0,1,90)

viz.callback(viz.KEYDOWN_EVENT,onkeydown)

def onstop(object, type):
if type == viz.MOVE:
#Viewpoint is done moving, translate it to the final value just to be sure
view.translate(final)
view.animating = 0
elif type == viz.SPIN:
#Viewpoint is done spinning, rotate it to the final value just to be sure
view.rotate(final,viz.BODY_ORI)
view.animating = 0

view.callback(viz.STOP_EVENT,onstop)

Gladsomebeast
07-03-2006, 01:46 PM
I think you found a bug. The spin actions don't do anything when applied to the view.

spacefarer
07-25-2006, 02:12 PM
You may probably find that you have a similar error ("TypeError: spaceturn() takes exactly 2 argumemts (1 given)") when you use SpaceBall. I'd appreciate it if you could fix it. Thanks!

farshizzo
07-25-2006, 02:29 PM
I just tried the script for R2.5 as shown below which was given by Farshid, but it doesn't work with R3. Could anyone tell what's wrong with it, please?The spin actions work fine on viewpoints. The problem is that the STOP_EVENT has been modified in 3.0. Replace your onstop function with the following:def onstop(e):
if e.action == viz.MOVE:
#Viewpoint is done moving, translate it to the final value just to be sure
view.translate(final)
view.animating = 0
elif e.action == viz.SPIN:
#Viewpoint is done spinning, rotate it to the final value just to be sure
view.rotate(final,viz.BODY_ORI)
view.animating = 0
You may probably find that you have a similar error ("TypeError: spaceturn() takes exactly 2 argumemts (1 given)") when you use SpaceBall. I'd appreciate it if you could fix it. Thanks!The spaceball callbacks have also been modified. Here is an example script for 3.0 that uses the spaceball:import viz
import vizspace
viz.go()

viz.add('gallery.ive')

viz.mouse(viz.OFF)

def spacemove(e):
viz.MainView.move(viz.Vector(e.pos)*e.elapsed)

def spacerot(e):
viz.MainView.rotate(1,0,0,e.ori[0]*e.elapsed,viz.HEAD_ORI,viz.RELATIVE_LOCAL)
viz.MainView.rotate(0,1,0,e.ori[1]*e.elapsed,viz.HEAD_ORI,viz.RELATIVE_LOCAL)
viz.MainView.rotate(0,0,1,e.ori[2]*e.elapsed,viz.HEAD_ORI,viz.RELATIVE_LOCAL)

def spacebuttondown(button):
print 'Button',button,'pressed'

def spacebuttonup(button):
print 'Button',button,'released'

viz.callback(vizspace.TRANSLATE_EVENT,spacemove)
viz.callback(vizspace.ROTATE_EVENT,spacerot)
viz.callback(vizspace.BUTTONDOWN_EVENT,spacebutton down)
viz.callback(vizspace.BUTTONUP_EVENT,spacebuttonup )

spacefarer
07-25-2006, 03:03 PM
Thanks! It seems it works fine.
But when I use both SpaceBall and eMagin's HMD, only translation of SpaceBall input is working...:confused:

farshizzo
07-25-2006, 03:06 PM
Are you sure you haven't accidentally disabled rotations in the Spaceball Control Panel?

spacefarer
07-25-2006, 03:21 PM
I guess so. I just add the following two lines to your script, and tested comment/uncomment them. When you uncomment them, the head tracker of the HMD works, but rotation of SpaceBall doesn't work. If you comment them, SpaceBall works in 6DOF.
---
visor = viz.add( 'eMagin.dls' )
viz.link( visor, viz.MainView )
---

farshizzo
07-25-2006, 03:24 PM
The reason it doesn't work is because you are linking the emagin to the main view. So the rotations of the spaceball are being overridden by the rotations of the emagin.

farshizzo
07-25-2006, 03:31 PM
Forgot to mention, if you want to use the spaceball together with the emagin, then replace viz.HEAD_ORI with viz.BODY_ORI in the spacerot function

spacefarer
07-25-2006, 03:37 PM
That's the easiest solution! It works perfect!! Thanks a lot!!!