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 ?- 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 ?
- 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 ?- 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()
- }
- }
- }
演示效果: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 就是相當於它們的基類,從而實現瞭解這種層級關係。為我們實現打下一些基礎