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

📄 physics3dthread.java

📁 运用java3D模拟刚体间的碰撞,爆炸及在万有引力作用下的运动轨迹,设置适当的参数可以模拟天体运动等多种物理现象.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/***************************************************

Copyright 2003 Ben Childs


This file is part of Physics 3D.

    Physics 3D 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; either version 2 of the License, or
    (at your option) any later version.

    Physics 3D 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.

    You should have received a copy of the GNU General Public License
    along with Physics 3D; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA




***************************************************/

package com.bchilds.Physics3D;

import java.awt.event.*;
import javax.swing.*;
import java.util.Date;

import javax.media.j3d.*;
import javax.vecmath.*;

import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.behaviors.vp.OrbitBehavior;
import com.sun.j3d.utils.picking.*;


public class Physics3DThread extends Thread implements MouseListener
{
	private static final int SPHERE_DETAIL = 20;
	
	private static Appearance app;			//Appearance for an unselected Particle
	private static Appearance selectedApp;	//Appearance for a selected Particle
	
	//Java3D Objects//////////////////////////////
	private SimpleUniverse u;		//Main Universe
	private PickCanvas pickCanvas;	//PickCanvas for selecting particles
	public PropertiesFrame pframe;	//Main Properties Frame
	private BranchGroup bgRoot;		//Root BranchGroup
	private OrbitBehavior orbit;	//The Orbit Behavior
	Shape3D lastShape = null;		//Keeps track of last selected shape
	///////////////////////////////////////////////////
	
	//Booleans to control thread state
	boolean animating = false;	//True: Simulation Engine Active : False: inactive
	boolean shouldStop = false; //True: Thread should stop completely (for shutdown procedures)
	boolean rewinding = false;	//True: Simulation Engine should run backwards : False: forwards
	
	static int accuracy = 10;			//Time in milliseconds between each calculation
	static float timeMultiplier = 1;	//Speed Setting (how long should the thread tell the engin that time has passed)
	
	JFrame mainFrame;			//Reference to main frame
	
	public Physics3DThread(SimpleUniverse u, JFrame mainFrame)
	{
		this.mainFrame = mainFrame;
	
		//Create generic Appearance
		app = new Appearance();
		
		Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
		Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
		Color3f red = new Color3f(1.0f, 0.0f, 0.0f);
		
		app.setMaterial(new Material(white, black, white, black, 1.0f));
		app.setCapability(Appearance.ALLOW_POLYGON_ATTRIBUTES_WRITE);
		app.setCapability(Appearance.ALLOW_POINT_ATTRIBUTES_WRITE);
		app.setCapability(Appearance.ALLOW_TEXTURE_WRITE);
		app.setCapability(Appearance.ALLOW_TRANSPARENCY_ATTRIBUTES_WRITE);
		
		//Create selectedAppearance
		selectedApp = new Appearance();
		selectedApp.setCapability(Appearance.ALLOW_POLYGON_ATTRIBUTES_WRITE);
		selectedApp.setCapability(Appearance.ALLOW_POINT_ATTRIBUTES_WRITE);
		selectedApp.setCapability(Appearance.ALLOW_TEXTURE_WRITE);
		selectedApp.setCapability(Appearance.ALLOW_TRANSPARENCY_ATTRIBUTES_WRITE);
		selectedApp.setMaterial(new Material(red, black, white, black, .5f));

		this.u = u;
		
		//Create Root Branch Group
		bgRoot = new BranchGroup();
		bgRoot.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
		bgRoot.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
		bgRoot.setCapability(BranchGroup.ALLOW_DETACH);
		bgRoot.setCapability(BranchGroup.ALLOW_COLLIDABLE_READ);	
		
		//Create the background (white sphere)
		u.addBranchGraph( createBackground() );
		
		//Add the lighting to the scene
		addLights( bgRoot );
	
		//Add the root BranchGroup to the Universe
		u.addBranchGraph(bgRoot);
		
		Bounds sceneBounds = getBoundingSphere();
		
		//Create a new Orbit behavior for mouse view control
		orbit = new OrbitBehavior(u.getCanvas(), OrbitBehavior.REVERSE_ROTATE | OrbitBehavior.REVERSE_TRANSLATE);
		orbit.setSchedulingBounds(sceneBounds);
		
		//Set the view behavior and back clip distance (anything over this crashes is there some way to make it bigger? So we can have super huge to-scale systems?)
		u.getViewingPlatform().setViewPlatformBehavior(orbit);
		u.getViewer().getView().setBackClipDistance(1E13);
		
		//Create  the pick canvas
		pickCanvas = new PickCanvas(u.getCanvas(), bgRoot);
    	pickCanvas.setMode(PickTool.BOUNDS); 
    	pickCanvas.setTolerance(4.0f);
		///////////////////////////////
		
		//Create the Properties Frame
		try
		{
			pframe = new PropertiesFrame();
			pframe.initComponents();
			pframe.setVisible(true);
		}
		catch(Exception e)
		{
		
		}
		
		//Tell the engine the root BranchGroup		
		Particle3D.setRoot(bgRoot);
		///////////////////////////////////
		
	}

