2011年7月15日 星期五

Box2D flash tutorial II–Add shape

接著我們來我的世界中加上二個物件(Shape)

一個是固定的(b2_staticBody),一個是被地心引力往下吸的(b2_dynamicBody)

首先先來看一下程式

  1: package
  2: {
  3:     import Box2D.Collision.Shapes.b2PolygonShape;
  4:     import Box2D.Common.Math.b2Vec2;
  5:     import Box2D.Dynamics.b2Body;
  6:     import Box2D.Dynamics.b2BodyDef;
  7:     import Box2D.Dynamics.b2DebugDraw;
  8:     import Box2D.Dynamics.b2FixtureDef;
  9:     import Box2D.Dynamics.b2World;
 10: 
 11:     import flash.display.Sprite;
 12:     import flash.events.Event;
 13: 
 14:     public class Initialization extends Sprite
 15:     {
 16:         private var _world:b2World;
 17:         private var scale:uint=30;
 18: 
 19:         public function Initialization()
 20:         {
 21:             super();
 22:             this.addEventListener(Event.ENTER_FRAME, function():void
 23:             {
 24:                 _world.Step(1 / 30, 10, 10);
 25:                 _world.ClearForces();
 26:                 _world.DrawDebugData();
 27:             });
 28: 
 29:             this._world=new b2World(new b2Vec2(0, 1), true);
 30: 
 31: 
 32:             var dbgDraw:b2DebugDraw=new b2DebugDraw();
 33: 
 34:             dbgDraw=new b2DebugDraw();
 35:             dbgDraw.SetSprite(this);
 36:             dbgDraw.SetDrawScale(30.0);
 37:             dbgDraw.SetFillAlpha(0.5);
 38:             dbgDraw.SetLineThickness(1.0);
 39:             dbgDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit);
 40: 
 41:             _world.SetDebugDraw(dbgDraw);
 42: 
 43:             this.fixRect(); //add fix shape
 44:             this.dinamicRect(); //add dynamic shape
 45:         }
 46: 
 47:         private function fixRect():void
 48:         {
 49:             var bd:b2BodyDef=new b2BodyDef();
 50:             bd.position.Set(10, 10);
 51: 
 52:             var rect:b2PolygonShape=new b2PolygonShape();
 53:             rect.SetAsBox(2, 2);
 54: 
 55:             var body:b2Body=_world.CreateBody(bd);
 56: 
 57:             var fd:b2FixtureDef=new b2FixtureDef();
 58:             fd.shape=rect;
 59:             fd.friction=0.3;
 60:             fd.density=0;
 61: 
 62:             body.CreateFixture(fd);
 63:         }
 64: 
 65:         private function dinamicRect():void
 66:         {
 67:             var bd:b2BodyDef=new b2BodyDef();
 68:             bd.position.Set(5, 5);
 69:             bd.type=b2Body.b2_dynamicBody;
 70: 
 71:             var rect:b2PolygonShape=new b2PolygonShape();
 72:             rect.SetAsBox(2, 2);
 73: 
 74:             var body:b2Body=_world.CreateBody(bd);
 75: 
 76:             var fd:b2FixtureDef=new b2FixtureDef();
 77:             fd.shape=rect;
 78:             fd.friction=0.3;
 79:             fd.density=0;
 80: 
 81:             body.CreateFixture(fd);
 82:         }
 83:     }
 84: }
 85: 


 



在line29我們先把地方引力調小,這樣在往下掉的時侯會掉的慢點



line43加上一個固定的方塊



line44加上一個會隨地心引力的方塊



接著來說明如何把一個物件(shape)加到世界上



要加到世界上主要是有四個動作




  1. b2bodyDef指定其相關資料或屬性 default type:b2_staticBody


  2. 建立 b2XXXShape ,並設定共屬性


  3. 由世界建立一個身體(b2Body)


  4. 設定 b2FixtureDef 的各摩擦等各屬性


  5. 由建立出的身體把固定物(b2FixtureDef )做建立



做完以上的動作即完成物件加到世界上



下一篇我們來把一些自定的flash物件加到整個世界上

沒有留言:

張貼留言