flash物理引擎应用 创建粒子

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

   刚刚接触这个引擎的时候,感觉很强大,在一些演示方面已经有不错的表现。只是这些都是外国一些类库,相对来讲,英语说明还是让人看得很头疼。的确很多时候,不了解整个架构的时候,只可以一点点摸索,一点点记录下来吧。

看看这个类包:com.fileitup.fisixengine.particles

粒子包

在文档api里面有四种大概的粒子,按字面意思圆形粒子,导航粒子(应该不太准备),方形粒子,还有一个还是转动粒子

圆形粒子:有坐标轴,中心线的动态圆

导航粒子:用于连接刚体物体

方形粒子; 处理多边形

转动粒子:跟圆形粒子差不多,但它能转动,

尝试一下创建一个简单的粒子:当我们打开这些包的时候,正是要去试试有什么效果,大概会知道这些是什么意思。毕竟外语很难把握就得去试试。

按习惯,我们先看看第一种粒子:CircleParticle

1.它的构造函数

CircleParticle(x:Number, y:Number, rad:Number)
Creates an instance of a CircleParticle
创建一个独立的圆形粒子

 粒子函数

它有很多的方法,我们尝试一些简单的看看试试能不能发生事情。

 首先导入我们想要的包

 import com.fileitup.fisixengine.core.FisixEngine;
 import com.fileitup.fisixengine.particles.CircleParticle;
 

使用flex来创建一个as 工程。名为Example

[c-sharp] view plain copy print ?
  1. package  
  2. {  
  3.     import com.fileitup.fisixengine.core.FisixEngine;  
  4.     import com.fileitup.fisixengine.particles.CircleParticle;  
  5.       
  6.     import flash.display.MovieClip;  
  7.       
  8.     /**In this example, a jello-type object is created and manipulated by the mouse 
  9.      */  
  10.     [SWF(width='700',height='500',backgroundColor='0x50464b',frameRate='30')]  
  11.     public class Example  extends MovieClip{  
  12.         private var myEngine:FisixEngine  
  13.       
  14.         public function Example (){  
  15.               
  16.             myEngine = new FisixEngine()  
  17.              myEngine.setGravity(0,1)  
  18.             var circle:CircleParticle=myEngine.newCircleParticle(300,30,30);  
  19.             //circle.bounce=0.7;  
  20.              
  21.             myEngine.setRender(true);  
  22.             //tell the engine where to render to  
  23.             myEngine.setRenderGraphics(graphics)  
  24.             myEngine.startEngine(30);  
  25.                
  26.         }  
  27.           
  28.        
  29.     }  
  30. }  
