1. 程式人生 > >flash物理引擎應用 建立粒子

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

這裡寫圖片描述