2013-09-25 82 views
0

我使用starling + as3在我的移動應用程序中創建舞臺牆壁和一個盒子。Starling + Box2d - 碰撞不精確

好了,現在當我測試應用程序的盒子下降,但它不匹配牆壁,彷彿有 是一個偏移:

https://www.dropbox.com/s/hd4ehnfthh0ucfm/box.png

這裏是我創建的盒(牆壁和盒子)。

看起來好像有一個隱藏的偏移量,你怎麼看?

public function createBox(x:Number, y:Number, width:Number, height:Number, rotation:Number = 0, bodyType:uint = 0):void { 

      /// Vars used to create bodies 
      var body:b2Body; 
      var boxShape:b2PolygonShape; 
      var circleShape:b2CircleShape; 

       var fixtureDef:b2FixtureDef = new b2FixtureDef(); 
      fixtureDef.shape = boxShape; 
      fixtureDef.friction = 0.3; 
      // static bodies require zero density 
      fixtureDef.density = 0; 

      var quad:Quad; 

       bodyDef = new b2BodyDef();    
       bodyDef.type = bodyType; 
       bodyDef.position.x = x/WORLD_SCALE; 
       bodyDef.position.y = y/WORLD_SCALE; 

       // Box 
       boxShape = new b2PolygonShape(); 
       boxShape.SetAsBox(width/WORLD_SCALE, height/WORLD_SCALE); 
       fixtureDef.shape = boxShape; 
       fixtureDef.density = 0; 
       fixtureDef.friction = 0.5; 
       fixtureDef.restitution = 0.2; 

       // create the quads 
       quad = new Quad(width, height, Math.random() * 0xFFFFFF); 

       quad.pivotX = 0; 
       quad.pivotY = 0; 


       // this is the key line, we pass as a userData the starling.display.Quad 
       bodyDef.userData = quad; 

       // 
       body = m_world.CreateBody(bodyDef); 
       body.CreateFixture(fixtureDef); 

       body.SetAngle(rotation * (Math.PI/180)); 

       _clipPhysique.addChild(bodyDef.userData); 

     } 

回答

0

SetAsBox方法將半寬和半高作爲其參數。我猜你的圖形不符合你的box2d機構。因此,無論您需要使您的圖形兩倍大還是將您的SetAsBox參數乘以0.5。此外,主體樞軸將位於其中心,因此根據其樞軸位置相應地偏移您的動畫片段。

請注意,box2d有一個debugrenderer,它可以勾勒你的身體,讓你看到發生了什麼。

+0

是的我也注意到了,setasbox是照顧 – mirzahat