📄 keynavigatetest.java
字号:
/********************************************************** Copyright (C) 2001 Daniel Selman First distributed with the book "Java 3D Programming" by Daniel Selman and published by Manning Publications. http://manning.com/selman This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. The license can be found on the WWW at: http://www.fsf.org/copyleft/gpl.html Or by writing to: Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. Author can be contacted at: Daniel Selman: daniel@selman.org If you make changes you think others would like please contact Daniel Selman.**************************************************************/package org.selman.java3d.book.keynavigatetest;import java.applet.Applet;import java.awt.*;import java.awt.event.*;import java.awt.image.*;import javax.media.j3d.*;import javax.vecmath.*;import com.sun.j3d.utils.applet.MainFrame;import com.sun.j3d.utils.geometry.*;import com.sun.j3d.utils.image.*;import org.selman.java3d.book.common.*;/*** Simple DOOM style navigation of a 3D scene using Java 3D.* The scene description is loaded from a GIF file where different* colors denote objects in the 3D scene. The example includes:* simple (manual) collision detection, texture animation, keyboard* navigation.*/public class KeyNavigateTest extends Java3dApplet implements CollisionDetector{ private static int m_kWidth = 300; private static int m_kHeight = 300; private static final String m_szMapName = new String( "small_map.gif" ); private float FLOOR_WIDTH = 256; private float FLOOR_LENGTH = 256; private final int m_ColorWall = new Color( 0,0,0 ).getRGB( ); private final int m_ColorGuard = new Color( 255,0,0 ).getRGB( ); private final int m_ColorLight = new Color( 255,255,0 ).getRGB( ); private final int m_ColorBookcase = new Color( 0,255,0 ).getRGB( ); private final int m_ColorWater = new Color( 0,0,255 ).getRGB( ); private Vector3d m_MapSquareSize = null; private Appearance m_WallAppearance = null; private Appearance m_GuardAppearance = null; private Appearance m_BookcaseAppearance = null; private Appearance m_WaterAppearance = null; private BufferedImage m_MapImage = null; private final double m_kFloorLevel = -20; private final double m_kCeilingHeight = 50; private final double m_kCeilingLevel = m_kFloorLevel + m_kCeilingHeight; private Vector3d m_Translation = new Vector3d( ); public KeyNavigateTest( ) { initJava3d( ); } protected double getScale( ) { return 0.05; } protected int getCanvas3dWidth( Canvas3D c3d ) { return m_kWidth - 10; } protected int getCanvas3dHeight( Canvas3D c3d ) { return m_kHeight - 10; } // edit the positions of the clipping // planes so we don't clip on the front // plane prematurely protected double getBackClipDistance( ) { return 20.0; } protected double getFrontClipDistance( ) { return 0.05; } // we create 2 TransformGroups above the ViewPlatform: // the first merely applies a scale, while the second is // manipulated using the KeyBehavior public TransformGroup[] getViewTransformGroupArray( ) { TransformGroup[] tgArray = new TransformGroup[2]; tgArray[0] = new TransformGroup( ); tgArray[1] = new TransformGroup( ); Transform3D t3d = new Transform3D( ); t3d.setScale( getScale( ) ); t3d.invert( ); tgArray[0].setTransform( t3d ); // ensure the Behavior can access the TG tgArray[1].setCapability( TransformGroup.ALLOW_TRANSFORM_WRITE ); // create the KeyBehavior and attach KeyCollisionBehavior keyBehavior = new KeyCollisionBehavior( tgArray[1], this ); keyBehavior.setSchedulingBounds( getApplicationBounds( ) ); keyBehavior.setMovementRate( 0.7 ); tgArray[1].addChild( keyBehavior ); return tgArray; } protected BranchGroup createSceneBranchGroup( ) { BranchGroup objRoot = super.createSceneBranchGroup( ); TransformGroup objTrans = new TransformGroup( ); objTrans.setCapability( TransformGroup.ALLOW_TRANSFORM_WRITE ); objTrans.setCapability( TransformGroup.ALLOW_TRANSFORM_READ ); createMap( objTrans ); createFloor( objTrans ); createCeiling( objTrans ); objRoot.addChild( objTrans ); return objRoot; } public Group createFloor( Group g ) { System.out.println( "Creating floor" ); Group floorGroup = new Group( ); Land floorTile = null; // use a shared Appearance so we only store 1 copy of the texture Appearance app = new Appearance( ); g.addChild( floorGroup ); final double kNumTiles = 6; for( double x = -FLOOR_WIDTH + FLOOR_WIDTH/(2 * kNumTiles); x < FLOOR_WIDTH; x = x + FLOOR_WIDTH/kNumTiles ) { for( double z = -FLOOR_LENGTH + FLOOR_LENGTH/(2 * kNumTiles); z < FLOOR_LENGTH; z = z + FLOOR_LENGTH/kNumTiles ) { floorTile = new Land( this, g, ComplexObject.GEOMETRY | ComplexObject.TEXTURE ); floorTile.createObject( app, new Vector3d( x,m_kFloorLevel,z ), new Vector3d( FLOOR_WIDTH/(2*kNumTiles),1, FLOOR_LENGTH/(2*kNumTiles) ), "floor.gif", null, null ); } } return floorGroup; } public Group createCeiling( Group g ) { System.out.println( "Creating ceiling" ); Group ceilingGroup = new Group( ); Land ceilingTile = null; // because we are technically viewing the ceiling from below // we want to switch the normals using a PolygonAttributes. Appearance app = new Appearance( ); app.setPolygonAttributes( new PolygonAttributes( PolygonAttributes.POLYGON_FILL, PolygonAttributes.CULL_NONE, 0, false ) ); g.addChild( ceilingGroup ); final double kNumTiles = 6; for( double x = -FLOOR_WIDTH + FLOOR_WIDTH/(2 * kNumTiles); x < FLOOR_WIDTH; x = x + FLOOR_WIDTH/kNumTiles ) { for( double z = -FLOOR_LENGTH + FLOOR_LENGTH/(2 * kNumTiles); z < FLOOR_LENGTH; z = z + FLOOR_LENGTH/kNumTiles ) { ceilingTile = new Land( this, g, ComplexObject.GEOMETRY | ComplexObject.TEXTURE ); ceilingTile.createObject( app, new Vector3d( x,m_kCeilingLevel,z ), new Vector3d( FLOOR_WIDTH/(2*kNumTiles),1, FLOOR_LENGTH/(2*kNumTiles) ) , "ceiling.gif", null, null ); } } return ceilingGroup; } Point3d convertToWorldCoordinate( int nPixelX, int nPixelY ) { Point3d point3d = new Point3d( ); Vector3d squareSize = getMapSquareSize( ); // range from 0 to 1 point3d.x = nPixelX * squareSize.x; point3d.x -= FLOOR_WIDTH; point3d.z = nPixelY * squareSize.z; point3d.z -= FLOOR_LENGTH; point3d.y = 0; return point3d; } Point3d convertToWorldCoordinatesPixelCenter( int nPixelX, int nPixelY ) { Point3d point3d = convertToWorldCoordinate( nPixelX, nPixelY ); Vector3d squareSize = getMapSquareSize( ); point3d.x += squareSize.x/2; point3d.z += squareSize.z/2; return point3d; } Point2d convertToMapCoordinate( Vector3d worldCoord ) { Point2d point2d = new Point2d( );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -