⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 keynavigatetest.java

📁 《Java 3D编程》的源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		Vector3d squareSize = getMapSquareSize( );		point2d.x = (worldCoord.x + FLOOR_WIDTH)/ squareSize.x;		point2d.y = (worldCoord.z + FLOOR_LENGTH)/ squareSize.z;				return point2d;						}	public boolean isCollision( Transform3D t3d, boolean bViewSide )	{		// get the translation		t3d.get( m_Translation );		// we need to scale up by the scale that was		// applied to the root TG on the view side of the scenegraph		if( bViewSide != false )			m_Translation.scale( 1.0 / getScale( ) );		Vector3d mapSquareSize = getMapSquareSize( );		// first check that we are still inside the "world"		if( 	m_Translation.x < -FLOOR_WIDTH + mapSquareSize.x ||			m_Translation.x > FLOOR_WIDTH - mapSquareSize.x ||			m_Translation.y < -FLOOR_LENGTH + mapSquareSize.y ||			m_Translation.y > FLOOR_LENGTH - mapSquareSize.y )			return true;		if( bViewSide != false )			// then do a pixel based look up using the map			return isCollision( m_Translation );		return false;	}	// returns true if the given x,z location in the world corresponds to a wall section	protected boolean isCollision( Vector3d worldCoord )	{		Point2d point = convertToMapCoordinate( worldCoord );		int nImageWidth = m_MapImage.getWidth( );		int nImageHeight = m_MapImage.getHeight( );		// outside of image		if( point.x < 0 || point.x >= nImageWidth ||			point.y < 0 || point.y >= nImageHeight )			return true;		int color = m_MapImage.getRGB( (int) point.x, (int) point.y );		// we can't walk through walls or bookcases		return( color == m_ColorWall || color == m_ColorBookcase );			}	public Group createMap( Group g )	{			System.out.println( "Creating map items" );		Group mapGroup = new Group( );		g.addChild( mapGroup );		Texture tex = new TextureLoader( m_szMapName, this ).getTexture( );		m_MapImage = ((ImageComponent2D) tex.getImage( 0 )).getImage( );		float imageWidth = m_MapImage.getWidth( );		float imageHeight = m_MapImage.getHeight( );		FLOOR_WIDTH = imageWidth * 8;		FLOOR_LENGTH = imageHeight * 8;		for( int nPixelX = 1; nPixelX < imageWidth-1; nPixelX++ )		{			for( int nPixelY = 1; nPixelY < imageWidth-1; nPixelY++ )				createMapItem( mapGroup, nPixelX, nPixelY );			float percentDone = 100 * (float) nPixelX / (float) (imageWidth-2);			System.out.println( "   " + (int) (percentDone) + "%" );		}		createExternalWall( mapGroup );		return mapGroup;	}	void createMapItem( Group mapGroup, int nPixelX, int nPixelY )	{		int color = m_MapImage.getRGB( (int) nPixelX, (int) nPixelY );		if( color == m_ColorWall )			createWall( mapGroup, nPixelX, nPixelY );		else if( color == m_ColorGuard )			createGuard( mapGroup, nPixelX, nPixelY );		else if( color == m_ColorLight )			createLight( mapGroup, nPixelX, nPixelY );		else if( color == m_ColorBookcase )			createBookcase( mapGroup, nPixelX, nPixelY );		else if( color == m_ColorWater )			createWater( mapGroup, nPixelX, nPixelY );	}		Vector3d getMapSquareSize( )	{		if( m_MapSquareSize == null )		{			double imageWidth = m_MapImage.getWidth( );			double imageHeight = m_MapImage.getHeight( );			m_MapSquareSize = new Vector3d( 2 * FLOOR_WIDTH / imageWidth, 0, 2 * FLOOR_LENGTH / imageHeight );		}		return m_MapSquareSize;	}	void createWall( Group mapGroup, int nPixelX, int nPixelY )	{		Point3d point = convertToWorldCoordinatesPixelCenter( nPixelX, nPixelY );		if( m_WallAppearance == null )			m_WallAppearance = new Appearance( );		Vector3d squareSize = getMapSquareSize( );		Cuboid wall = new Cuboid( this, mapGroup, ComplexObject.GEOMETRY | ComplexObject.TEXTURE );		wall.createObject( m_WallAppearance, 			new Vector3d( point.x, m_kFloorLevel, point.z ),			new Vector3d( squareSize.x/2, m_kCeilingHeight/2, squareSize.z/2 ) ,			"wall.gif", null, null );	}	void createExternalWall( Group mapGroup )	{		Vector3d squareSize = getMapSquareSize( );		Appearance app = new Appearance( );		app.setColoringAttributes( new ColoringAttributes( new Color3f( 132f/255f,0,66f/255f ), ColoringAttributes.FASTEST ) );		int imageWidth = m_MapImage.getWidth( );		int imageHeight = m_MapImage.getHeight( );		Point3d topLeft = convertToWorldCoordinatesPixelCenter( 0,0 );		Point3d bottomRight = convertToWorldCoordinatesPixelCenter( imageWidth-1, imageHeight-1 );		// top		Cuboid wall = new Cuboid( this, mapGroup, ComplexObject.GEOMETRY );		wall.createObject( app, 			new Vector3d( 0, m_kFloorLevel, topLeft.z ),			new Vector3d( squareSize.x * imageWidth/2, m_kCeilingHeight/2, squareSize.z/2 ),			null, null, null );		// bottom		wall = new Cuboid( this, mapGroup, ComplexObject.GEOMETRY );		wall.createObject( app, 			new Vector3d( 0, m_kFloorLevel, bottomRight.z ),			new Vector3d( squareSize.x * imageWidth/2, m_kCeilingHeight/2, squareSize.z/2 ),			null, null, null );		// left		wall = new Cuboid( this, mapGroup, ComplexObject.GEOMETRY );		wall.createObject( app, 			new Vector3d( topLeft.x, m_kFloorLevel, 0 ),			new Vector3d( squareSize.x/2, m_kCeilingHeight/2, squareSize.z * imageHeight/2 ) ,			null, null, null );		// right		wall = new Cuboid( this, mapGroup, ComplexObject.GEOMETRY );		wall.createObject( app, 			new Vector3d( bottomRight.x, m_kFloorLevel, 0 ),			new Vector3d( squareSize.x/2, m_kCeilingHeight/2, squareSize.z * imageHeight/2 ) ,			null, null, null );	}	void createGuard( Group mapGroup, int nPixelX, int nPixelY )	{		Point3d point = convertToWorldCoordinatesPixelCenter( nPixelX, nPixelY );		if( m_GuardAppearance == null )			m_GuardAppearance = new Appearance( );		Vector3d squareSize = getMapSquareSize( );		Guard guard = new Guard( this, mapGroup, ComplexObject.GEOMETRY, this );		guard.createObject( m_GuardAppearance,			new Vector3d( point.x, (m_kFloorLevel+m_kCeilingLevel)/2, point.z ),			new Vector3d( 1, 1, 1 ),			null, null, null );	}	void createLight( Group mapGroup, int nPixelX, int nPixelY )	{		Point3d point = convertToWorldCoordinatesPixelCenter( nPixelX, nPixelY );		// we do not share an Appearance for lights		// or they all animate in synch...		final double lightHeight = m_kCeilingHeight/1.5;		Light light = new Light( this, mapGroup, ComplexObject.GEOMETRY | ComplexObject.TEXTURE );		light.createObject( new Appearance( ), 			new Vector3d( point.x, m_kFloorLevel+lightHeight/2, point.z ),			new Vector3d( 5, lightHeight, 5 ) ,			"light.gif", null, null );	}	void createWater( Group mapGroup, int nPixelX, int nPixelY )	{		Point3d point = convertToWorldCoordinatesPixelCenter( nPixelX, nPixelY );		if( m_WaterAppearance == null )		{			m_WaterAppearance = new Appearance( );			m_WaterAppearance.setPolygonAttributes( new PolygonAttributes( PolygonAttributes.POLYGON_FILL, PolygonAttributes.CULL_NONE, 0, false ) );			m_WaterAppearance.setTransparencyAttributes( new TransparencyAttributes( TransparencyAttributes.BLENDED, 1.0f ) );			m_WaterAppearance.setTextureAttributes( new TextureAttributes( TextureAttributes.REPLACE, new Transform3D( ), new Color4f( 0,0,0,1 ), TextureAttributes.FASTEST ) );		}		Land water = new Land( this, mapGroup, ComplexObject.GEOMETRY | ComplexObject.TEXTURE );		water.createObject( m_WaterAppearance, 			new Vector3d( point.x, m_kFloorLevel+0.1, point.z ),			new Vector3d( 40, 1, 40 ) ,			"water.gif", null, null );	}	void createBookcase( Group mapGroup, int nPixelX, int nPixelY )	{		Point3d point = convertToWorldCoordinatesPixelCenter( nPixelX, nPixelY );		if( m_BookcaseAppearance == null )			m_BookcaseAppearance = new Appearance( );		Vector3d squareSize = getMapSquareSize( );		Cuboid bookcase = new Cuboid( this, mapGroup, ComplexObject.GEOMETRY | ComplexObject.TEXTURE );		bookcase.createObject( m_BookcaseAppearance,			new Vector3d( point.x, m_kFloorLevel, point.z ),			new Vector3d( squareSize.x/2, m_kCeilingHeight/2.7, squareSize.z/2 ),			"bookcase.gif", null, null );	}	public static void main( String[] args )	{		KeyNavigateTest keyTest = new KeyNavigateTest( );		keyTest.saveCommandLineArguments( args );		new MainFrame( keyTest, m_kWidth, m_kHeight );	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -