WorldViz User Forum  

Go Back   WorldViz User Forum > Vizard

 
 
Thread Tools Rating: Thread Rating: 2 votes, 5.00 average. Display Modes
Prev Previous Post   Next Post Next
  #15  
Old 01-11-2005, 10:32 AM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
Hi,

Try the following script. I changed it so that every time the spacebar is pressed a new ball is created in the room with a random velocity direction. This script will only work with version 2.5 and above. It uses a list of collidable objects similar to what you are using. I hope this is similar enough to what you are doing, if not let me know.
Code:
import viz
import vizmat
import time
viz.go()

#List of collidable objects
collidable = []

#Create a room
myroom = viz.add('room.wrl')
myroom.collidemesh()
collidable.append(myroom)

def moveball(ball):
	
	#Used to calculate elpased time between each iteration
	tick = time.clock()
	
	while 1:
	
		for object in collidable:
			
			#Don't check for collisions with the same object
			if object == ball:
				continue
				
			#Perform the collision check with the room
			info = ball.collidingwith(object,1)
			if info.intersected:
				
				#Set the balls new vector to the reflection vector
				ball.vector = viz.Vector(vizmat.ReflectionVector(ball.vector,info.normalVector))
				
				#Create a vector of the normal of the collision
				normal = viz.Vector(info.normalVector)
			
				#Check dot product of velocity and normal
				if ball.vector * normal < 0:
					ball.vector *= -1
			
			#Get the balls current position
			pos = ball.get(viz.POSITION)
			
			#Calculate the balls future position based on its velocity and elapsed time
			curtick = time.clock()
			futurePos = pos + (ball.vector * (curtick-tick))
			tick = curtick
			ball.translate(futurePos.get())
		
		viz.waittime(0.01)

def onkeydown(key):
	if key == ' ':
		#Create a ball with random velocity vector
		ball = viz.add('ball.wrl')
		ball.collidesphere(0.25)
		ball.vector = viz.Vector(vizmat.GetRandom(-1,1),vizmat.GetRandom(-1,1),vizmat.GetRandom(-1,1))
		ball.vector.normalize()
		ball.vector *= 2
		ball.translate(0,1,0)
		collidable.append(ball)
		viz.director(moveball,ball)
		
viz.callback(viz.KEYDOWN_EVENT,onkeydown)
Reply With Quote
 


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 01:10 PM.


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