RigidBodyPhysics Example: Getting Started

From H3D.org

Jump to: navigation, search

Welcome to the very first example of H3DAPI RigidBodyPhysics (RBP), in this example we'll show you how to add physics-enabled simulation to current geometry nodes. The example displays 2 objects: a ground and a panel. When you press 'G', RBP is enabled and the panel (according to law of gravitation) falls down to the ground.

Image:Note-tip.pngThis tutorial refers to the source code. You can download it from SVN at H3D release branch, or find it at H3D/RigidBodyPhysics/examples/gettingstarted/.

Alternatively, you can download the example here.


The example

Step 1: The geometry nodes

  <Transform DEF='BOX-T'>
    <Shape DEF='BOX-S'>
      <Appearance DEF='A'>
        <Material diffuseColor='0.7 0.2 0.4' />
        <FrictionalSurface />
      <Box DEF='BOX-G' size='0.4 0.004 0.4' />
  <Transform DEF='BOX2-T'>
    <Shape DEF='BOX2-S'>
      <Appearance USE='A' />
      <Box DEF='BOX2-G' size='0.7 0.008 0.05' />

BOX is the ground and BOX2 is the panel. Nothing really fancy, let's move on fast to the next step!

Step 2: Adding RigidBodyPhysics

The node to indicate RigidBodyPhysics scope is <RigidBodyCollection>, here you can set the global settings for it. Refer to the Specs for more information. In this example we will use ODE engine and set the gravity force along the y-axis, downward.

  <RigidBodyCollection physicsEngine='ODE' gravity='0 -1 0'>

Next we will define group of collidable objects through <CollisionCollection > and <CollidableShape>

    <CollisionCollection containerField='collider' bounce='0'>
      <CollidableShape DEF='BOX-C' containerField='collidables'>
        <Shape USE='BOX-S' containerField='shape' />
      <CollidableShape DEF='BOX2-C' containerField='collidables'>
        <Shape USE='BOX2-S' containerField='shape' />

The CollisionCollection holds a collection of objects that can be collided against each other.

Next we define our RigidBody objects (within the RigidBodyCollection node)

    <RigidBody DEF='BOX-R' fixed='true'>
      <Geometry USE='BOX-C' containerField='geometry' />
      <Box USE='BOX-G' containerField='massDensityModel' />
    <RigidBody DEF='BOX2-R'>
      <Geometry USE='BOX2-C' containerField='geometry' />
      <Box USE='BOX2-G' containerField='massDensityModel' />

The RigidBody present an rigid object that those physics law can be applied upon.

Another way of looking at it is that while <Shape> gives you the look, <RigidBody> adds the sense of realism to it. And to make sure that the "look" object and the "realistic" object are always coordinately identical, we route from RigidBody's position and orientation to the <Transform>'s translation and rotation (this would be outside of <RigidBodyCollection>:

  <ROUTE fromNode='BOX-R' fromField='position' toNode='BOX-T' toField='translation' />
  <ROUTE fromNode='BOX-R' fromField='orientation' toNode='BOX-T' toField='rotation' />
  <ROUTE fromNode='BOX2-R' fromField='position' toNode='BOX2-T' toField='translation' />
  <ROUTE fromNode='BOX2-R' fromField='orientation' toNode='BOX2-T' toField='rotation' />

So to sum everything up, the <Shape>, <Box> make up the geometry object that you can see, then <RigidBody> creates the realistic object, and <CollidableCollection> groups these realistic objects together and makes sure they can collide with each other.

Step 3: Toggling button

We purposely set enable to false, we now allow user to press 'g' to toggle enable/disable:

  <KeySensor DEF='K' />
  <PythonScript DEF='P' url='eg01.py' />
  <ROUTE fromNode='K' fromField='keyPress' toNode='P' toField='toggleRBP' />

And the python script:

rbc, = references.getValue()
class ToggleRBP(AutoUpdate(SFString)):
  def update(self, event):
    global rbc
    if event.getValue() == 'g' or event.getValue() == 'G':
      val = 1 - rbc.enabled.getValue()
      print 'RBP toggled: ' + str(val)
    return ''
toggleRBP = ToggleRBP()


After this simple example, you now have an idea how to write a simple h3d application with RigidBodyPhysics.


Personal tools
go to