APE物理引擎初探+執行個體

來源:互聯網
上載者:User

先去
http://www.cove.org/ape/index.htm


下載作者的最新版APE
然後開啟說明文檔 docs/api/index.html
這裡一共十二個類,看起來很少,所以初學者可以更有信心了。
***************************************************************************************************
先簡單介紹一下:
AbstractCollection
  所有群組的抽象類別
AbstractConstraint
  所有物理相互作用的的抽象類別
AbstractItem
  所有相互作用(碰撞)、粒子的基類
AbstractParticle
  關於粒子的基類
APEngine
主引擎、力的類
CircleParticle
  圓形粒子
RectangleParticle
  矩形粒子
Composite
  可以包含粒子和碰撞的複合物類
Group
一個組的類,可以包含粒子、碰撞、複合物
SpringConstraint
兩個粒子之間類似彈性碰撞的類(彈簧)
Vector  

WheelParticle  
一個粒子,類比輪子行為
********************************************************************************************
這些類的繼承關係:
AbstractCollection
  所有群組的抽象類別
              Composite
  可以包含粒子和碰撞的複合物類
              Group
一個組的類,可以包含粒子、碰撞、複合物
AbstractItem
  所有相互作用(碰撞)、粒子的基類
              AbstractConstraint
  所有物理相互作用的的抽象類別
                             SpringConstraint
兩個粒子之間類似彈性碰撞的類(彈簧)
              AbstractParticle
  關於粒子的基類
                             RectangleParticle
  矩形粒子
                             CircleParticle
  圓形粒子
                                           WheelParticle
  一個粒子,類比輪子行為
APEngine
主引擎、力的類
Vector

OK,對這個物理引擎,我們已經有了初步認識了,下面我們就利用它實現一個簡單的小例子,同時瞭解它的用法:
先看下這個例子的效果

用WSAD可以給其中一個球的軸方向加力,這個球的品質有別於其他的球。
下面的幾個按鈕
可以給整個容器加力,先試一下效果吧~  ^&^
http://blog.5d.cn/user47/hzq1122/upload/2008-06/myP.swf



***************************************************************************************************************
下面開始製作:
我用的工具
是Flex,所以以Flex製作為例,如果還沒安裝,就搜一下,盡量安裝eclipse的Flex外掛程式版
****************************************************************************************************************
這裡我建了1個類,作為邊界框。代碼
如下:
package
{
import org.cove.ape.*; //匯入包
public class Floor extends Group
{
  public function Floor()
  {
//定義矩形粒子,括弧內參數依次為:橫座標,縱座標,寬度,高度,弧度,是否固定。
   var diban1:RectangleParticle=new RectangleParticle(250,0,400,10,0,true)
   var diban2:RectangleParticle=new RectangleParticle(250,400,400,10,0,true)
   var diban3:RectangleParticle=new RectangleParticle(50,200,400,10,Math.PI/2,true)
   var diban4:RectangleParticle=new RectangleParticle(450,200,400,10,Math.PI/2,true)
//矩形粒子的父類AbstractItem的setStyle方法。
   diban1.setStyle(1,0x006699,1,0x663366,1)
   diban2.setStyle(1,0x006699,1,0x663366,1)
   diban3.setStyle(1,0x006699,1,0x663366,1)
   diban4.setStyle(1,0x006699,1,0x663366,1)
//添加該粒子,(就當它是addChild理解吧)
   addParticle(diban1);
   addParticle(diban2);
   addParticle(diban3);
   addParticle(diban4);
  }
}
}
ok,邊界框的類寫完了。下面寫mxml檔案

****************************************************************************************************************
源碼

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicati>
<mx:Script>
  <![CDATA[
   import mx.core.UIComponent;
   import org.cove.ape.*;
//Flex需要UIComponent做容器,所以定義一個作為物理引擎的容器
   private var APEcontent:UIComponent;
   private var heros:Group
   private var floors:Floor
   private var others:Array
   
   private var hero:CircleParticle
   
   private function GameTest():void {
//幀頻設高點會更流暢。
   stage.frameRate = 100
   heros=new Group();
   floors=new Floor();
   others=new Array();
   APEcontent = new UIComponent();
   addChild(APEcontent)
   APEngine.init(0.25);
   APEngine.container=APEcontent;

//定義10個球
   for (var i:int=0;i<10;i++){
    var other:Group=new Group()
    APEngine.addGroup(other)
    other.addParticle(new CircleParticle(Math.random()*100+50,Math.random()*100+20,10))
    others
=other
//使這個球與組內的其他成員產生作用
    other.addCollidableList(others)
   }
   
   APEngine.addGroup(heros);
   hero=new CircleParticle(150,20,10,false,2);
   heros.addParticle(hero)
//使這個hero與others內的成員產生作用
   heros.addCollidableList(others)
   APEngine.addGroup(floors);
   floors.addCollidable(heros)
//同上類似
   floors.addCollidableList(others)
   
   stage.addEventListener(KeyboardEvent.KEY_DOWN,onD);
   stage.addEventListener(Event.ENTER_FRAME,onF);
  }
  public function onD(e:KeyboardEvent):void {
   switch (e.keyCode) {
    case "W".charCodeAt() :
     hero.addForce(new Vector(0,-3));
//addForce和addMasslessForce的區別,在於是否忽略品質
     //hero.addMasslessForce(new Vector(0,-3));
     break;
    case "S".charCodeAt() :
     hero.addForce(new Vector(0,3));
     break;
    case "A".charCodeAt() :
     hero.addForce(new Vector(-3,0));
     break;
    case "D".charCodeAt() :
     hero.addForce(new Vector(3,0));
     break;
   }
  }
  public function onF(e:Event):void {
   //重繪
   APEngine.step();
   APEngine.paint();
  }
//建立幾個按鈕,點擊執行如下方法
  private function left():void{
   APEngine.addMasslessForce(new Vector(-3,0));
  }
  private function right():void{
   APEngine.addForce(new Vector(3,0));
  }
  private function up():void{
   APEngine.addForce(new Vector(0,-3));
  }
  private function down():void{
   APEngine.addForce(new Vector(0,3));
  }
  private function nothing():void{
   APEngine.addForce(new Vector(0,0));
  }
  ]]>
</mx:Script>
<mx:Button x="10" y="410" label="left" click="left()"/>
<mx:Button x="64" y="410" label="right" click="right()"/>
<mx:Button x="125" y="410" label="up" click="up()"/>
<mx:Button x="173" y="410" label="down" click="down()"/>
<mx:Button x="237" y="410" label="nothing" click="nothing()"/>
</mx:Application>

****************************************************************************************************
這個過程就完成了,再提一下AbstractParticle
這個類最常用的屬性
和方法
center : Vector

[read-only] Returns A Vector of the current location of the particle
返回加在當前對象身上的力
collidable : Boolean

Determines if the particle can collide with other particles or constraints.
決定當前粒子是否與其他粒子或作用產生作用
elasticity : Number

The elasticity of the particle.
彈性
fixed : Boolean

The fixed state of the particle.
是否固定
friction : Number

The surface friction of the particle.
粒子表面摩擦力
mass : Number

The mass of the particle.
品質
px : Number

The x position of this particle
py : Number

The y position of this particle
*************************************************************************************************************
現在你可以延伸一下,嘗試著做出一個類似撞球的遊戲
了!簡單吧!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.