View Single Post
  #12  
Old 01-11-2005, 07:02 AM
Johannes Johannes is offline
Member
 
Join Date: Jan 2005
Posts: 143
Tried your example. Works, but it is only good for the case of one ball. Tried to change it (see below) to fit the more general purpose of more balls and objects:

I added after ball.translate(0,1,0):

collidables=[]
collidables.append(myroom)
collidables.append(ball)



I repaced info = ball.collidingwith(myroom,1) with

for objects in collidables:
#Perform the collision check with the object
info = ball.collidingwith(object,1)

and adjusted the indent.
Does not work.
Best,
Johannes

Here is the full changed Program
import viz
import vizmat

viz.go()

myroom = viz.add('../resources/models/room.wrl')
ball = viz.add('../resources/models/ball.wrl')


myroom.collidemesh()
ball.collidesphere(0.25)
ball.vector = viz.Vector(1,1,1)
ball.translate(0,1,0)

collidables=[]
collidables.append(myroom)
collidables.append(ball)

def moveball():
while 1:
info = ball.collidingwith(myroom,1)
#Perform the collision check with the room
if info.intersected:
#Set the balls new vector to the reflection vector
ball.vector = viz.Vector(vizmat.ReflectionVector(ball.vector,inf o.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
futurePos = pos + (ball.vector * 0.02)
ball.translate(futurePos.get())
viz.waittime(0.01)
viz.director(moveball)
Reply With Quote