Hi all,
I created two boxes. One box connected to the sensor with 3DOF orientation and the other one connected to the Mainview. I linked the two boxes with function viz.grab(). The mouse's buttons were used to make mainview forward or backward along the direction of mainview. The space key was used to make the boxes back to the original point with positions (0,0,0) and euler (0,0,0). But it did not work. I didn't the reason. If someone know, please tell me. Thanks very much.
Code:
"""
key commands:
r - reset the position and orientation of mainview,
place the mainview at the original point
mouse's left button - move the mainview forward
mouse's right button - move the mainview backward
up array - move the mainview forward
down array - move the mainview backward
4 - move the mainview forward
1 - move the mainview backward
5 - move the mainview up along the +Y axis( increase eyeheight)
2 - move the mainview down along the -Y axis(decrease eyeheight)
"""
import viz
import vizact
import math
import vizinput
viz.setMultiSample(4)
eyeheight=1.65
viz.eyeheight(eyeheight)
#sensor_place=['Sensor facing FORWARD','Sensor facing LEFT']
#place=vizinput.choose('Select Sensor Config',sensor_place)
display_mode=['2D_Fullscreen','Sony Hmz-T1']
i = vizinput.choose('Select Display Mode', display_mode )
if i==0:
viz.go()
else:
viz.go(viz.FULLSCREEN)
import sony
hmd=sony.HMZT1()
#load the 3 space sensor information
import threespace_api as ts_api
device_list, unknown_list = ts_api.getAvailableComPorts()
for device_port in device_list:
com_port, friendly_name, device_type=device_port
if device_type=="USB":
device=ts_api.TSSensor(com_port=com_port)
elif device_type=='DNG':
device=ts_api.TSDongle(com_port=com_port)
#device=dng_device[0]
elif device_type=='WL':
device=ts_api.TSWLSensor(com_port=com_port)
#load the environment model
environment=viz.addChild('gallery.osgb')
#make mouse invisible and activate mainview's collision
viz.mouse.setVisible(viz.OFF)
viz.mouse.setOverride()
viz.MainView.collision(viz.ON)
viz.MainView.collisionBuffer(0.5)
#viz.MainView.gravity(0)
box_scale=0.0001
box_pos=[0,eyeheight,0] #box1 and box2 must be in the same position
box2_sensor=viz.addChild('crate.osgb') #link box2 to 3 space sensor
box2_sensor.setPosition(box_pos)
box2_sensor.setScale([box_scale]*3)
box1_view=viz.addChild('crate.osgb')
box1_view.setPosition(box_pos)
box1_view.setScale([box_scale]*3)
#link mainview to box1 in Vizard
link2=viz.link(box1_view,viz.MainView)
#link box1 to box2
link=viz.grab( box2_sensor, box1_view )
#reset the mainview to the original position
def reset_mainview():
global link,link2
link2.remove()
link.remove()
link=None
link2=None
pos=environment.getPosition()
pos[1]=pos[1]+eyeheight
euler=environment.getEuler()
box1_view.setPosition(pos)
box1_view.setEuler(euler)
box2_sensor.setPosition(pos)
#viz.MainView.setPosition(0,0,0)
#viz.MainView.setEuler(0,0,0)
link2=viz.link(box1_view,viz.MainView,srcFlag=viz.ABS_GLOBAL)
link=viz.grab(box2_sensor,box1_view)
vizact.onkeydown(' ',reset_mainview)
degree=180/math.pi
#link the rotation of mainview to the 3 space sensor.
#def connect_USB_Forward():
# sensor_euler=device.getFiltTaredOrientEuler() #[pitch,yaw,roll] in radians
# yaw=sensor_euler[1]*degree
# pitch=sensor_euler[0]*degree
# roll=sensor_euler[2]*degree
# box2_sensor.setEuler(yaw,-pitch,-roll)
#link the rotation of mainview to the 3 space sensor every frame
def connect():
sensor_euler=device.getFiltTaredOrientEuler() #[pitch,yaw,roll] in radians
yaw=sensor_euler[1]*degree
pitch=sensor_euler[0]*degree
roll=sensor_euler[2]*degree
box2_sensor.setEuler(yaw,pitch,roll)
vizact.ontimer(0,connect)
#move the mainview along the view direction
param=0.02 # the step of increase or decrease distance
def move_forward():
pos=viz.MainView.getPosition()
yaw=viz.MainView.getEuler()[0]*(math.pi)/180
#pos=box1_view.getPosition()
#yaw=box1_view.getEuler()[0]*(math.pi)/180
cos=math.cos(yaw)
sin=math.sin(yaw)
pos[2]=pos[2]+param*cos
pos[0]=pos[0]+param*sin
viz.MainView.setPosition(pos)
def move_backward():
pos=viz.MainView.getPosition()
yaw=viz.MainView.getEuler()[0]*(math.pi)/180
cos=math.cos(yaw)
sin=math.sin(yaw)
pos[2]=pos[2]-param*cos
pos[0]=pos[0]-param*sin
viz.MainView.setPosition(pos)
#move the mainview along y axis
vizact.whilemousedown(viz.MOUSEBUTTON_LEFT,move_forward)
vizact.whilemousedown(viz.MOUSEBUTTON_RIGHT,move_backward)
vizact.onkeydown(viz.KEY_UP,move_forward)
vizact.onkeydown(viz.KEY_DOWN,move_backward)
vizact.onkeydown('4',move_forward)
vizact.onkeydown('1',move_backward)
If I changed the function move_forward() like below, It also didn't work and the mouse's button can't work either.
Code:
def move_forward():
pos=box1_view.getPosition()
yaw=box1_view.getEuler()[0]*(math.pi)/180
cos=math.cos(yaw)
sin=math.sin(yaw)
pos[2]=pos[2]+param*cos
pos[0]=pos[0]+param*sin
box1_view.setPosition(pos)
box2_sensor.setPosition(pos)