WorldViz User Forum  

Go Back   WorldViz User Forum > Vizard

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 03-08-2006, 10:06 AM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
Hi,

The code I posted was made with the latest internal version of Vizard. I realized that it won't work with the version you have. You just need to change the screentoworld command to the following:
Code:
bulletvector = viz.screentoworld(pos[0]+0.25,pos[1])
Also, I made this script to show the bullet coming from the mouse. I didn't add any picking to it, so nothing will happen to the avatars when you click them. As I already mentioned, this will only work when you set IPD to 0. However, having an IPD of 0 will make your HMD useless since you won't be receiving stereo images.
Reply With Quote
  #2  
Old 03-08-2006, 12:41 PM
betancourtb82 betancourtb82 is offline
Member
 
Join Date: Jan 2006
Posts: 103
What do you mean by useless? I tried using the code w/the HMD and the bullet came exactly from the mouse position. Is there a way to make them perform an action when they are hit by a bullet? I wouldn't mind keeping the IPD at zero as long as the avatars would fall when they got hit.
Reply With Quote
  #3  
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
  #4  
Old 03-08-2006, 02:03 PM
betancourtb82 betancourtb82 is offline
Member
 
Join Date: Jan 2006
Posts: 103
Thank you very much!
Reply With Quote
  #5  
Old 03-09-2006, 02:26 PM
betancourtb82 betancourtb82 is offline
Member
 
Join Date: Jan 2006
Posts: 103
Ok, I tried adding the wait then freeze, instead of just having the avatar fall and get back up, but for some reason, it's not working. What does the underscore in speed_node(0) mean?

Code:
	info = viz.intersect(pos,futurePos)
	if info.intersected:
		if info.object in avatars:
			#viz.starttimer(FREEZE_AVATAR,info.object.getduration(7)-0.1)
			WaitThenFreeze = vizact.sequence( vizact.waittime(info.object.getduration(7)-0.2), vizact.speed_node(0) )
			info.object.add(WaitThenFreeze) #Add the action to the avatar
			#info.object.add(vizact.sequence(vizact.waittime(no
		bullet.visible(0)
		bullet.active = 0
Reply With Quote
  #6  
Old 03-09-2006, 03:08 PM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
Hi,

You are not actually executing any animations on the avatar. You should add the following line before adding the WaitThenFreeze action:
Code:
info.object.execute(7)
Reply With Quote
  #7  
Old 03-20-2006, 01:22 PM
betancourtb82 betancourtb82 is offline
Member
 
Join Date: Jan 2006
Posts: 103
Ok, everything works great so far. The only problem is that I am getting some kind of traceback error.

Code:
Traceback (most recent call last):
  File "Test2WithEyeAdjustment.py", line 185, in mytimer
    avatar = avatars[nextAvatar]
IndexError: list index out of range
This occurs whenever I shoot an avatar and he disappears. When I double click on the error it highlights:
Code:
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)
		
	elif num == START_AVATAR:
		global nextAvatar
		#Get the next available avatar and start it
		avatar = avatars[nextAvatar]
		#favatar = favatars[nextAvatar]
		walkAvatar(avatar)
		#walkAvatar(favatar)
		nextAvatar = (nextAvatar + 1) % NUM_AVATARS
		viz.starttimer(START_AVATAR,TIME_BETWEEN_AVATARS)
#Create callbacks for timer events
viz.callback(viz.TIMER_EVENT,mytimer)
When I shoot the avatars, this function is called:
Code:
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:
			#viz.starttimer(FREEZE_AVATAR,info.object.getduration(7)-0.1)
			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())
#Shoot a ball whenever left mousebutton is clicked
vizact.onmousedown(viz.MOUSEBUTTON_LEFT,shootBullet)
What does the error mean? Can it be fixed
Reply With Quote
  #8  
Old 03-21-2006, 10:47 AM
betancourtb82 betancourtb82 is offline
Member
 
Join Date: Jan 2006
Posts: 103
New question, similar problem. I have now been told that the avatars must appear with different faces, based on an input. Here is what I have done so far:

Code:
#define a choice variable
choice = viz.input("Please input the condition:")
global facepic
if choice == 1:
	facepic = 'biohead_talk.vzf'
else:
	facepic = 'morph_head.vzf'

#Create six avatars
for i in range(NUM_AVATARS):
	newX = -10 * math.sin(theta * i)
	newZ = 10 * math.tan(theta * i)
	if newZ <=10:
		newZ = -15
	else:
		newZ = 10 * math.tan(theta * i)
	#print i,"\t\t\t\t\t",newX,"\t",newZ
	male = viz.add('male.cfg')
	male.face(facepic)
	male.scale(1.25,1.25,1.25)
	male.translate(newX,-10,newZ)
	avPos = male.get(viz.POSITION)
	print 'original avatar position', avPos 
	male.rotate(180,0,0)
	avatars.append(male) #Save avatar in list
Now, when I shoot the avatars, they fall and disappear, as planned. The problem I'm having is that the face still stays on the screen.

Here is the code for removing the avatars once shot:
Code:
	#Check if bullet intersected with anything
	info = viz.intersect(pos,futurePos)
	if info.intersected:
		print 'intersect point is',info.intersectPoint
		if info.object in avatars:
			WaitThenFreeze = vizact.sequence( vizact.waittime(info.object.getduration(7)-0.005), 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
Is there a reason why the face stays floating in the air. I'm assuming it should disappear with the body. Please help.

Thanks
Reply With Quote
  #9  
Old 03-30-2006, 01:07 PM
betancourtb82 betancourtb82 is offline
Member
 
Join Date: Jan 2006
Posts: 103
Earilier in this forum, when we discussed the crosshair, you mentioned that we shouldn't be using objects that are attached to the screen when using stereo. I spoke to Mattias this weekend at the VR conference in Alexandria, VA and he mentioned that if we use a 3d object, we would probably be able to fix the problem of the bullets (mentioned in above posts) without having to change the IPD. Is this the case. Is there some way of making a "3d crosshair" that will accurately point to where the bullet would go? If so, do you have any ideas on how this can be implemented? I was thinking of making a vector that would match the vector of the bullet and place an icon (crosshair) at some point on the vector, in 3d, to show an aiming point. How does this idea sound? Can it be done? Please help.

Below is the code I have for the bullet so far.
Code:
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):
	global avatarsHit
	#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:
		avatarsHit = avatarsHit + 1
		print 'Avatars hit: ', avatarsHit
		print 'intersect point is',info.intersectPoint
		if info.object in avatars:
			WaitThenFreeze = vizact.sequence( vizact.waittime(info.object.getduration(7)-0.005), 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())
Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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 03:15 AM.


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