Java Programing

January 29, 2007

For example, instead of creating a new BoundingBox,

Filed under: Java 3D Programming — webmaster @ 5:20 am

2. Initialize is called by Java 3D. 3. WakeUpcondition is set to be WakeupOnElapsedTime( n milliseconds ). 4. processStimulusis called after n milliseconds. 5. The Appearanceattributes are modified for the Shape3D. 6. The WakeUpcondition is set to WakeupOnElapsedFrames( 1 ). 7. processStimulusis called after every frame. 8. The GeometryArray s vertex coordinates is modified. 9. Coordinates are reassigned. 10. If frame number < number of frames for animation Set the WakeUpcondition to WakeupOnElapsedFrames( 1 ) 11. Else Restore the original Shape3D Appearanceand coordinates. Notify the ExplosionListenerthat the behavior is done. Call setEnable( false )to disabled the behavior. The processStimulusmethod for the ExplodeBehavioris as follows. From ExplodeBehavior.java public void processStimulus( java.util.Enumeration criteria ) { while( criteria.hasMoreElements() ) { WakeupCriterion wakeUp = (WakeupCriterion) criteria.nextElement(); if( wakeUp instanceof WakeupOnElapsedTime ) { //we are starting the explosion, //apply the appearance changes we require PolygonAttributes polyAttribs = new PolygonAttributes( PolygonAttributes.POLYGON_POINT, PolygonAttributes.CULL_NONE, 0 ); m_Shape3D.getAppearance().setPolygonAttributes( polyAttribs ); PointAttributes pointAttribs = new PointAttributes( 3, false ); m_Shape3D.getAppearance().setPointAttributes( pointAttribs ); m_Shape3D.getAppearance().setTexture( null ); m_TransparencyAttributes = new TransparencyAttributes( TransparencyAttributes.NICEST, 0 ); m_TransparencyAttributes.setCapability( TransparencyAttributes.ALLOW_VALUE_WRITE ); m_Shape3D.getAppearance().setTransparencyAttributes( m_TransparencyAttributes ); } else { //we are mid explosion, modify the GeometryArray m_nFrameNumber++; m_GeometryArray.getCoordinates( 0, m_CoordinateArray ); 221

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Java Web Hosting services

For example, instead of creating a new BoundingBox,

Filed under: Java 3D Programming — webmaster @ 5:20 am

For example, instead of creating a new BoundingBox, which would have had size 0, a single BoundingBoxobject was resized using: m_BoundingBox.setLower( 0,0,0 ); m_BoundingBox.setUpper( 0,0,0 ); With Java 3D in general, you should avoid burning (allocate, followed by garbage-collect) Objectsas much as possible, and minimize the work that the garbage collector has to perform. 13.3 ExplodeBehavior The constructor for the ExplodeBehavioris as follows: public ExplodeBehavior( Shape3D shape3D, int nElapsedTime, int nNumFrames, ExplosionListener listener ) The behavior attaches to the Shape3Dspecified and explodes the object after nElapsedTime milliseconds (figure 13.2). The explosion animation takes nNumFrames to complete, and, once complete, a notification is passed to the caller via an ExplosionListenerinterface method. Figure 13.2 The ExplodeBehavior: Frame 1, the original Shape3D; frames 2 4, some frames of the explosion animation To model the simple explosion, the behavior switches the Shape3D s appearance to rendering in points (by modifying the PolygonAttributes) and sets the point size (using PointAttributes). The transparency of the Shape3Dis then set using TransparencyAttributes. The vertices of the Shape3D s geometry are then moved away from the origin with a slight random bias in the x+, y+, and z+ direction. The ExplodeBehaviormoves through the following life cycle: 1. The behavior is created. 220

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Java Web Hosting services

January 28, 2007

From ObjectSizeBehavior.java class ObjectSizeBehavior extends Behavior { //the

Filed under: Java 3D Programming — webmaster @ 8:40 pm

{ //get all the coordinates m_GeometryArray.getCoordinates( 0, m_CoordinateArray ); //clear the old BoundingBox m_BoundingBox.setLower( 0,0,0 ); m_BoundingBox.setUpper( 0,0,0 ); //loop over every vertex and combine with the BoundingBox for( int n = 0; n

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services

From ObjectSizeBehavior.java class ObjectSizeBehavior extends Behavior { //the

Filed under: Java 3D Programming — webmaster @ 8:40 pm

From ObjectSizeBehavior.java class ObjectSizeBehavior extends Behavior { //the wake up condition for the behavior protected WakeupCondition m_WakeupCondition = null; //the GeometryArray for the Shape3D that we are querying protected GeometryArray m_GeometryArray = null; //cache some information on the model to save reallocation protected float[] m_CoordinateArray = null; protected BoundingBox m_BoundingBox = null; protected Point3d m_Point = null;; public ObjectSizeBehavior( GeometryArray geomArray ) { //save the GeometryArray that we are modifying m_GeometryArray = geomArray; //set the capability bits that the behavior requires m_GeometryArray.setCapability( GeometryArray.ALLOW_COORDINATE_READ ); m_GeometryArray.setCapability( GeometryArray.ALLOW_COUNT_READ ); //allocate an array for the coordinates m_CoordinateArray = new float[ 3 * m_GeometryArray.getVertexCount() ]; //create the BoundingBox used to calculate the size of the object m_BoundingBox = new BoundingBox(); //create a temporary point m_Point = new Point3d(); //create the WakeupCriterion for the behavior WakeupCriterion criterionArray[] = new WakeupCriterion[1]; criterionArray[0] = new WakeupOnElapsedFrames( 20 ); //save the WakeupCriterion for the behavior m_WakeupCondition = new WakeupOr( criterionArray ); } public void initialize() { //apply the initial WakeupCriterion wakeupOn( m_WakeupCondition ); } public void processStimulus( java.util.Enumeration criteria ) { while( criteria.hasMoreElements() ) { WakeupCriterion wakeUp = (WakeupCriterion) criteria.nextElement(); //every N frames, recalculate the bounds for the points //in the GeometryArray if( wakeUp instanceof WakeupOnElapsedFrames ) 218

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services

CHAPTER 13 Writing custom behaviors 13.1 The BehaviorTest

Filed under: Java 3D Programming — webmaster @ 4:14 pm

Figure 13.1 The BehaviorTest example application. StretchBehavior is used to modify the geometry of the Sphere after every frame, while ObjectSizeBehavior reports the Bounds for the object after every 20 frames ObjectSizeBehavioris the simplest, and it calculates the smallest BoundingBoxthat encloses a Shape3D s geometry. The BoundingBoxis recalculated every 20 frames, and the size of the BoundingBoxis written to standard output. Note that the basic anatomy of a behavior described in section 11.3 is adhered to here. ExplodeBehavioris more complex. Given a Shape3Dobject, it explodes the object after a specified number of milliseconds by rendering the Shape3Das points and modifying the coordinates within the Shape3D s GeometryArray. The transparency of the object is gradually increased so that the object fades into the background. StretchBehavioris the most complex of the custom behaviors. It operates upon a specified GeometryArrayand animates the vertices within the array as if they were weights attached by springs to the origin. StretchBehaviorlistens for key presses and increases the acceleration of each vertex when a key is pressed. The increased acceleration causes the vertices to move away from the origin, which causes an increase in the restraining force from the spring. The vertices oscillate back and forth, finally coming to rest at their original position. 13.2 ObjectSizeBehavior The ObjectSizeBehaviorclass implements a simple behavior that calculates and prints the size of an object based on the vertices in its GeometryArray. 217

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Tomcat Web Hosting services

CHAPTER 13 Writing custom behaviors 13.1 The BehaviorTest

Filed under: Java 3D Programming — webmaster @ 4:14 pm

CHAPTER 13 Writing custom behaviors 13.1 The BehaviorTest example 13.2 ObjectSizeBehavior 13.3 ExplodeBehavior 13.4 StretchBehavior 13.5 Using behaviors for debugging 13.6 Summary Some behaviors automatically execute complex code to modify objects within the scenegraph, so care must be taken to ensure that behavior processing does not bog down application performance. With careful design and knowledge of some of the limitations, behaviors can be a powerful asset in quickly developing or prototyping application logic. By the end of this chapter you should have a good sense of how to develop your own behaviors. By mixing and matching your own and the built-in behaviors, you should be able to design your application logic within Java 3D s behavior model. 13.1 The BehaviorTest example There are occasions when the built-in behaviors do not provide enough functionality to capture the logic of your application. By creating your own classes derived from Behavior, you can easily integrate your application logic into Java 3D s behavior processing framework. The BehaviorTestexample application uses four behaviors: the built-in RotationInterpolator and three custom behaviors of varying complexity: ObjectSizeBehavior, ExplodeBehavior, and StretchBehavior. See figure 13.1. 216

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Tomcat Web Hosting services

Spherethe normal vectors assigned to the vertices of

Filed under: Java 3D Programming — webmaster @ 10:39 am

Spherethe normal vectors assigned to the vertices of the Spheremust be created with the Primitive.GENERATE_NORMALS_INWARDoption. From SplineInterpolatorTest.java //we want a texture-mapped background of a sky protected Background createBackground() { //add the sky backdrop Background back = new Background(); back.setApplicationBounds( getApplicationBounds() ); BranchGroup bgGeometry = new BranchGroup(); //create an appearance and assign the texture image Appearance app = new Appearance(); Texture tex = new TextureLoader( “sky.gif”, this).getTexture(); app.setTexture( tex ); Sphere sphere = new Sphere( 1.0f, Primitive.GENERATE_TEXTURE_COORDS | Primitive.GENERATE_NORMALS_INWARD, app ); bgGeometry.addChild( sphere ); back.setGeometry( bgGeometry ); return back; } 12.4.5 Controlling the extent of the audio for the helicopters The distance over which the PointSoundscreated for the three helicopters can be heard is controlled by three factors: (1) the ViewPlatform s activation radius, (2) the scheduling bounds of the Sound, and (3) distance/gain parameters of the PointSounditself. A PointSoundis potentially audible when the ViewPlatform s activation radius intersects the scheduling bounds for the Sound. The actual volume of the mixed sound is controlled by the distance/gain parameters for the PointSound. From Helicopter.java //Return the scheduling bounds for the Helicopter s sound protected Bounds getSoundSchedulingBounds( boolean bCollide ) { return new BoundingSphere( new Point3d(0,0,0), 20 ); } The getSoundDistanceGainmethod returns an array of Point2fobjects that define how the volume of the sound attenuates with distance. In the following example the sound is at 20 percent of its maximum intensity at 2 units distance and at 5 percent of maximum intensity at 20 units distance. 214

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Java Web Hosting services

Spherethe normal vectors assigned to the vertices of

Filed under: Java 3D Programming — webmaster @ 10:39 am

protected Point2f[] getSoundDistanceGain( boolean bCollide ) { Point2f[] gainArray = new Point2f[2]; gainArray[0] = new Point2f( 2, 0.2f ); gainArray[1] = new Point2f( 20, 0.05f ); return gainArray; } 12.5 Summary This discussion of Interpolatorsand the SplineInterpolatorTestexample should have alerted you to some of the power of the interpolator behaviors built into Java 3D. The support for such high-level features as interpolators in Java 3D is a real time saver. Just think about having to implement this functionality by hand. 215

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Java Web Hosting services

-2 4 8 -0.3 0.6 0.2 1 1

Filed under: Java 3D Programming — webmaster @ 4:34 am

* - linear (int - 0 or 1) */ while( true ) { try { float knot = Float.parseFloat( tokenizer.nextToken() ); float posX = Float.parseFloat( tokenizer.nextToken() ); float posY = Float.parseFloat( tokenizer.nextToken() ); float posZ = Float.parseFloat( tokenizer.nextToken() ); float rotX = Float.parseFloat( tokenizer.nextToken() ); float rotY = Float.parseFloat( tokenizer.nextToken() ); float rotZ = Float.parseFloat( tokenizer.nextToken() ); float scaleX = Float.parseFloat( tokenizer.nextToken() ); float scaleY = Float.parseFloat( tokenizer.nextToken() ); float scaleZ = Float.parseFloat( tokenizer.nextToken() ); float tension = Float.parseFloat( tokenizer.nextToken() ); float continuity = Float.parseFloat( tokenizer.nextToken() ); float bias = Float.parseFloat( tokenizer.nextToken() ); int linear = Integer.parseInt( tokenizer.nextToken() ); //create the actual keyframe from the data just read TCBKeyFrame keyframe = new TCBKeyFrame( knot, linear, new Point3f( posX, posY, posZ ), createQuaternionFromEuler( rotX, rotY, rotZ ), new Point3f( scaleX, scaleY, scaleZ ), tension, continuity, bias ); keyFramesVector.add( keyframe ); } catch( Exception e ) { break; } } //create the return structure and populate TCBKeyFrame[] keysReturn = new TCBKeyFrame[ keyFramesVector.size() ]; for( int n = 0; n

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Tomcat Web Hosting services

-2 4 8 -0.3 0.6 0.2 1 1

Filed under: Java 3D Programming — webmaster @ 4:34 am

-2 4 8 -0.3 0.6 0.2 1 1 1 -1 1 -1 0 0.7 -2 5 10 -0.4 -0.6 0.5 1 1 1 -1 1 -1 0 0.8 -1 4 5 -0.6 -0.9 -0.2 1 1 1 1 1 1 0 0.9 0 10 15 -1.2 0 0 1 1 1 1 0 1 0 1.0 0 52 0 -1.5 0 0 1 1 1 0 1 1 0 The utility method to read and create the key frame array is simply the following: From Utils.java in the org.selman.java3d.book package static public TCBKeyFrame[] readKeyFrames( URL urlKeyframes ) { StringBuffer szBufferData = readFile( urlKeyframes ); if( szBufferData == null ) return null; Vector keyFramesVector = new Vector(); //create a tokenizer to tokenize the input file at whitespace java.util.StringTokenizer tokenizer = new java.util.StringTokenizer( szBufferData.toString() ); /* * Each keyframe is defined as follows: * - knot (0 >= k <= 1) * - position (x,y,z) * - rotation (rx,ry,rz) * - scale (x,y,z) * - tension (-1 >= t <= 1) * - continuity (-1 >= c <= 1) * - bias (-1 >= b <= 1) 212

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Tomcat Web Hosting services

« Previous PageNext Page »

Powered by Java Web Hosting