View Single Post
  #27  
Old 03-08-2006, 01:32 PM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
The whole point of wearing an HMD is that each eye gets a different view of the scene. This is what make the image look 3D. Setting IPD to zero will effectively disable stereo since both eyes will be receiving the same view of the scene.

Here is some sample code that will play the hit animation when the avatar is hit by a bullet. Replace the code with whatever action you need:
Code:
import viz
import vizmat
import math

viz.go(viz.STEREO)

#arrays
avatars = []			#for avatars
bullets = []			#for number of balls

#constants
NUM_BULLETS = 3			#number of balls in the array
nextBullet = 0			#index for the next ball in the array
BULLET_SPEED = 250		#speed in meters/secs of the bullet
ANIMATION = 1			#animation ID
NUM_AVATARS	= 5			#the number of avatars

court = viz.add('terrain1.wrl')
court.scale(0.5,0.5,0.5)
court.rotate(0,1,0,-90)
court.translate(-5,0,-10)
court.appearance(viz.MODULATE)
court.disable(viz.LIGHT0)

#Set eye distance to 0 to show why the bullet goes to the left of the mouse
viz.ipd(0)

#Create six avatars
def startAvatar(location):
	global court
	for i in range(NUM_AVATARS):
		
		newX = -math.cos(-2*i+15) * 2
		newZ = math.sin(i^2+1) * 2	
		male = viz.add('male.cfg')
		male.translate(newX,0,newZ)
		male.rotate(180,0,0)
		avatars.append(male) #Save avatar in list
startAvatar(court)

for x in range(0,NUM_BULLETS):
	#create a bullet that is initially hidden
	bullet = viz.add('ball.wrl')
	bullet.scale(.1,.1,.1)
	bullet.visible(viz.OFF)
	bullet.disable(viz.COLLISION)
	bullet.active = 0
	
	#add the ball to the ball list
	bullets.append(bullet)
	
def shootBullet():
	global nextBullet
	#find the next available ball to shoot
	bullet = bullets[nextBullet]
	nextBullet = (nextBullet + 1) % NUM_BULLETS
	#Calculate the vector of the ball based on the mouse position
	pos = viz.mousepos()
	bulletvector = viz.screentoworld(pos[0]+0.25,pos[1]) #Need to account for stereo
	bullet.vector = viz.Vector(bulletvector[3]-bulletvector[0],bulletvector[4]-bulletvector[1],bulletvector[5]-bulletvector[2])
	bullet.vector.normalize()
	bullet.vector *= BULLET_SPEED
	
	#translate the bullet to the head position
	bullet.translate(viz.get(viz.HEAD_POS))
	
	#make the bullet visible
	bullet.visible(viz.ON)
	
	#mark the bullet as active
	bullet.active = 1

def MoveBullet(bullet,elapsed):
	
	#get the bullet's current position
	pos = bullet.get(viz.POSITION)
	
	#calculate the balls future position based on it's velocity
	futurePos = pos + (bullet.vector * elapsed)
	
	#Check if bullet intersected with anything
	info = viz.intersect(pos,futurePos)
	if info.intersected:
		if info.object in avatars:
			info.object.add(vizact.animation(7))
		bullet.visible(0)
		bullet.active = 0
	
	#Update balls positions
	bullet.translate(futurePos.get())

def mytimer(num):
	
	if num == ANIMATION:
		
		#Calculate elapsed time since last update
		elapsed = viz.elapsed()
		
		#Update each active ball
		for bullet in bullets:
			if bullet.active:
				#Move the ball and check if it has collided with any objects
				MoveBullet(bullet,elapsed)

#Shoot a ball whenever left mousebutton is clicked
vizact.onmousedown(viz.MOUSEBUTTON_LEFT,shootBullet)

#Create callbacks for timer events
viz.callback(viz.TIMER_EVENT,mytimer)

#Start the animation timer, which animates the ball and checks for collisions
viz.starttimer(ANIMATION,0.01,-1)

#Move the head position back
viz.translate(viz.HEAD_POS,0,0,-10)	

#Disable mouse navigation
viz.mouse(viz.OFF)
Reply With Quote