< Zurück | Inhalt | Weiter >

From BoundsTest.java

//use the defaults

ColorCube cube0 = new ColorCube( 1.0 );


image


RESULTS BoundsAutoCompute: true

Collidable: true Pickable: true

Bounds: Bounding box: Lower=–1.0 –1.0 –1.0 Upper=1.0 1.0 1.0 CollisionBounds: null

By default, Shape3D objects are created as Collidable and Pickable, and they autocompute their Bounds. No CollisionBounds are assigned, so if collision detection functionality is required, the collision mode USE_GEOMETRY should be used.


//explicitly set the Bounds using a BoundingBox ColorCube cube1 = new ColorCube( 2.0 ); cube1.setBoundsAutoCompute( false );

Bounds bounds = new BoundingBox( new Point3d( –2, –2, –2), new Point3d( 2, 2, 2 ) );

cube1.setBounds( bounds ); cube1.setCollisionBounds( bounds );


RESULTS BoundsAutoCompute: false

Collidable: true Pickable: true

Bounds: Bounding box: Lower = –2.0 –2.0 –2.0 Upper = 2.0 2.0 2.0

CollisionBounds: Bounding box: Lower = –2.0 –2.0 –2.0 Upper = 2.0 2.0 2.0

By calling setBoundsAutoCompute( false ), the Bounds and CollisionBounds for the

Shape3D object can be manually specified, as one would expect.


//explicitly set the Bounds using a BoundingSphere ColorCube cube2 = new ColorCube( 4.0 ); cube2.setBoundsAutoCompute( false );

bounds = new BoundingSphere( new Point3d( 0, 0, 0 ), 4 ); cube2.setBounds( bounds );

cube2.setCollisionBounds( bounds );


RESULTS BoundsAutoCompute: false

Collidable: true Pickable: true

Bounds: Bounding box: Lower = –4.0 –4.0 –4.0 Upper = 4.0 4.0 4.0

CollisionBounds: Center = (0.0, 0.0, 0.0) Radius = 4.0

Surprisingly, if a BoundingSphere is used to specify the Bounds and CollisionBounds for the Shape3D object, the BoundingSphere will be internally converted to a BoundingBox and used for the Bounds. The CollisionBounds uses the original BoundingSphere, however.


//auto compute, manual collision ColorCube cube3 = new ColorCube( 6.0 ); cube3.setBoundsAutoCompute( true );

bounds = new BoundingBox( new Point3d( –10, –10, –10 ), new Point3d( 10, 10, 10 ) );

cube3.setCollisionBounds( bounds );


RESULTS BoundsAutoCompute: true

Collidable: true Pickable: true

Bounds: Bounding box: Lower = –6.0 –6.0 –6.0 Upper = 6.0 6.0 6.0

CollisionBounds: Bounding box: Lower = –10.0 –10.0 –10.0 Upper = 10.0 10.0 10.0

//auto compute both

ColorCube cube4 = new ColorCube( 6.0 ); cube4.setBoundsAutoCompute( true );


RESULTS BoundsAutoCompute: true

Collidable: true Pickable: true

Bounds: Bounding box: Lower = –6.0 –6.0 –6.0 Upper = 6.0 6.0 6.0 CollisionBounds: null

5.3.1 Bounds and CollisionBounds propagation

There is a final piece to the Bounds story. The scenegraph is a hierarchical data structure, so it makes sense for the Bounds of a parent object to automatically encompass a volume large enough to hold all of its child objects. Java 3D can perform these calculations automatically, as table 5.1 illustrates (from BoundsTest.java).


Table 5.1 Bounds propagation within a branch of the scenegraph


Scenegraph item

Bounds

Autocompute

BranchGroup

null

false

TransformGroup

Bounding Sphere: Center = (0.477, 0.34,

0.23) Radius = 11.00

true

RotationInterpolator

null

true

Group1

Center = (0.0,0.0, 0.0) Radius = 10.39

true

Cube1

Bounding box: Lower = –1.0 –1.0 –1.0

Upper = 1.0 1.0 1.0

true

Cube2

Bounding box: Lower = –2.0 –2.0 –2.0

Upper = 2.0 2.0 2.0

true

Cube3

Bounding box: Lower = –4.0 –4.0 –4.0

Upper = 4.0 4.0 4.0

true

Cube4

Bounding box: Lower = –6.0 –6.0 –6.0

Upper = 6.0 6.0 6.0

true

Cube5

Bounding box: Lower = –6.0 –6.0 –6.0

Upper = 6.0 6.0 6.0

true

Group2

Bounding Sphere: Center = (–0.01,–0.01,0.00) Radius = 8.62

true

PointsArray

Bounding box: Lower = –4.97 –4.98 –5.00

Upper = 4.95 4.96 4.97

true

Group1 contains the five ColorCubes, as created earlier in the section. The largest ColorCube has a BoundingBox of (–6,–6,–6) –> (6,6,6). The radius of the smallest BoundingSphere to enclose the largest ColorCube is therefore radius = sqrt( 62 + 62 + 62) = 10.392. This BoundingSphere is automatically created by Java 3D and assigned to the parent Group (Group1) of the ColorCubes. Note that Group1 has the property setBoundsAutoCompute( true ).


Group2 contains a Shape3D object composed from 200 random points in a PointArray (positioned between –5 and 5 in the x−, y−, and z−axes). Java 3D automatically creates a BoundingBox to enclose the points composing the Shape3D object—approximately: (–5,–5,–5) –> (5,5,5). The BoundingBox is automatically assigned to the Shape3D object containing the PointArray. The Bounds for the Shape3D object are propagated up the scenegraph hierarchy as a BoundingSphere and assigned to Group2. The center of the BoundingSphere is positioned to minimize the radius (in this case approximately 0,0,0). The radius of the BoundingSphere is approximately computed from radius = sqrt( 52 + 52 + 52) = 8.660.

The parent of Group1, Group2, and RotationInterpolator is TransformGroup. TransformGroup combines the Bounds objects for its children to compute its own Bounds. In this case, as the Bounds of the children are all approximately centered at (0,0,0), which is equal to the Bounds of Group1 (which is the largest).


NOTE The top−level parent BranchGroup node has the attribute setBoundsAutoCompute( false ).

//routine to create a Shape3D object made from a point cloud

//of 200 random points protected Group createPoints()

{

Group group = new Group();


final int kNumPoints = 200; final double kRadius = 10.0;

Point3d points[] = new Point3d[kNumPoints];


for( int n = 0; n <kNumPoints; n++ )

{

double randX = (java.lang.Math.random() * kRadius ) – kRadius/2; double randY = (java.lang.Math.random() * kRadius ) – kRadius/2; double randZ = (java.lang.Math.random() * kRadius ) – kRadius/2;


points[n] = new Point3d( randX, randY, randZ );

}


PointArray pointArray = new PointArray( points.length, GeometryArray.COLOR_4 | GeometryArray.COORDINATES );

pointArray.setCoordinates( 0, points );


Shape3D shapePoints =

new Shape3D( pointArray, new Appearance() ); group.addChild( shapePoints );

return group;

}


Note that the propagation of Bounds up the scenegraph hierarchy (from child to parent) does not occur with CollisionBounds. Cube4 has CollisionBounds of (–10,–10,–10) –> (10,10,10) but these do not influence the Bounds of the parent Group1. Surprisingly, the CollisionBounds of Cube4 do not influence the CollisionBounds of the parent Group1 either. It appears that the application programmer is responsible for manually propagating CollisionBounds from child to parent Nodes.