Vizard 7 » Tutorials & Examples » Physics engine » Tutorial: Forces & material properties
7.7

Tutorial: Forces and Material Properties

The forcesAndMaterials.py script collides a box and ball into each other through the application of forces to their physics shapes.  It also allows the run-time editing of their physics shapes' properties: density, friction, hardness, and bounce.  This tutorial steps through code found in that script.

 

Open up the script forcesAndMaterials.py from the \tutorials\physics directory.

 

With every press of the spacebar, the box and ball positions are reset and their physics shapes reinitialized with the property values specified by the sliders. In the reset() function you'll see the following lines of code.

#Tell the box to collide as if it were a box and get the handle to its physics object
box.collideNone() #remove existing physical shapes
boxPhysicalShape = box.collideBox() #create new physical shape

The <node3d>.collideNone() call is necessary to delete the current physics shape.  If this was not done, each call to <node3d>.collideBox() would accumulate physics shapes, becoming heavier and heavier.  We are recreating the box physics shape object every reset because the scale of the box may have been changed.  We also save the returned VizPhysicsShape object in a variable so that we can modify its properties later.

#Get the material properties from the sliders
boxPhysicalShape.density = boxDensity.get()
boxPhysicalShape.friction = boxFriction.get()
boxPhysicalShape.hardness = boxHardness.get()
boxPhysicalShape.bounce = boxBounce.get()

The above code retrieves the position of the sliders with <node3d:GUI:slider>.get() and sets the VizPhysicsShape object's appropriate properties.  

 

We now apply the force that will cause the box to move towards the ball.

#Place object on collision course with speed determined by the force slider
box.applyForce( dir = [ -10 * boxForce.get(), 0, 0 ], duration=0.1, pos = boxStartPOS )

The dir parameter is the force vector.  Force vectors determine the power and the direction of the force.  The duration argument is how long the force will be applied.  The pos parameter is the origin of force vector.

 

The rest of the code in the reset() function sets the commands for the ball.

Basic physics concepts

Forces and material properties

Callbacks and complex shapes