WorldViz User Forum  

Go Back   WorldViz User Forum > Vizard

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 07-08-2014, 09:13 AM
Shweta Shweta is offline
Member
 
Join Date: Jun 2014
Posts: 9
Using move() and spinTo efficiently

Hello,

I have created a cloud of triangles using startlayer and endlayer in a scene.
Now I want to smoothly move the viewpoint within the scene.
I do this using move and spinTo.
However, the problem now is that when I use move() and spinTo(), the triangles move along with the scene.
I want the triangles to remain fixed, which happens when we use mouse to navigate through the view.
Can someone help me with this?
Kindly find below my code:

import viz
import struct
import vizact
import vizinput
import random

viz.go()

import socket


#The maximum amount of data to receive at a time
MAX_DATA_SIZE = 1024

#The port to send/receive data on
PORT = 4999
PORT_SEND = 5000

#Get the name of this computer
COMPUTER_NAME = socket.gethostname()

#Get the IP address of this computer
COMPUTER_IP_ADDRESS = socket.gethostbyname('localhost')

#Create a socket to send data over
OutSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
OutSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
OutSocket.bind(('',PORT_SEND))
OutSocket.setblocking(0)

#Create a socket to receive data from
InSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
InSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
InSocket.bind(('', PORT))
InSocket.setblocking(0)

#Selection of the scene (Indoor?Outdoor)
choice = vizinput.choose('Select an environment',['indoor','outdoor','none'])
if choice == 0:
sceneIndoor = vizinput.choose('Seect a scene',['court','gallery','maze'])
if sceneIndoor == 0:
scene = viz.add('court.ive')
elif sceneIndoor == 1:
scene = viz.add('gallery.osgb')
else:
scene = viz.add('maze.osgb')

elif choice == 1:
sceneOutdoor = vizinput.choose('Select a scene',['mountains','beach','forest','open ground'])
if sceneOutdoor == 0:
scene = viz.addChild('building.obj')
elif sceneOutdoor == 1:
scene = viz.addChild()
elif scene == 2:
scene = viz.addChild()
elif choice == 2:
pass


#Create a layer.
viz.startLayer(viz.TRIANGLES)
viz.vertexColor(1,0,0)
viz.vertex(0,0,0)
viz.vertexColor(0,1,0)
viz.vertex(0.5,1,0)
viz.vertexColor(0,0,1)
viz.vertex(-0.5,1,0)
myTria = viz.endLayer()
myTria.setEuler(0,180,0)

#def Create_shape():
shapes = []
Number = 5000
for i in range(Number):
x = random.randint(-20,20)
y = random.randint(-20,20)
z = random.randint(-20,20)
yaw = random.randint(0,360)
pitch = random.randint(0,360)
roll = random.randint(0,360)
shape = myTria
shape.setPosition([x,y,z])
shapes.append(shape)

ver1 = shape.getVertex(0)
ver2 = shape.getVertex(1)
ver3 = shape.getVertex(2)

viz.startLayer(viz.TRIANGLES)
viz.vertexColor(1,0,0)
viz.vertex(ver1)
viz.vertexColor(0,1,0)
viz.vertex(ver2)
viz.vertexColor(0,0,1)
viz.vertex(ver3)

x = shape.getPosition()
myTriangle = viz.endLayer()
myTriangle.setPosition(x)
myTriangle.setEuler(0,180,0)



## Code for sending data
def SendData(data):
OutSocket.sendto(data,(COMPUTER_IP_ADDRESS,PORT_SE ND))

## Code for receiving data
def ReceiveData():
try:
return InSocket.recv(MAX_DATA_SIZE)
except socket.error:
#Insert error handling code here
pass

## May change as per requirement
vizact.onkeydown(' ',SendData,'hello there')

def CheckSocket():
#Try to receive data from socket
data = ReceiveData()
if data:
numvalues = (len(data)/ 8)
udp = struct.unpack('>'+'d' * numvalues ,data)

# Assigning the rotation and translation values

yaw = udp[0]
pitch = udp[1]
roll = udp[2]
moveSpeed = udp[3]
xPos = udp[4]
yPos = udp[5]
zPos = udp[6]

if ((yaw == 1) and (pitch == 1) and (roll == 1) and (moveSpeed == 1) and (xPos == 1) and (yPos == 1) and (zPos == 1)):
initPos = vizact.spinTo(euler=[0,0,0], speed = moveSpeed)
scene.addAction(initPos)
else:
index = random.randint(0,1)
if index == 0:
rotation = vizact.spinTo(euler=[yaw,pitch,roll], time = moveSpeed)
scene.addAction(rotation)
else:
rotation = vizact.spinTo(euler=[-yaw,-pitch,-roll], time = moveSpeed)
scene.addAction(rotation)


rotTime = 0;
if xPos != 0:
rotTime = xPos / moveSpeed
elif yPos !=0:
rotTime = yPos / moveSpeed
elif zPos !=0:
rotTime = zPos / moveSpeed
# Check condition to execute translation
if rotTime != 0:
translation = vizact.move([xPos,yPos,zPos],rotTime)
scene.addAction(translation)


print udp
vizact.ontimer(0,CheckSocket)

Thanks in advance.

Kind Regards,
Shweta.
Reply With Quote
  #2  
Old 07-14-2014, 11:31 AM
Jeff Jeff is offline
WorldViz Team Member
 
