< Zurück | Inhalt | Weiter >

From HiResCoordTest.java


protected Locale createLocaleEarth( VirtualUniverse u )

{

int[] xPos = { 0, 0, 0, 0, 0, 0, 0, 0 };

int[] yPos = { 0, 0, 0, 0, 0, 0, 0, 0 };

int[] zPos = { 0, 0, 0, 1, 0, 0, 0, 0 };


HiResCoord hiResCoord = new HiResCoord( xPos, yPos, zPos ); return new Locale( u, hiResCoord );

}


image


A HiResCoord is created using three 8−element integer arrays. An int is 32 bits, so 8 * 32 = 256 bits.


NOTE The integer at index 0 contains the most significant bit, while the integer at index 7 contains the least significant bit. The decimal point is defined to lie between indexes 3 and 4, that is,


0x0 0x0 0x0 0x0 . 0x0 0x0 0x0 0x0


The 8−element integer array to specify a coordinate can be considered an 8−digit number in base

232. The numbers that can be expressed by such an entity are mind−boggling, but table 6.1 (from the API specification) can be used to get you into the right ballpark for the quantity you are trying to

express.

Table 6.1 Physical dimensions expressed as a power of 2


2n Meters

Units

87.29

Universe (20 billion light−years)

69.68

Galaxy (100,000 light−years)

53.07

Light−year

43.43

Solar system diameter

23.60

Earth diameter

10.65

Mile

9.97

Kilometer

0.00

Meter

–19.93

Micron

–33.22

Angstrom

–115.57

Planck length

For example, to specify a distance in light−years (9,460 billion kilometers), find from table 6.1 that 253.07 is equal to 9,460 billion kilometers. That is, a 1 at the 53rd bit position will be approximately one light−year. When mapped into the integer array, the 53rd bit is located within the integer at index 3 – 53/32 = 2. It is the 21st bit (53 – 32) within the third integer of the array. The number 221 equals 0x200000, so setting the integer at index 2 of the integer array to 0x200000 will create a HiResCoord instance that stores approximately a light−year.


The integer at index 3 is defined to store meters, so simply setting the integer at index 3 will define a HiResCoord in meters. Conversely, the 20th bit to the right of the decimal point position is approximately a micron.


A useful exercise would be to develop a Java class that returned a HiResCoord for quantities expressed in the various units shown in table 6.1.


//creates a Locale that is positioned (232 + 5) meters

//away from the origin in the +Z direction.

int[]

xPos

=

{

0,

0,

0,

0,

0,

0,

0,

0

};

int[]

yPos

=

{

0,

0,

0,

0,

0,

0,

0,

0

};

int[]

zPos

=

{

0,

0,

1,

5,

0,

0,

0,

0

};


HiResCoord hiResCoord = new HiResCoord( xPos, yPos, zPos );


Once a Locale has been created and positioned, it can be populated with geometry by attaching a

BranchGroup containing scenegraph elements:


//create the Universe

m_Universe = new VirtualUniverse();


//create the position for the Locale

int[]

xPos

=

{

0,

0,

0,

0,

0,

0,

0,

0

};

int[]

yPos

=

{

0,

0,

0,

0,

0,

0,

0,

0

};

int[]

zPos

=

{

0,

0,

1,

5,

0,

0,

0,

0

};

HiResCoord hiResCoord = new HiResCoord( xPos, yPos, zPos );


//create the Locale and attach to the VirtualUniverse Locale locale = new Locale( u, hiResCoord );


//create the BranchGroup containing the Geometry for the scene

BranchGroup sceneBranchGroup = createSceneBranchGroup(); sceneBranchGroup.compile();


//add the scene BranchGroup to the Locale locale.addBranchGraph( sceneBranchGroup );


Note that this code will not yet make the geometry visible until a View and a ViewPlatform are created. This is the subject of the next section.