	public void run()
	{
		//Main sleep loop
		while(true)
		{
			if(shouldStop)	//Stop if it should
				break;
				
			if(animating)	//Main Simulation section
			{
			
				Date time = new Date();	//Get Current time
				
				step();					//Step
				
				Date afterTime = new Date();	//Get Current Time
				
				//Figure out the sleep time : accuracy - timeUsedAlready if the computer
				//can't keep up, everything will slow down as sleepTime will be < 0
				try
				{
					long sleepTime = accuracy - (afterTime.getTime() - time.getTime());
					
					if(sleepTime > 0)
						sleep(sleepTime);
				
				}
				catch(InterruptedException e)
				{
				
				}
			
			}
			else		//If not animating just sleep
			{
				try
				{
					sleep(accuracy);
				
				}
				catch(InterruptedException e)
				{
				
				}
			}	
		
		}
		u = null;	//Set the universe to null and quit
	}
	
	//Set the rewinding state of the thread
	public void setRewind(boolean rew)
	{
		rewinding = rew;
	}
	///////////////////////////////////////
	
	//Stop the thread
	public void stopMe()
	{
		shouldStop = true;	
	}
	/////////////////////
	
	
	//Animate//////////
	public void setAnimating(boolean ani)
	{
		//Show/Hide the properties frame
		if(ani)
		{
			Particle3D.setup();
			pframe.setVisible(false);
		}
		else
			pframe.setVisible(true);
		pframe.reset();
		
		animating = ani;
	}
	///////////////////
	
	
	//Add the lighting to the scene (2 directional lights 1 yellow, 1 magenta, 
	//and 1 gray ambient light)
	public void addLights (BranchGroup bg)
	{
		Color3f color = new Color3f(1.0f, 1.0f, 0.0f);
		Vector3f direction = new Vector3f ( -1.0f, -1.0f, -1.0f);
		
		DirectionalLight light = new DirectionalLight( color, direction );
		light. setInfluencingBounds(getBoundingSphere() );
		bg.addChild(light);
		
		color = new Color3f(1.0f, 0.3f, 0.85f);
		direction = new Vector3f ( -1.0f, -2.0f, -1.0f);
		
		light = new DirectionalLight( color, direction );
		light. setInfluencingBounds(getBoundingSphere() );
		bg.addChild(light);
		
		AmbientLight alight = new AmbientLight(new Color3f(.15f, .15f, .15f));
		alight. setInfluencingBounds(getBoundingSphere() );
		bg.addChild(alight);
	}
	//////////////////////////////////////////////////////
	
	
	//Create Background Branch Group
	public BranchGroup createBackground()
	{
		BranchGroup backgroundGroup = new BranchGroup();
		
		Background back = new Background();
		back.setApplicationBounds(getBoundingSphere());
		
		BranchGroup bgGeometry = new BranchGroup();
		Appearance app = new Appearance();
		
		//Load the back.png texture to the texture (you can replace this file with any png file
		//for different background textures)
	//	Texture tex = new TextureLoader("back.png", u.getCanvas()).getTexture();
	//	app.setTexture(tex);
		Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
		Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
			
		app.setMaterial(new Material(white, black, white, black, 1.0f));
		
		Sphere sphere = new Sphere( 1.0f, Primitive.GENERATE_TEXTURE_COORDS | Primitive.GENERATE_NORMALS_INWARD, app);
		
		bgGeometry.addChild(sphere);

⌨️ 快捷键说明

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