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

📄 particles.java

📁 用java3d做的一个粒子系统可以控制粒子运动
💻 JAVA
字号:
/*  Particle System    programming: Jeff White (jsw@cs.brown.edu)  particle algorithm: Steven Dollins (scd@cs.brown.edu)    usage: particles [options] command line options   defaults    interactive keys  -pointsize {1-8}       3           number keys 1-8  -numparts {0-32}       16          '[' and ']' to inc/dec by 1                                      number of particles is 1.415^n  -starttime float       seconds since epoch  -speed float           1.0         ',' and '.' to inc/dec by 0.05                                     'm' and '/' jump forward/back by 5 sec  -xrot degrees          0.0         up and down arrows to rot by 5 degrees  -zrot degrees          0.0         left and right arrows  -dots                              ' ' (space) toggles between  -lines                                      dots and lines  -box                   off         'b' to toggle bounding box display  -help                              '?' to print statistics  Performance:  long ago: I get ~23fps retained and ~9fps immediate  2/23/98  using big linearray instead of individual shapes   pwr=12   sol retained 60fps   sol immediate 48fps   nt  java retained 50   nt java imm 40   nt jre retained 120   nt jre imm 90   pwr=18   nt jre retained 30fps       */import javax.media.j3d.*;import javax.vecmath.*;import java.util.Enumeration;import com.sun.j3d.utils.applet.MainFrame;import com.sun.j3d.utils.universe.SimpleUniverse;import java.applet.Applet;import java.awt.*;import java.awt.event.*;public final class Particles extends Applet {    Switch switcher;     static final double NUM_POINTS_BASE=1.415;    static int numPtsPwr=22;    static int numPoints;    static double timeSpeed=1.0;    static double localTime=0;    static double lastTime=0;        static double x_rot=0;    static double z_rot=0;    static double z_rot_vel=0;    static double[] SIN,COS;    static final int MODE_LINES=0;    static final int MODE_DOTS=1;    static int render_mode=MODE_DOTS;    static LineShape lineShape;    static Shape3D lShape;    static PointShape pointShape;    static Shape3D pShape;    static int pointSize=3;    static {        COS = new double[65536];        SIN = new double[65536];	for(int i=0;i<65536;i++){            double t= ((double) i) * Math.PI*2 / 65536;            COS[i]=Math.cos(t);            SIN[i]=Math.sin(t);        }    }        void setPointSize(int i){	pointSize=i;        if(pShape!=null) {            PointAttributes pattrib=new PointAttributes();            pattrib.setPointSize(i);            pShape.getAppearance().setPointAttributes(pattrib);        }    }        void setNumPoints(int i){        synchronized(this){            numPoints=i;            pointShape=new PointShape(i);            pShape.setGeometry(pointShape);	    lineShape = new LineShape(i);	    lShape.setGeometry(lineShape);        }    }    void increasePoints(){ 	setNumPoints((int) Math.pow(NUM_POINTS_BASE,++numPtsPwr));    }    void decreasePoints(){	setNumPoints((int) Math.pow(NUM_POINTS_BASE,--numPtsPwr));    }    void setRenderMode(int mode){	render_mode=mode;        switcher.setWhichChild(render_mode);    }        void switchem(){        if(render_mode==0) setRenderMode(1);        else               setRenderMode(0);    }    void pause(){	timeSpeed=0;    }    void slower(){	timeSpeed-=.05;     }    void faster(){	timeSpeed+=.05;    }    void backward(){	localTime += timeSpeed * -10;    }    void forward(){	localTime += timeSpeed * 10;     }        Shape3D createLineShape(int numPoints){        lineShape=new LineShape(numPoints);	lShape = new Shape3D(lineShape);	lShape.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);        return lShape;    }        Shape3D createPointShape(int numPoints){        Appearance app=new Appearance();        app.setCapability(app.ALLOW_POINT_ATTRIBUTES_READ);        app.setCapability(app.ALLOW_POINT_ATTRIBUTES_WRITE);        PointAttributes pattrib=new PointAttributes();        pattrib.setPointSize(pointSize);        app.setPointAttributes(pattrib);        pointShape=new PointShape(numPoints);        pShape=new Shape3D(pointShape,app);        pShape.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);        pShape.setCapability(Shape3D.ALLOW_APPEARANCE_READ);        return pShape;    }        public Particles(){        setLayout(new BorderLayout());        Canvas3D c;        BranchGroup root = new BranchGroup();        numPoints = (int) Math.pow(NUM_POINTS_BASE,numPtsPwr);        switcher = new Switch();        switcher.setCapability(Switch.ALLOW_SWITCH_WRITE);        switcher.setWhichChild(render_mode);        switcher.addChild(createLineShape(numPoints));        switcher.addChild(createPointShape(numPoints));	c=new Canvas3D(null);	root.addChild(new ParticleBehavior());	XRot xrot = new XRot();	root.addChild(xrot);	        xrot.addChild(switcher);/*  	if("true".equals(System.getProperty("fps")))	    root.addChild(new FPS());*/        root.addChild(new KeyboardHandler());        root.compile();        add("Center",c);	Frame f = new Frame("ControlPanel");	Component controlPanel;/*	if("true".equals(System.getProperty("swing")))	    controlPanel=new ControlPanel(this);	else	    controlPanel=new AWTControlPanel(this);*/	 controlPanel=new AWTControlPanel(this);	//controlPanel.setBackground(new Color(120,120,120));	//controlPanel.setForeground(Color.orange);		f.add(controlPanel);	f.pack(); f.show(); 	SimpleUniverse u = new SimpleUniverse(c);	TransformGroup tg=u.getViewingPlatform().getViewPlatformTransform();	Transform3D t = new Transform3D();	t.setTranslation(new Vector3d(0,0,6));	tg.setTransform(t);	        u.addBranchGraph(root);    }    final class ParticleBehavior extends Behavior {        private final WakeupCondition w=new WakeupOnElapsedFrames(0);        public void initialize(){            setSchedulingBounds(new BoundingSphere(new Point3d(),1000));            wakeupOn(w);        }        public void processStimulus(Enumeration criteria){            draw();            wakeupOn(w);        }        final void draw(){	    double newTime=System.currentTimeMillis()*.001;            double dt=newTime-lastTime;            lastTime=newTime;                        localTime+=timeSpeed*dt;	    switch(render_mode) {            case MODE_DOTS:                pointShape.update(localTime);                break;            case MODE_LINES:                lineShape.update(localTime);                break;            }        }                }    public static void main(String[] argv){        for(int i=0;i<argv.length;i++){            if(argv[i].equals("-help")) {                System.out.println("usage: particles [options]");                System.out.println("command line options   defaults    interactive keys");                System.out.println("-pointsize {1-8}       3           number keys 1-8");                System.out.println("-numparts {0-32}       16          '[' and ']' to inc/dec by 1");                System.out.println("                                   number of particles is 1.415^n");                System.out.println("-starttime float       seconds since epoch");                System.out.println("-speed float           1.0         ',' and '.' to inc/dec by 0.05");                System.out.println("                                   'm' and '/' jump forward/back by 5 sec");                System.out.println("-xrot degrees          0.0         up and down arrows to rot by 5 degrees");                //System.out.println("-zrot degrees          0.0         left and right arrows");                System.out.println("-dots                              (space) toggles between");                System.out.println("-lines                                      dots and lines");                //System.out.println("-box                   off         'b' to toggle bounding box display");                System.out.println("-help                              '?' to print statistics");                System.exit(0);            }            else if(argv[i].equals("-pointsize")) pointSize=Integer.parseInt(argv[++i]);            else if(argv[i].equals("-numparts")) numPtsPwr=Integer.parseInt(argv[++i]);            else if(argv[i].equals("-starttime")) localTime=Integer.parseInt(argv[++i]);            else if(argv[i].equals("-speed")) timeSpeed=Float.valueOf(argv[++i]).floatValue();            else if(argv[i].equals("-xrot")) x_rot=Double.valueOf(argv[++i]).doubleValue();            //else if(argv[i].equals("-zrot")) z_rot=Double.valueOf(argv[++i]).doubleValue();            else if(argv[i].equals("-dots")) render_mode=MODE_DOTS;            else if(argv[i].equals("-lines")) render_mode=MODE_LINES;        }    	Frame f = new MainFrame(new Particles(),512,512);	f.setTitle("Particles");	f.setLocation(200,15);    }    final class KeyboardHandler extends Behavior {        private final WakeupCondition w=new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED);                public void initialize(){            setSchedulingBounds(new BoundingSphere(new Point3d(),1000));            wakeupOn(w);        }                public void processStimulus(Enumeration criteria){            keyPress((KeyEvent)((WakeupOnAWTEvent)criteria.nextElement()).getAWTEvent()[0]);            wakeupOn(w);        }                void keyPress(KeyEvent e){            int key=e.getKeyCode();            switch(key){            case e.VK_0: pause(); break;            case e.VK_1: case e.VK_2: case e.VK_3: case e.VK_4: case e.VK_5: case e.VK_6: case e.VK_7:             case e.VK_8: setPointSize(key-e.VK_0); break;            case e.VK_OPEN_BRACKET: decreasePoints(); break;            case e.VK_CLOSE_BRACKET: increasePoints(); break;            case e.VK_SPACE: switchem(); break;            case e.VK_COMMA: slower(); break;            case e.VK_PERIOD: faster(); break;            case e.VK_Q: System.exit(0); break;            case e.VK_SLASH: forward(); break;            case e.VK_M:     backward(); break;		//case e.VK_UP: x_rot -= Math.PI/180*5; break;		//case e.VK_DOWN: x_rot += Math.PI/180*5; break;            case e.VK_LEFT: z_rot_vel -= 2.5; break;            case e.VK_RIGHT: z_rot_vel += 2.5; break;            }        }    }}

⌨️ 快捷键说明

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