Thread: Stop for... loop View Single Post
#15
01-11-2005, 09:32 AM
 farshizzo 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.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