创建静止不动的刚体
在”掉落的苹果——b2Body“中,有一个地面,它是静止不动的,不随刚体下落(废话,地面掉落那是地震!)。我想特别说明一下静止固定刚体的创建方法。
在Box2D中,刚体是否为静止不动,由”形状需求”b2ShapeDef决定。具体的讲,质量density为0的刚体就是静止不动的。
var shapeRequest:b2PolygonDef = new b2PolygonDef(); //详细说明我们的需求 //--------------------------------- //设置密度density为0,即表示该刚体是静止不动的 shapeRequest.density = 0; //--------------------------------- shapeRequest.friction = 0.3; shapeRequest.restitution = 0.2; shapeRequest.SetAsBox(w/30, h/30);
下面的例子中,掉落的小球会被固定的挡板挡住,然后一个个掉进下面的小槽内(你可以在”创建圆形刚体“一章回顾一下小球的创建方法)
代码
package { import Box2D.Collision.b2AABB; import Box2D.Collision.Shapes.b2CircleDef; import Box2D.Collision.Shapes.b2PolygonDef; import Box2D.Common.Math.b2Vec2; import Box2D.Dynamics.b2Body; import Box2D.Dynamics.b2BodyDef; import Box2D.Dynamics.b2DebugDraw; import Box2D.Dynamics.b2World; import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; /** * http://www.ladeng6666.com * @author ladeng6666 */ public class Main extends Sprite { private var world:b2World; private var body:b2Body; private var life:Number = 0; private var interval:Number = 20; public function Main() { createWorld(); createDebug(); createBody(stage.stageWidth/2,0); createWalls(); addEventListener(Event.ENTER_FRAME, loop); stage.addEventListener(MouseEvent.MOUSE_DOWN, onStageMouseDown); } private function onStageMouseDown(e:MouseEvent):void { createBody(mouseX,mouseY); } private function loop(e:Event):void { world.Step(1 / 30, 10); if (life < interval) { life++; }else { createBody(120, 0); life = 0; } } private function createWorld():void { //1.创建一个环境 var environment:b2AABB = new b2AABB(); environment.lowerBound = new b2Vec2( -100, -100); environment.upperBound = new b2Vec2(100, 100); //2.声明重力 var gravity:b2Vec2 = new b2Vec2(0, 10); //3.睡着的对象是否模拟 var doSleep:Boolean = true; //4.创建b2World世界 world = new b2World(environment, gravity, doSleep); } private function createDebug():void { var debugSprite:Sprite = new Sprite(); addChild(debugSprite); var debugDraw:b2DebugDraw = new b2DebugDraw(); debugDraw.m_sprite = debugSprite; debugDraw.m_drawScale = 30.0; debugDraw.m_fillAlpha = 0.5; debugDraw.m_lineThickness = 1.0; debugDraw.m_drawFlags = b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit; world.SetDebugDraw(debugDraw); } private function createBody(posX:Number,posY:Number):void { //1.创建刚体需求b2BodyDef var bodyRequest:b2BodyDef = new b2BodyDef(); bodyRequest.position.Set(posX / 30, posY / 30);//记得米和像素的转换关系 //2.Box2D世界工厂更具需求创建createBody()生产刚体 body=world.CreateBody(bodyRequest); //3.创建敢提形状需求b2ShapeDef的子类 //定义形状需求是b2CircleDef圆形 var shapeRequest:b2CircleDef = new b2CircleDef(); //详细说明我们的需求 shapeRequest.density = 3; shapeRequest.friction = 0.3; shapeRequest.restitution = 0.2; shapeRequest.radius = 0.5; //4.b2Body刚体工厂根据需求createShape生产形状 body.CreateShape(shapeRequest); body.SetMassFromShapes(); } private function createWalls():void { createStaticBody(stage.stageWidth / 2, stage.stageHeight, stage.stageWidth, 30); createStaticBody(120, 100, 80, 5, Math.PI / 8); createStaticBody(240, 270, 5, 40); createStaticBody(440, 270, 5, 40); createStaticBody(340, 310, 104, 5); } private function createStaticBody(posX:Number, posY:Number, w:Number, h:Number,angle:Number=0):b2Body { //1.创建刚体需求b2BodyDef var bodyRequest:b2BodyDef = new b2BodyDef(); bodyRequest.position.Set(posX / 30, posY / 30);//记得米和像素的转换关系 bodyRequest.angle = angle; //2.Box2D世界工厂更具需求创建createBody()生产刚体 var staticBody:b2Body=world.CreateBody(bodyRequest); //3.创建敢提形状需求b2ShapeDef的子类 var shapeRequest:b2PolygonDef = new b2PolygonDef(); //详细说明我们的需求 //--------------------------------- //设置密度density为0,即表示该刚体是静止不动的 shapeRequest.density = 0; //--------------------------------- shapeRequest.friction = 0.3; shapeRequest.restitution = 0.2; shapeRequest.SetAsBox(w/30, h/30); //4.b2Body刚体工厂根据需求createShape生产形状 staticBody.CreateShape(shapeRequest); staticBody.SetMassFromShapes(); return staticBody; } } }
联系作者
为什么画出来的圆里面有一条线?
还有 有些圆会变红色,红色和灰色代表什么呢?静止和运动吗?
求解释 debugDraw.m_drawFlags = b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit;
那条线可以可以标示圆形刚体的角度,灰色表示刚体静止,不参与Box2D模拟计算,红色表示刚体为运动的,要进行物理模拟计算,专业分类可以节省CPU开支。
是不是说反了,红色表示刚体为非运动的,不进行物理模拟计算?
sorry,我讲反了,红色是静止的,呵呵