Tutorial 11: Spaces

 

 

Spaces are useful for optimizing collision detection in application-specific ways.  They allow you to setup a customized space-partitioning scheme by encapsulating groups of Shapes.  Here are a few important concepts to keep in mind:

 

  • Every Shape is contained within a Space.
  • Spaces can contain Shapes and other Spaces.
  • Every Simulator has a single root Space internally which contains all of its Shapes and possibly other Spaces.
  • By default, all new Solids’ Shapes are added to the Simulator’s root Space.
  • Every Shape within a single Solid must be contained within the same Space.
  • Collision checks are performed among all of a Space’s contents (Shapes and/or Spaces).
  • If two Spaces collide, collision checks are performed between the contents of one Space and the contents of the other Space.
  • If a Shape collides with a Space, collision checks are performed between the Shape and the contents of the Space.

 

Spaces are mainly useful when you have lots of Shapes that are naturally grouped together.  For a simple example, say your application contains ten simulated robots, each using ten Solids (each Solid using a single Shape).  You have two options: leave all the Solids in the Simulator’s root Space, or create a new Space for each robot.  In the first case there would be 100 * 100 collision checks every time step because every Shape needs to be checked against every other Shape.  In the second case, as long as the robots weren’t near each other, there would only be 10 * 10 collision checks per time step because every Space would be checked against the other Spaces.  Obviously, this can save a lot of time.

 

The following examples show a few different ways to setup Spaces.  Each example contains sample code and a diagram showing the Space hierarchy.  Note that Solids are shown in the diagrams, but it is the Shapes that are actually contained within the Spaces.

 

 

Example 1

 

 

 

opal::Simulator* sim = opal::createSimulator();

opal::Solid* solid1 = sim->createSolid();

opal::Solid* solid2 = sim->createSolid();

opal::Solid* solid3 = sim->createSolid();

 

// Add Shapes to the Solids.

 

 

 

 

 

 

 

 

 

 

 

Example 2

 

 

 

opal::Simulator* sim = opal::createSimulator();

opal::Solid* solid1 = sim->createSolid();

opal::Solid* solid2 = sim->createSolid();

opal::Solid* solid3 = sim->createSolid();

 

// Add Shapes to the Solids.

 

opal::Space* space1 = sim->createSpace();

solid2->setSpace(space1);

solid3->setSpace(space1);

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Example 3

 

 

 

opal::Simulator* sim = opal::createSimulator();

opal::Solid* solid1 = sim->createSolid();

opal::Solid* solid2 = sim->createSolid();

opal::Solid* solid3 = sim->createSolid();

opal::Solid* solid4 = sim->createSolid();

 

// Add Shapes to the Solids.

 

opal::Space* space1 = sim->createSpace();

opal::Space* space2 = sim->createSpace();

space2->setParentSpace(space1);

solid2->setSpace(space1);

solid3->setSpace(space2);

solid4->setSpace(space2);

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

OPAL is Copyright © 2004-2005 Alan Fischer, Andres Reinot, and Tyler Streeter