View Single Post
  #39  
Old 04-04-2006, 10:14 AM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
I tried your script but I didn't see any bullet. I've modified my previous code so that it shoots a bullet from the center of the viewpoint towards the direction of the crosshair. There will always be an apparent offset between the crosshair and the bullet due to binocular vision.
Code:
import viz
viz.go(viz.STEREO)

viz.add('tut_ground.wrl')
viz.clearcolor(viz.GRAY)

crosshair = viz.add(viz.TEXQUAD)
crosshair.texture(viz.add('crosshair.png'))
crosshair.scale(0.01,0.01,0.01)

viz.mouse(viz.OFF)

def ontimer(num):
	line = viz.screentoworld(viz.mousepos())
	v = viz.Vector(viz.get(viz.HEAD_POS))
	dir = viz.Vector(line[3:]) - viz.Vector(line[:3])
	dir.normalize()
	dir *= 0.1 #Place crosshair 1/10th meter in front of user
	v += dir
	crosshair.translate(v.get())
	crosshair.rotatequat(viz.get(viz.VIEW_QUAT))
	
viz.callback(viz.TIMER_EVENT,ontimer)
viz.starttimer(0,0,viz.FOREVER)

bullet = viz.add('white_ball.wrl')

def shoot():
	pos = viz.Vector(viz.get(viz.HEAD_POS))
	dir = viz.Vector(crosshair.get(viz.POSITION)) - pos
	dir.normalize()
	dir *= 10
	bullet.translate(pos.get())
	bullet.goto((pos+dir).get(),3)
vizact.onmousedown(viz.MOUSEBUTTON_LEFT,shoot)
Reply With Quote