View Single Post
  #8  
Old 03-20-2006, 02:36 PM
betancourtb82 betancourtb82 is offline
Member
 
Join Date: Jan 2006
Posts: 103
I wanted to follow up with this issue. I'm having a bit of a problem with my code. Everything works according to plan, however, if I close my left eye and try to look through only my right eye, the mouse does not show accurately. The mouse cursor is being used as an "aiming" device and bullets shoot from the point where the mouse is pointing. When I aim with my left eye, I can aim at the target and shoot fine, but when I close the left eye and aim with my right eye, even if I point directly at the target, the bullet goes through the target and nothing happens to the avatar. Here is my code:
Code:
import viz
import vizmat
import math
import time
viz.go(viz.STEREO | viz.TRACKER | viz.FULLSCREEN)
#commands to enable HMD tracking
if viz.get(viz.TRACKER):
	v = viz.add('intersense.dls')
	#pos = viz.add('vizppt.dls')
	viz.tracker()
	v.reset()
	tracking = 1
else:
	tracking = 0
	viz.collision(viz.ON)

viz.translate(viz.HEAD_POS,0,0,-1)

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

#arrays
avatars = []			#for avatars
bullets = []			#for number of balls
dead_list = []			#list for dead avatars

#constants
NUM_BULLETS = 3				#number of balls in the array
BULLET_SPEED = 250			#speed in meters/secs of the bullet
rotRight = viz.Transform()	#bullet rotation
rotRight.setRot(0,1,0,-90)
ANIMATION = 1				#animation ID
NUM_AVATARS	= 6				#the number of avatars
TIME_BETWEEN_AVATARS = 5	#time between each avatar
START_AVATAR = 0			#flag to start avatars
nextAvatar = 0 				#index for avatars
nextBullet = 0				#index for the next ball in the array
ACTIVE_AVATARS = 5			#create a flag to reset array
index = NUM_AVATARS			#looping index
nextAvatar = 0
theta = (math.pi / 3)

sunlight = viz.add(viz.LIGHT)
sunlight.position(10,10,-20)
sunlight.direction(-0.5,-0.5,1)
sunlight.color(1,1,1)

sky = viz.add('skydome.dlc',0,'',15)
env = viz.add(viz.ENVIRONMENT_MAP,'sky.bmp')
sky.texture(env)
sky.disable(viz.FOG)

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)

#Add a crosshair
crosshair = viz.add(viz.TEXQUAD,viz.SCREEN)
crosshair.texture(viz.add('crosshair.tif'))


#print 'i\t\t\t\t\t''x\t\t\t\t\t\t''z'

#Create six avatars
for i in range(NUM_AVATARS):
	headpos = viz.get(viz.HEAD_POS) 
	newX = -10 * math.sin(theta * i)
	newZ = 10 * math.tan(theta * i)
	if newZ <=10:
		newZ = -17
	else:
		newZ = 10 * math.tan(theta * i)
	#print i,"\t\t\t\t\t",newX,"\t",newZ
	male = viz.add('male.cfg')
	male.scale(1.25,1.25,1.25)
	male.translate(newX,-10,newZ)
	male.rotate(180,0,0)
	avatars.append(male) #Save avatar in list

#WalkForever = vizact.sequence(vizact.walkto(vizact.randfloat(-2,5),0,vizact.randfloat(-10,5)),viz.FOREVER)

def walkAvatar(starter):
	global court
	walk = starter.walkto(0,court,-10.5,1,260,2)
	#Clear all the avatar's actions, reposition it, and start the walk action
	starter.clear(viz.ALL)
	starter.act(walk)


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 RemoveAvatar(avatar):
	
	avatar.visible(0) #Hide avatar
	avatar.speed(1) #Restore speed
	avatars.remove(avatar) #Remove from list
	dead_list.append(avatar) #Add to dead list

def RestoreAvatar():
#	global court
	x = vizmat.GetRandom(-10,10)
	z = vizmat.GetRandom(-20,10)
	if len(dead_list):
		avatar = dead_list.pop() #Ressurrect dead avatar
		avatar.visible(1) #Make it visible
		avatars.append(avatar) #Add it to avatar list
		avatar.translate(x,0,z)
		walkAvatar(avatar)

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:
			WaitThenFreeze = vizact.sequence( vizact.waittime(info.object.getduration(7)-0.05), vizact.speed_node(0) )
			info.object.execute(7)
			info.object.clear(viz.ALL)
			info.object.clear(viz.CURRENT_ACTION)
			info.object.add(WaitThenFreeze) #Add the action to the avatar
			RemoveAvatarAction = vizact.call(RemoveAvatar,info.object)
			info.object.add(RemoveAvatarAction) #Add action to remove avatar

		bullet.visible(0)
		bullet.active = 0
	
	#Update balls positions
	bullet.translate(futurePos.get())
def mytimer(num):
	global avatars
	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)

		#Update the crosshair position
		crosshair.translate(viz.mousepos()+[0])

	elif num == START_AVATAR:
		global nextAvatar
		#Get the next available avatar and start it
		avatar = avatars[nextAvatar]
		walkAvatar(avatar)
		nextAvatar = (nextAvatar + 1) % len(avatars)
		print 'nextAvatar is', nextAvatar
		viz.starttimer(START_AVATAR,TIME_BETWEEN_AVATARS)

#Restore next available avatar
vizact.onkeydown(' ',RestoreAvatar)

#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 a timer which starts the next available avatar
viz.starttimer(START_AVATAR,0.1)

#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)
#viz.cursor(0)
Reply With Quote