package{ import com.fileitup.fisixengine.core.FisixEngine; import com.fileitup.fisixengine.particles.CircleParticle;  import flash.display.MovieClip;  /**In this example, a jello-type object is created and manipulated by the mouse  */ [SWF(width='700',height='500',backgroundColor='0x50464b',frameRate='30')] public class Example  extends MovieClip{  private var myEngine:FisixEngine   public function Example (){      myEngine = new FisixEngine()    myEngine.setGravity(0,1)      var circle:CircleParticle=myEngine.newCircleParticle(300,30,30);      //circle.bounce=0.7;        myEngine.setRender(true);   //tell the engine where to render to   myEngine.setRenderGraphics(graphics)            myEngine.startEngine(30);      }     }}

程序入口:

    myEngine = new FisixEngine()//创建一个物理引擎,
    myEngine.setGravity(0,1)//指定他的重力

2.创建一个圆:

var circle:CircleParticle=myEngine.newCircleParticle(300,30,30); //这一句,我猜想是为这个物理引擎创建一个单独的圆,半径为30,坐标(300,30)。

接下来,启动引擎,渲染物体,设置引擎的帧速度

myEngine.setRender(true);
       myEngine.setRenderGraphics(graphics)
            myEngine.startEngine(30);

一个简单的圆似乎已经完成了。看看效果如何:为了显示方便,我将半径放大

演示

在我看来,能弄出这个圆是一件很让人值得高兴的事情,因为我探索之路正在开始了。后面还会更加精彩:

还可以设置碰撞墙,弹力等效果。

[c-sharp] view plain copy print ?
  1. package     
  2. {     
  3.     import com.fileitup.fisixengine.core.FisixEngine;  
  4.     import com.fileitup.fisixengine.particles.CircleParticle;  
  5.     import com.fileitup.fisixengine.utils.BoundingBox;  
  6.       
  7.     import flash.display.MovieClip;     
  8.          
  9.     /**In this example, a jello-type object is created and manipulated by the mouse   
  10.      */    
  11.     [SWF(width='700',height='500',backgroundColor='0x50464b',frameRate='30')]     
  12.     public class Example2  extends MovieClip{     
  13.         private var myEngine:FisixEngine     
  14.          
  15.         public function Example2 (){     
  16.                  
  17.             myEngine = new FisixEngine()     
  18.             myEngine.setGravity(0,1)    
  19.             myEngine.setBounds(new BoundingBox(0,0,700,500));  
  20.             myEngine.boundsCollisions=true;  
  21.             var circle:CircleParticle=myEngine.newCircleParticle(300,30,30);     
  22.              circle.bounce=0.7;     
  23.                 
  24.             myEngine.setRender(true);     
  25.             //tell the engine where to render to     
  26.             myEngine.setRenderGraphics(graphics)     
  27.             myEngine.startEngine(30);     
  28.                   
  29.         }     
  30.              
  31.           
  32.     }     
  33. }    
package   {       import com.fileitup.fisixengine.core.FisixEngine;    import com.fileitup.fisixengine.particles.CircleParticle;    import com.fileitup.fisixengine.utils.BoundingBox;        import flash.display.MovieClip;              /**In this example, a jello-type object is created and manipulated by the mouse       */      [SWF(width='700',height='500',backgroundColor='0x50464b',frameRate='30')]       public class Example2  extends MovieClip{           private var myEngine:FisixEngine                  public function Example2 (){                              myEngine = new FisixEngine()               myEngine.setGravity(0,1)              myEngine.setBounds(new BoundingBox(0,0,700,500));            myEngine.boundsCollisions=true;            var circle:CircleParticle=myEngine.newCircleParticle(300,30,30);                circle.bounce=0.7;                             myEngine.setRender(true);               //tell the engine where to render to               myEngine.setRenderGraphics(graphics)               myEngine.startEngine(30);                           }                          }   } 

再进化一下:可以弄出下面的效果

[c-sharp] view plain copy print ?
  1. package  
  2. {  
  3.     import com.fileitup.fisixengine.constraints.SpringConstraint;  
  4.     import com.fileitup.fisixengine.core.FisixEngine;  
  5.     import com.fileitup.fisixengine.core.FisixObject;  
  6.     import com.fileitup.fisixengine.utils.BoundingBox;  
  7.       
  8.     import flash.display.Bitmap;  
  9.     import flash.display.MovieClip;  
  10.     import flash.events.Event;  
  11.     import flash.text.TextField;  
  12.       
  13.     /**In this example, a jello-type object is created and manipulated by the mouse 
  14.      */  
  15.     [SWF(width='700',height='500',backgroundColor='0x50464b',frameRate='30')]  
  16.     [Embed (source="1.jpg")]  
  17.     public class Example2  extends MovieClip{  
  18.         private var myEngine:FisixEngine  
  19.         private var txtFPS:TextField  
  20.           
  21.         public function Example2 (){  
  22.             //first, create an instance of the fisixengine object  
  23.            
  24.            
  25.             myEngine = new FisixEngine()  
  26.               
  27.             //bound all of the objects in the simulation within a box  
  28.             myEngine.setBounds(new BoundingBox(0,0,700,500))  
  29.             //enable collisions with the bounding box  
  30.             myEngine.boundsCollisions=true  
  31.               
  32.             //set the gravity to pull down at a rate of 1 pixel per second  
  33.             myEngine.setGravity(0,1)  
  34.               
  35.             //create a jello cube  
  36.             var jello:FisixObject = myEngine.newFisixObject();  
  37.                
  38.             for(var i:uint=0;i<12;i++)  
  39.             jello.newCircleParticle(250+100*Math.cos(i*30),200+100*Math.sin(i*30),20);  
  40.              SpringConstraint.constraintAll(jello,jello.particles,.9);  
  41.    
  42.             //attach the corner of the jello to the mouse  
  43.              myEngine.newMouseAttacher(jello.particles[0],root,3)  
  44.   
  45.             //turn on primitive rendering  
  46.             myEngine.setRender(true);  
  47.             //tell the engine where to render to  
  48.             myEngine.setRenderGraphics(graphics)  
  49.   
  50.             addEventListener(Event.ENTER_FRAME,onEnterFrame)  
  51.               
  52.             txtFPS = new TextField()  
  53.              addChild(txtFPS)  
  54.         }  
  55.           
  56.        
  57.         private function onEnterFrame(e:Event):void{  
  58.             myEngine.mainLoop(1)  
  59.   
  60.             txtFPS.text = int(myEngine.getRealFPS()).toString()  
  61.         }  
  62.     }  
  63. }  
package{ import com.fileitup.fisixengine.constraints.SpringConstraint; import com.fileitup.fisixengine.core.FisixEngine; import com.fileitup.fisixengine.core.FisixObject; import com.fileitup.fisixengine.utils.BoundingBox;  import flash.display.Bitmap; import flash.display.MovieClip; import flash.events.Event; import flash.text.TextField;  /**In this example, a jello-type object is created and manipulated by the mouse  */ [SWF(width='700',height='500',backgroundColor='0x50464b',frameRate='30')] [Embed (source="1.jpg")] public class Example2  extends MovieClip{  private var myEngine:FisixEngine  private var txtFPS:TextField    public function Example2 (){   //first, create an instance of the fisixengine object         myEngine = new FisixEngine()      //bound all of the objects in the simulation within a box   myEngine.setBounds(new BoundingBox(0,0,700,500))   //enable collisions with the bounding box   myEngine.boundsCollisions=true      //set the gravity to pull down at a rate of 1 pixel per second   myEngine.setGravity(0,1)      //create a jello cube   var jello:FisixObject = myEngine.newFisixObject();       for(var i:uint=0;i<12;i++)   jello.newCircleParticle(250+100*Math.cos(i*30),200+100*Math.sin(i*30),20);    SpringConstraint.constraintAll(jello,jello.particles,.9);    //attach the corner of the jello to the mouse    myEngine.newMouseAttacher(jello.particles[0],root,3)   //turn on primitive rendering   myEngine.setRender(true);   //tell the engine where to render to   myEngine.setRenderGraphics(graphics)   addEventListener(Event.ENTER_FRAME,onEnterFrame)      txtFPS = new TextField()    addChild(txtFPS)  }      private function onEnterFrame(e:Event):void{   myEngine.mainLoop(1)   txtFPS.text = int(myEngine.getRealFPS()).toString()  } }}

lizi

演示效果:http://files.cnblogs.com/hero82748274/Example.swf

二、类关系:

Package com.fileitup.fisixengine.particles
Class public class CircleParticle
Inheritance CircleParticle --> Particle --> CollisionObject
Subclasses CircleParticleConveyor, Projectile, WheelParticle

 可以看出CircleParticle的类,继承了Particle,而Partilce 继承了 CollisionObject

CollisionObject 就是相当于它们的基类,从而实现了解这种层级关系。为我们实现打下一些基础

           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

这里写图片描述

猜你喜欢

转载自blog.csdn.net/truhfcg/article/details/83952289
今日推荐