View Single Post
  #2  
Old 03-15-2013, 09:42 AM
Jeff Jeff is offline
WorldViz Team Member
 
Join Date: Aug 2008
Posts: 2,461
Yes, you could try placing a collision shape at the viewpoint location to detect the collision. Then in the collision callback, add a force to the object that was collided with:
Code:
import viz
import vizact
viz.go()

viz.MainView.move([0,-0.8,0])

viz.phys.enable()

ground = viz.add('ground.osgb')
ground.collidePlane()

#Add crates to collide with
crate1 = viz.addChild('crate.osgb',pos=[0.7,0.3,5],scale=[0.6,0.6,0.6])
crate2 = crate1.clone(pos=[0,0.3,5.1],euler=[5,0,0],scale=[0.6,0.6,0.6])
crate3 = crate1.clone(pos=[0.3,0.9,5.1],euler=[-5,0,0],scale=[0.6,0.6,0.6])

crate1.collideBox()
crate2.collideBox()
crate3.collideBox()

crates = [crate1,crate2,crate3]

#Add an invisible cube to detect collisions with the crates
import vizshape
cube = vizshape.addCube(size=0.3)
cube.collideBox()
cube.enable(viz.COLLIDE_NOTIFY)
cube.disable(viz.RENDERING)
cube.disable(viz.DYNAMICS)

#Place the cube at the viewpoint location
def updateCube():
	cube.setPosition(viz.MainView.getPosition())
	cube.setQuat(viz.MainView.getQuat())
	
vizact.onupdate(0,updateCube)

#Apply a force to the crate when the viewpoint
#and invisible cube collides with it
def onCollideBegin(e):
	if e.obj1 == cube and e.obj2 in crates:
		dir = viz.MainView.getMatrix().getForward()
		e.obj2.applyForce(dir,duration=0.1)

viz.callback(viz.COLLIDE_BEGIN_EVENT,onCollideBegin)
Reply With Quote