?? clothsim.java
字號:
import java.awt.AWTEvent;
import java.util.Enumeration;
import java.applet.Applet;
import java.lang.Math.*;
import java.awt.Graphics;
import java.awt.*;
import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;
import java.awt.event.WindowAdapter;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.geometry.Box;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.behaviors.mouse.*;
import com.sun.j3d.utils.behaviors.keyboard.*;
import com.sun.j3d.utils.behaviors.picking.*;
import com.sun.j3d.utils.image.TextureLoader;
//添加類包
/////////////////////////////////////////////////////////////
// ClothSim主類 //
// //
// 1.創建場景圖,添加鼠標,鍵盤行為,添加光照背景, //
// 創建ClothSimFace面模型 //
// 2. 初始質點位置,速度. //
// 3.創建刷新線程,調用物理模型計算,刷新質點坐標, //
// 調用lothSimUpdater類,刷新三角面的位置,頂點法向量 //
// //
// //
/////////////////////////////////////////////////////////////
public class ClothSim extends Applet implements Runnable
{
ClothSim clothSim;
Thread timer;//聲明一個刷新線程對象
boolean timerFlage=true;//刷新線程暫停標志
int timerFlagevcout=0;//鍵盤響應線程狀態切換變量
Point3d pointPosition[][];//聲明質點位置三維坐標數組
Transform3D pointTransform3D[][];//聲明質點局部坐標系Transform3D數組
TransformGroup pointTransformGroup[][];//聲明質點局部坐標系數組
SharedGroup shared;//聲明一個復制SharedGroup對象
Vector3d vectorABack[][];//聲明質點加速度備用數組
Point3d pointPositionBack[][];//聲明質點位置三維坐標備用數組
Vector3d vectorVBack[][];//聲明質點速度備用數組
Vector3d pointNormal[][];//質點法向量數組
Vector3d vector3dV[][];//聲明質點速度數組
Physics physicsActive;//聲明一個物理模型對象
Vector3d vectorF[][];//聲明質點受力對象
TriangleArray clothSimArray=null;//聲明ClothSimFace面模型對象
LineArray clothSimLines= null;//聲明ClothSImLines線模型對象
GeometryUpdater geometryUpdater=null;//聲明GeometryUpdater更新對象
GeometryUpdater geometryLineUpdater=null;//聲明GeometryLineUpdater更新對象
private java.net.URL texImage = null;//地面紋理圖片鏈接變量
private java.net.URL bgImage = null;//背景貼圖圖片鏈接變量
boolean downFlage=false;//布料脫離旗桿標志位
double xyDistance=0.06;//相鄰質點距離
public Switch s;//面,線模型Switch對象
public Switch sPoint;//質點球模型Switch對象
double k=500.0;//彈性模量常數
double kair=0.0125;//空氣阻力常數
double kvi=0.5;//空氣流參數
double lbend=xyDistance*2;//彎曲彈簧原始長度常數
double lstretch=xyDistance;//結構彈簧原始長度常數
double lshear=xyDistance*Math.sqrt(2.0);//剪切彈簧原始長度常數
double mg=0.033516*20;//質點重量常數
double m=0.00342*20;//質點質量常數
double t=0.003;//位置數組pointPosition刷新計算時間常數
double Fwindx=2.5;//x方向風力變量初始值
double Fwindz=-1.5;//z方向風力變量初始值
int clothSimFaceType=1;//clothsim面形式變量
int tWait=2;//刷新線程刷新間隔時間變量
int xTotal=16;//橫向質點總數
int yTotal=12;//縱向質點總數
int ouLaFlage=0;//在簡單歐拉公式和改進歐拉公式間切換的標志位
BranchGroup objRoot;
int h=0;
SimpleUniverse u ;
TransformGroup objTrans;
TransformGroup trans1;
//創建場景圖
public BranchGroup createSceneGraph(SimpleUniverse su)
{
objRoot=new BranchGroup();
objRoot.setCapability(BranchGroup.ALLOW_DETACH);
objTrans=new TransformGroup();
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
objTrans.setCapability(Group.ALLOW_CHILDREN_EXTEND);
//創建根節點對象,以及根節點坐標系
Transform3D trans3D1=new Transform3D();
trans3D1.setTranslation(new Vector3f(-1.0f,0.0f,0.0f));
trans1=new TransformGroup(trans3D1);
trans1.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
trans1.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
trans1.addChild(objTrans);
objRoot.addChild(trans1);
//創建旗桿,布料整體局部坐標系
//objRoot.removeChild(trans1);
Group clothSimArray=ClothSimArray();
//創建clothSim質點球模型
ClothSimFace clothSimFace = new ClothSimFace();
//創建clothSim三角面模型
ClothSimLine clothSimLines=new ClothSimLine();
//創建clothSim線模型
s=new Switch();
s.setCapability(Switch.ALLOW_SWITCH_READ );
s.setCapability(Switch.ALLOW_SWITCH_WRITE );
s.addChild(clothSimFace);
s.addChild(clothSimLines);
s.setWhichChild(0);
//創建線,面模型Switch對象,并設置其能力
sPoint=new Switch();
sPoint.setCapability(Switch.ALLOW_SWITCH_READ);
sPoint.setCapability(Switch.ALLOW_SWITCH_WRITE );
sPoint.addChild(clothSimArray);
sPoint.setWhichChild(Switch.CHILD_NONE);
//創建質點球模型Switch對象,并設置其能力
objTrans.addChild(s);
objTrans.addChild(sPoint);
//將Switch對象s,sPoint添加到objTrans對象
ColoringAttributes ca=new ColoringAttributes();
ca.setColor(0.1f,0.4f,0.4f);
Appearance app=new Appearance();
app.setColoringAttributes(ca);
Cylinder clothSimCylinder=new Cylinder(0.015f,2.0f,app);
objTrans.addChild(clothSimCylinder);
Transform3D trans3DShpere=new Transform3D();
trans3DShpere.setTranslation(new Vector3f(0.0f,1.025f,0.0f));
TransformGroup transShpere=new TransformGroup(trans3DShpere);
objTrans.addChild(transShpere);
Sphere shpere=new Sphere(0.03f,app);
transShpere.addChild(shpere);
//添加旗桿
BoundingSphere bounds =
new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
TextureLoader bgTexture = new TextureLoader(bgImage, this);
Background bg = new Background(bgTexture.getImage());
bg.setApplicationBounds(bounds);
objRoot.addChild(bg);
//添加背景
Appearance app1 = new Appearance();
Material material = new Material();
material.setEmissiveColor(new Color3f(0.0f,0.5f,0.3f));
app1.setMaterial(material);
app1.setCapability(app.ALLOW_COLORING_ATTRIBUTES_WRITE);
TextureLoader myLoader1=new TextureLoader (texImage,this);
ImageComponent2D myImage1=myLoader1.getImage ();
if ( myImage1 == null )
{
System.err.println( "Cannot load texture '" + texImage + "'" );
return null;
}
Texture2D myTex=(Texture2D)myLoader1.getTexture();
app1.setTexture(myTex);
Transform3D trans3DShpere1=new Transform3D();
trans3DShpere1.setTranslation(new Vector3f(0.0f,-1.0f,0.0f));
TransformGroup transShpere1=new TransformGroup(trans3DShpere1);
objTrans.addChild(transShpere1);
Box box=new Box(100f,0.005f,100.0f,Box.GENERATE_TEXTURE_COORDS,app1);
transShpere1.addChild(box);
//創建地面并添加紋理
// 添加鼠標行為
MouseRotate myMR = null;
MouseTranslate myMT = null;
MouseZoom myMZ = null;
// 添加旋轉功能
myMR = new MouseRotate();
myMR.setTransformGroup(objTrans);
myMR.setSchedulingBounds(new BoundingSphere());
objRoot.addChild(myMR);
// 添加平移功能
myMT = new MouseTranslate();
myMT.setTransformGroup(objTrans);
myMT.setSchedulingBounds(new BoundingSphere());
objRoot.addChild(myMT);
// 添加縮放功能
myMZ = new MouseZoom();
myMZ.setTransformGroup(objTrans);
myMZ.setSchedulingBounds(new BoundingSphere());
objRoot.addChild(myMZ);
//添加鍵盤響應功能
ClothSimBehavior myClothSimBehavior = new ClothSimBehavior();
myClothSimBehavior.setSchedulingBounds(new BoundingSphere());
// 設定作用范圍
objRoot.addChild(myClothSimBehavior);
// 將鍵盤響應對象添加到根結點
AmbientLight lightA = new AmbientLight();
lightA.setInfluencingBounds(new BoundingSphere(new Point3d(),1000.0));
objRoot.addChild(lightA);
DirectionalLight lightD1 = new DirectionalLight();
lightD1.setInfluencingBounds(new BoundingSphere(new Point3d(),1000.0));
Vector3f direction = new Vector3f(0.25f, 0.0f, -1.0f);
Color3f color = new Color3f(1.0f,1.0f,1.0f);
direction.normalize();
lightD1.setDirection(direction);
lightD1.setColor(color);
objRoot.addChild(lightD1); //添加正面光照
DirectionalLight lightD2 = new DirectionalLight();
lightD2.setInfluencingBounds(new BoundingSphere(new Point3d(),1000.0));
Vector3f direction2 = new Vector3f(0.5f, 0.0f, 1.0f);
Color3f color2 = new Color3f(1.0f,0f,0f);
direction2.normalize();
lightD2.setDirection(direction2);
lightD2.setColor(color2);//添加反面光照
objRoot.addChild(lightD2);
//添加光照
objRoot.compile();
return objRoot;
}
//ClothSim主類構造函數
public ClothSim() {
}
public ClothSim(java.net.URL bgurl, java.net.URL texurl) {
bgImage = bgurl;
texImage = texurl;
}
public void init() {
if (bgImage == null) {
try {
bgImage = new java.net.URL(getCodeBase().toString() +
"bg.jpg");
}
catch (java.net.MalformedURLException ex) {
System.out.println(ex.getMessage());
System.exit(1);
}
}
if (texImage == null) {
try {
texImage = new java.net.URL(getCodeBase().toString() +
"apimage.jpg");
}
catch (java.net.MalformedURLException ex) {
System.out.println(ex.getMessage());
System.exit(1);
}
}
//導入背景,地面紋理圖片
initializationPVF();//初始質點受力,速度
physicsActive=new Physics();//創建質點物理模型對象
setLayout(new BorderLayout());
Canvas3D c = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
add("Center", c);
Viewer viewer = new Viewer(c);
Vector3d viewpoint = new Vector3d(0.0, 0.0, 4.0); //初始觀察點位置
Transform3D t = new Transform3D();
t.set(viewpoint);
ViewingPlatform v = new ViewingPlatform( );
v.getViewPlatformTransform().setTransform(t);
u = new SimpleUniverse(v, viewer);
BranchGroup scene = createSceneGraph(u);
u.getViewingPlatform();
u.addBranchGraph(scene);
}
// 根據物理模型不斷刷新質點位置線程
public void start()
{
timer = new Thread(this);
timer.start();
}
//線程run函數
public void run()
{
while (true)
{
try{
if(h==1)
{
objRoot.addChild(trans1);
h=0;}
else
if(timerFlage==true){
physicsActive.oulaCalculate();//根據質點的受力,利用簡單歐拉公式和改進歐拉公式混合計算刷新質點位置
UpdataerLineArray();//刷新質點球模型
clothSimLines.updateData(geometryLineUpdater); //刷新線模型
clothSimArray.updateData(geometryUpdater);//刷新三角面模型
Thread.sleep(tWait);
} } catch (InterruptedException e) {return;}
}
}
//實例化類ClothSim主函數
public static void main(String[] args)
{ java.net.URL bgurl = null;
java.net.URL texurl = null;
try {
bgurl = new java.net.URL("file:bg.jpg");
texurl = new java.net.URL("file:apimage.jpg");
}
catch (java.net.MalformedURLException ex) {
System.out.println(ex.getMessage());
System.exit(1);
}//創建背景,地面紋理鏈接
new MainFrame(new ClothSim(bgurl, texurl),2000,1000);
}
//初始質點位置坐標,速度,受力
public void initializationPVF()
{
pointPosition=new Point3d[xTotal][yTotal];
vector3dV=new Vector3d[xTotal][yTotal];
vectorF=new Vector3d[xTotal][yTotal];
vectorABack=new Vector3d[xTotal][yTotal];
vectorVBack=new Vector3d[xTotal][yTotal];
pointPositionBack =new Point3d[xTotal][yTotal];
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -