WorldViz User Forum

WorldViz User Forum (https://forum.worldviz.com/index.php)
-   Vizard (https://forum.worldviz.com/forumdisplay.php?f=17)
-   -   Can I apply a force on an object by running into it? (https://forum.worldviz.com/showthread.php?t=4497)

Daithi90 02-16-2013 05:11 PM

Can I apply a force on an object by running into it?
 
I'm trying to create a stack of barrels that will fall apart when the user bumps into them. As is, nothing happens but if i apply force to another object in the barrels' direction they do fall.

I get the feeling it's only something small but as I'm relatively new to Vizard, I'm not too sure.

Jeff 03-15-2013 08:42 AM

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)



All times are GMT -7. The time now is 08:13 AM.

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