View Single Post
  #5  
Old 03-13-2009, 11:43 AM
Jeff Jeff is offline
WorldViz Team Member
 
Join Date: Aug 2008
Posts: 2,471
To move one of the objects to a new place, you need to randomly select one of your objects and then randomly select one of the available positions to move it to.

Code:
import viz
viz.go()

viz.MainView.move(0,0,-10)

#keep nine positions in an array
pos = [[-4,0,0],[-3,0,0],[-2,0,0],[-1,0,0],[0,0,0],[1,0,0],[2,0,0],[3,0,0],[4,0,0]]

#add objects to an array
box = viz.add('box.wrl')
ball = viz.add('ball.wrl')
whiteball = viz.add('white_ball.wrl')

objects = [box,ball,whiteball]


import random

rand_positions = []
def sceneOn(num):

	global rand_positions
	
	positions = [0,1,2,3,4,5,6,7,8]
	
	if num == 1:
		
		#set objects to 3 different random positions
		rand_positions = random.sample(positions,3)
		
		objects[0].setPosition(pos[rand_positions[0]])
		objects[1].setPosition(pos[rand_positions[1]])
		objects[2].setPosition(pos[rand_positions[2]])

		vizact.ontimer2(2,0,sceneOff)

	elif num == 2:
		
		#choose one object to move randomly and leave others in there places
		
		#first get available positions
		diff_list = [item for item in positions if not item in rand_positions]
		rand_position = random.sample(diff_list,1)[0]
		
		#choose one of the objects
		rand_obj = random.randint(0,2)
		
		#set the randomly chosen object to the new random position
		objects[rand_obj].setPosition(pos[rand_position])
		print 'object ' + str(rand_obj) + ' moved to position ' + str(rand_position)  
		
	viz.MainScene.visible(viz.ON)
	

def sceneOff():
	
	viz.MainScene.visible(viz.OFF)
	vizact.ontimer2(2,0,sceneOn,2)

sceneOn(1)
Reply With Quote