Join Date: Aug 2008
Posts: 2,471
Please see the guidelines for posting Vizard code. This will make it easier for other users to help with your code.
Reply With Quote
  #3  
Old 07-28-2014, 01:54 AM
Shweta Shweta is offline
Member
 
Join Date: Jun 2014
Posts: 9
Reposting the code :

Code:
import viz
import struct
import vizact
import vizinput
import random

viz.go()

import socket


#The maximum amount of data to receive at a time
MAX_DATA_SIZE = 1024

#The port to send/receive data on
PORT = 4999
PORT_SEND = 5000

#Get the name of this computer
COMPUTER_NAME = socket.gethostname()

#Get the IP address of this computer
COMPUTER_IP_ADDRESS = socket.gethostbyname('localhost')

#Create a socket to send data over
OutSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
OutSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
OutSocket.bind(('',PORT_SEND))
OutSocket.setblocking(0)

#Create a socket to receive data from
InSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
InSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
InSocket.bind(('', PORT))
InSocket.setblocking(0)

#Selection of the scene (Indoor?Outdoor)
choice = vizinput.choose('Select an environment',['indoor','outdoor','none'])
if choice == 0:
sceneIndoor = vizinput.choose('Seect a scene',['court','gallery','maze'])
if sceneIndoor == 0:
scene = viz.add('court.ive')
elif sceneIndoor == 1:
scene = viz.add('gallery.osgb')
else:
scene = viz.add('maze.osgb')

elif choice == 1:
sceneOutdoor = vizinput.choose('Select a scene',['mountains','beach','forest','open ground'])
if sceneOutdoor == 0:
scene = viz.addChild('building.obj')
elif sceneOutdoor == 1:
scene = viz.addChild()
elif scene == 2:
scene = viz.addChild()
elif choice == 2:
pass


#Create a layer.
viz.startLayer(viz.TRIANGLES)
viz.vertexColor(1,0,0)
viz.vertex(0,0,0)
viz.vertexColor(0,1,0)
viz.vertex(0.5,1,0)
viz.vertexColor(0,0,1)
viz.vertex(-0.5,1,0)
myTria = viz.endLayer()
myTria.setEuler(0,180,0)

#def Create_shape():
shapes = []
Number = 5000
for i in range(Number):
x = random.randint(-20,20)
y = random.randint(-20,20)
z = random.randint(-20,20)
yaw = random.randint(0,360)
pitch = random.randint(0,360)
roll = random.randint(0,360)
shape = myTria
shape.setPosition([x,y,z])
shapes.append(shape)

ver1 = shape.getVertex(0)
ver2 = shape.getVertex(1)
ver3 = shape.getVertex(2)

viz.startLayer(viz.TRIANGLES)
viz.vertexColor(1,0,0)
viz.vertex(ver1)
viz.vertexColor(0,1,0)
viz.vertex(ver2)
viz.vertexColor(0,0,1)
viz.vertex(ver3)

x = shape.getPosition()
myTriangle = viz.endLayer()
myTriangle.setPosition(x)
myTriangle.setEuler(0,180,0)



## Code for sending data	
def SendData(data):
OutSocket.sendto(data,(COMPUTER_IP_ADDRESS,PORT_SE ND))

## Code for receiving data	
def ReceiveData():
try:
return InSocket.recv(MAX_DATA_SIZE)
except socket.error:
#Insert error handling code here
pass

## May change as per requirement
vizact.onkeydown(' ',SendData,'hello there')

def CheckSocket():
#Try to receive data from socket
data = ReceiveData()
if data:
numvalues = (len(data)/ 8)
udp = struct.unpack('>'+'d' * numvalues ,data)

#	Assigning the rotation and translation values 

yaw = udp[0]
pitch = udp[1]
roll = udp[2]
moveSpeed = udp[3]
xPos = udp[4]
yPos = udp[5]
zPos = udp[6]

if ((yaw == 1) and (pitch == 1) and (roll == 1) and (moveSpeed == 1) and (xPos == 1) and (yPos == 1) and (zPos == 1)):
initPos = vizact.spinTo(euler=[0,0,0], speed = moveSpeed)
scene.addAction(initPos)
else:	
index = random.randint(0,1)
if index == 0:
rotation = vizact.spinTo(euler=[yaw,pitch,roll], time = moveSpeed)
scene.addAction(rotation)
else:
rotation = vizact.spinTo(euler=[-yaw,-pitch,-roll], time = moveSpeed)
scene.addAction(rotation)


rotTime = 0;
if xPos != 0:
rotTime = xPos / moveSpeed
elif yPos !=0:
rotTime = yPos / moveSpeed
elif zPos !=0:
rotTime = zPos / moveSpeed
#	Check condition to execute translation
if rotTime != 0:
translation = vizact.move([xPos,yPos,zPos],rotTime)
scene.addAction(translation)


print udp
vizact.ontimer(0,CheckSocket)
Reply With Quote
  #4  
Old 07-29-2014, 05:10 PM
Jeff Jeff is offline
WorldViz Team Member
 
Join Date: Aug 2008
Posts: 2,471
It's not possible to copy/paste your code and run it. The indentation has been lost and it seems the example could be simplified. Try and minimize the code so that it can be run directly without requiring a network connection. You might need to store the data in some lists to simulate what's received via the network.
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


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


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