Box2D 坠落的汉字
正在熟悉Box2D中。这里看演示。Box2D中建立一个刚体比较复杂,我写了一个快速建立Box的函数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | public function createBox(wx:Number, wy:Number, wWidth:Number, wHeight:Number, wAngle:Number = 0, isWall:Boolean = false) { var wall:Shape = new Shape() wall.graphics.beginFill(0); wall.graphics.drawRect( -wWidth * .5, -wHeight * .5, wWidth, wHeight) wall.graphics.endFill() addChild(wall) boxDef = new b2PolygonDef() boxDef.SetAsBox(wWidth * bm, wHeight * bm) boxDef.density = isWall?0:1 bodyDef = new b2BodyDef() bodyDef.position.Set(wx * bp, wy * bp) bodyDef.angle = wAngle*Math.PI/180 bodyDef.userData = wall body = world.CreateBody(bodyDef) body.CreateShape(boxDef) body.SetMassFromShapes() } |
isWall为true则将物体设为静止(不受外力影响)。bm和bp是两个转换系数,bm =1/30,bp=1/60(Box2D里1单位=30像素,默认定位是物体中心点)
![]()