[Box2D]二.让乌龟做自由落体运动
发布时间:2020-12-15 18:11:41 所属栏目:百科 来源:网络整理
导读:点这看效果 创建复合刚体 使用b2PolygonShape类中和SetAsBox()方法相似的SetAsOrientedBox()增强方法来创建矩形,此方法有4个参数, 半宽长,半高长,中心,旋转角度,中心是一个b2Vec2对象,是相对坐标。 package {import Box2D.Collision.Shapes.b2Polygon
点这看效果 创建复合刚体 使用b2PolygonShape类中和SetAsBox()方法相似的SetAsOrientedBox()增强方法来创建矩形,此方法有4个参数, 半宽长,半高长,中心,旋转角度,中心是一个b2Vec2对象,是相对坐标。 package { import Box2D.Collision.Shapes.b2PolygonShape; import Box2D.Common.Math.b2Vec2; import Box2D.Dynamics.b2Body; import Box2D.Dynamics.b2BodyDef; import Box2D.Dynamics.b2DebugDraw; import Box2D.Dynamics.b2FixtureDef; import Box2D.Dynamics.b2World; import flash.display.Sprite; import flash.events.Event; [SWF(width="640",height="480")] public class Tortoise extends Sprite { private var stageWidth:Number = 640; //舞台宽度 private var stageHeight:Number = 480; //舞台高度 private var backgroundColor:uint = 0x333333; //背景色 private var world:b2World; //Box2D世界 private var worldScale:Number = 30; //一米等于30像素 private var timeStep:Number = 1 / 30; //时间步,世界将在每一个时间步被更新 private var velIterations:int = 10; //速率约束解算器 private var posIterations:int = 10; //位置约束解算器 public function Tortoise() { drawBackground(); var gravity:b2Vec2 = new b2Vec2(0,9.81); //重力 //世界中的刚体静止时,可以允许他们进入睡眠状态,睡眠的刚体无需模拟 var sleep:Boolean = true; world = new b2World(gravity,sleep); debugDraw(); createFloor(); createTortoise(); addEventListener(Event.ENTER_FRAME,updateWorld); } //创建传说中的乌龟 private function createTortoise():void { //乌龟壳由9个矩形组成,以下两个变量分别是矩形的半宽长,半高长 var halfWidth:Number = 10 /worldScale; var halfHeight:Number = 10 /worldScale; //乌龟头的半宽长,半高长 var headHalfWidth:Number = 7 /worldScale; var headHalfHeight:Number = 7 /worldScale; var x:Number = stageWidth/2/worldScale; var y:Number = 100/worldScale; var bodyDef:b2BodyDef = new b2BodyDef();//刚体定义 bodyDef.type = b2Body.b2_dynamicBody; //刚体类型为动态 //设置刚体坐标,Box2D坐标用米表示,注册点在中心 bodyDef.position.Set(x,y); var polygonShape:b2PolygonShape = new b2PolygonShape(); //创建多边形 polygonShape.SetAsBox(halfWidth,halfHeight);//创建乌龟壳最中间的那个矩形 var fixtureDef:b2FixtureDef = new b2FixtureDef(); //夹具(fixture)用于将形状绑定到刚体上 fixtureDef.density = 1; //密度 fixtureDef.restitution = 0.4; //弹性系数 fixtureDef.friction = 0.5; //摩擦系数 fixtureDef.shape = polygonShape; var theBody:b2Body = world.CreateBody(bodyDef); theBody.CreateFixture(fixtureDef); var pos:b2Vec2 = new b2Vec2(-20/worldScale,-20/worldScale); //相对坐标 //创建乌龟壳剩余的8个矩形 polygonShape.SetAsOrientedBox(halfWidth,halfHeight,new b2Vec2(-2 * halfWidth,-2 * halfHeight)); theBody.CreateFixture(fixtureDef); polygonShape.SetAsOrientedBox(halfWidth,new b2Vec2(0,new b2Vec2(2 * halfWidth,-2 * halfHeight)); theBody.CreateFixture(fixtureDef); polygonShape.SetAsOrientedBox(halfWidth,0)); theBody.CreateFixture(fixtureDef); polygonShape.SetAsOrientedBox(halfWidth,0)); theBody.CreateFixture(fixtureDef); polygonShape.SetAsOrientedBox(halfWidth,2 * halfHeight)); theBody.CreateFixture(fixtureDef); polygonShape.SetAsOrientedBox(halfWidth,2 * halfHeight)); theBody.CreateFixture(fixtureDef); //创建乌龟头 polygonShape.SetAsOrientedBox(headHalfWidth,headHalfHeight,-(3 * halfHeight + headHalfHeight))); theBody.CreateFixture(fixtureDef); //创建乌龟的4只脚 polygonShape.SetAsOrientedBox(8/worldScale,8/worldScale,new b2Vec2(-3 * halfWidth,-3 * halfHeight),Math.PI / 4); theBody.CreateFixture(fixtureDef); polygonShape.SetAsOrientedBox(8/worldScale,new b2Vec2(3 * halfWidth,3 * halfHeight),Math.PI / 4); theBody.CreateFixture(fixtureDef); } //创建地面 private function createFloor():void { var bodyDef:b2BodyDef = new b2BodyDef(); //刚体定义 bodyDef.type = b2Body.b2_staticBody; //刚体类型为静态 //设置刚体坐标,Box2D坐标用米来表示,注册点是中心 bodyDef.position.Set(stageWidth/2/worldScale,(stageHeight-10)/worldScale); var polygonShape:b2PolygonShape = new b2PolygonShape(); //创建多边形 polygonShape.SetAsBox(stageWidth/2/worldScale,10/worldScale);//轴对称的矩形,半宽长和半高长 var fixtureDef:b2FixtureDef = new b2FixtureDef(); //夹具(fixture)用于将形状绑定到刚体上 fixtureDef.density = 1; //密度 fixtureDef.restitution = 0.3; //弹性系数 fixtureDef.friction = 0.1; //摩擦系数 fixtureDef.shape = polygonShape; var theFloorBody:b2Body = world.CreateBody(bodyDef); //创建刚体 theFloorBody.CreateFixture(fixtureDef); } private function updateWorld(e:Event):void { world.Step(timeStep,velIterations,posIterations); world.ClearForces(); world.DrawDebugData(); } private function drawBackground():void { graphics.beginFill(backgroundColor); graphics.drawRect(0,stageWidth,stageHeight); graphics.endFill(); } private function debugDraw():void { var debugDraw:b2DebugDraw = new b2DebugDraw(); //调试绘图类 var debugSprite:Sprite = new Sprite(); //显示调试绘图数据的画布 addChild(debugSprite); debugDraw.SetSprite(debugSprite); //告知debugSprite将要被用来显示调试绘图 debugDraw.SetDrawScale(worldScale); //允许我们决定将在调试绘图(debug draw)中描绘的物 //理实体的类型。此刻,我们只需要绘制形状 debugDraw.SetFlags(b2DebugDraw.e_shapeBit); debugDraw.SetFillAlpha(0.5); world.SetDebugDraw(debugDraw); } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |