Open topic with navigation
Basic physics
\tutorials\physics\basicPhysics.py
This script demonstrates a simple physics simulation.
"""
A simple physics simulation.
Press spacebar to reset the simulation.
"""
import viz
import vizact
viz.setMultiSample(4)
viz.fov(60)
viz.go()
import vizinfo
vizinfo.InfoPanel(align=viz.ALIGN_LEFT_BOTTOM)
viz.clearcolor( viz.SLATE )
#Turn on the physics engine
viz.phys.enable()
#Add ground
ground = viz.addChild('ground.osgb')
ground.collidePlane() #Make ground collide with objects as if it were an infinite plane
#Create seesaw board
seesaw = viz.addChild('box.wrl')
seesaw.setScale([10,0.3,1])
seesaw.collideBox() #Make object collide as if a bounding box surrounded the object
#Create balance point of seesaw
fulcrum = viz.addChild('tut_cone.wrl')
fulcrum.setScale([0.5,0.5,0.5])
fulcrum.setPosition([0,.01,1])
fulcrum.collideMesh() #Make object collide if its actual geometry intersects another object
fulcrum.disable( viz.DYNAMICS ) #Disables dynamic physics forces from acting on the object
#Create weight on right side of seesaw
load = viz.addChild('duck.wrl')
load.collideBox()
#Create weight on left side of seesaw
counterWeight = viz.addAvatar('duck.cfg')
counterWeight.collideBox(density=5) #The high density parameter makes the object heavier
#Move the viewpoint so that it can see the show
viz.MainView.setPosition([0,2,-10])
#Puts the moving objects in their original state
def reset():
seesaw.reset() #Zeros out all forces
seesaw.setPosition([0,3,5])
seesaw.setEuler([0,0,0]) #Puts object upright
load.reset()
load.setPosition([4,5,5])
load.setEuler([0,0,0])
counterWeight.reset()
counterWeight.setPosition([-4, 11, 5])
counterWeight.setEuler([90,0,0])
#Reset simulation
reset()
vizact.onkeydown(' ', reset)