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)