[Box2D]三.创建圆,长方形,凸多边形
点这看效果 下面说下凸多边形的创建.Box2D允许你创建任何种类的多边形形状,只要多边形是凸多边形,这将意味着它拥 有的所有内角要小于180度,所有的顶点要远离中心,而且你要按顺时针方向排列它们。 创建过程: 1. 首先,创建一个向量(Vector)来储存所有的顶点: var vertices:Vector.<b2Vec2> = new Vector.<b2Vec2>(); 2. 然后,我们将所有顶点作为b2Vec2对象并顺时针方向加到向量(Vector)里, 并设置b2Vec2对象的坐标(相对于刚体中心的坐标)。 vertices.push( new b2Vec2(-20/worldScale, -20/worldScale) ); vertices.push( new b2Vec2(20/worldScale, 20/worldScale) ?); vertices.push( new b2Vec2(-20/worldScale, 20/worldScale) ?); 3. 将向(vectors)变成多边形形状 polygonShape.SetAsVector(vertices,4); ? ? //4是顶点数 package { import Box2D.Collision.Shapes.b2CircleShape; 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; import flash.events.MouseEvent; import flash.text.TextField; import flash.text.TextFieldAutoSize; import flash.text.TextFormat; [SWF(width="640",height="480")] public class Polygon 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; //时间步,世界将在每一个时间步被更新 //只是定义了时间步还不够。在每一步,每一个物理实体(physic entity)根据作用 //于自身的作用力来更新(不包括睡眠状态)。处理这项任务的算法叫约束解算器 //(constraint solver)。它是基于循环每一个约束然后解算来进行的,一次一个。 private var velIterations:int = 10; //速率约束解算器 private var posIterations:int = 10; //位置约束解算器 public function Polygon() { drawBackground(); initWorld(); debugDraw(); createTip(); createFloor(); addEventListener(Event.ENTER_FRAME,onUpdate); stage.addEventListener(MouseEvent.CLICK,onStageClick); } private function createTip():void { var tip:TextField = new TextField(); var format:TextFormat = new TextFormat(); format.color = 0xFFFFFF; tip.autoSize = TextFieldAutoSize.LEFT; tip.defaultTextFormat = format; tip.text = "点击,随机创建圆,三角形,正方形,六边形"; addChild(tip); tip.x = (stageWidth - tip.width) / 2; } private function initWorld():void { var gravity:b2Vec2 = new b2Vec2(0,9.81); //重力 //世界中的刚体静止时,可以允许他们进入睡眠状态,睡眠的刚体无需模拟 var sleep:Boolean = true; world = new b2World(gravity,sleep); } private function onStageClick(event:MouseEvent):void { var random:Number = Math.random(); if(random <= 0.25 ) { createBall(mouseX,mouseY); }else if(random <= 0.5) { createRect(mouseX,mouseY); }else if(random <= 0.75) { crateTriangle(mouseX,mouseY); }else { cratePolygon(mouseX,mouseY); } } private function createBall(x:Number,y:Number):void { var bodyDef:b2BodyDef = new b2BodyDef(); //刚体定义 bodyDef.type = b2Body.b2_dynamicBody; //动态刚体 //设置刚体坐标,Box2D坐标用米来表示,注册点是中心点 bodyDef.position.Set(x/worldScale,y/worldScale); var circleShape:b2CircleShape = new b2CircleShape(25/worldScale); //半径为25像素的圆 var fixtureDef:b2FixtureDef = new b2FixtureDef(); //夹具(fixture)用于将形状绑定到刚体上 fixtureDef.density = 1; //密度 fixtureDef.restitution = 0.3; //弹性系数 fixtureDef.friction = 0.1; //摩擦系数 fixtureDef.shape = circleShape; var theBall:b2Body = world.CreateBody(bodyDef); //创建刚体 theBall.CreateFixture(fixtureDef); } private function createRect(x:Number,y/worldScale); var polygonShape:b2PolygonShape = new b2PolygonShape(); //创建多边形 polygonShape.SetAsBox(20/worldScale,20/worldScale); //轴对称的矩形,半宽长和半高长 var fixtureDef:b2FixtureDef = new b2FixtureDef(); //夹具(fixture)用于将形状绑定到刚体上 fixtureDef.density = 1; //密度 fixtureDef.restitution = 0.1; //弹性系数 fixtureDef.friction = 0.1; //摩擦系数 fixtureDef.shape = polygonShape; var theRect:b2Body = world.CreateBody(bodyDef); theRect.CreateFixture(fixtureDef); } private function crateTriangle(x:Number,y/worldScale); var polygonShape:b2PolygonShape = new b2PolygonShape(); var vertices:Vector.<b2Vec2> = new Vector.<b2Vec2>(); var theX:Number = 0; var theY:Number = -20/worldScale; var angle:Number = 2 * Math.PI / 3; //旋转角度是60度 //这是个公式,表示点(x1,y1)绕点(0,0)旋转angle角度后的坐标(x,y) //x = Math.cos(angle) * x1 - Math.sin(angle) * y1; //y = Math.cos(angle) * y1 + Math.sin(angle) * x1; for (var i:int = 0; i < 3; i++) {//顺时针方向创建6个顶点 var vec2:b2Vec2 = new b2Vec2(theX,theY); vertices.push( vec2 ); var tempX:Number = theX; var tempY:Number = theY; theX = Math.cos(angle) * tempX - Math.sin(angle) * tempY; theY = Math.cos(angle) * tempY + Math.sin(angle) * tempX; } polygonShape.SetAsVector(vertices,3); var fixtureDef:b2FixtureDef = new b2FixtureDef(); //夹具(fixture)用于将形状绑定到刚体上 fixtureDef.density = 1; //密度 fixtureDef.restitution = 0.1; //弹性系数 fixtureDef.friction = 0.1; //摩擦系数 fixtureDef.shape = polygonShape; var polygon:b2Body = world.CreateBody(bodyDef); //创建刚体 polygon.CreateFixture(fixtureDef); } private function cratePolygon(x:Number,y/worldScale); var polygonShape:b2PolygonShape = new b2PolygonShape(); var vertices:Vector.<b2Vec2> = new Vector.<b2Vec2>(); var theX:Number = -20/worldScale; var theY:Number = 0; var angle:Number = 2 * Math.PI / 6; //旋转角度是60度 //这是个公式,表示点(x1,y) //x = Math.cos(angle) * x1 - Math.sin(angle) * y1; //y = Math.cos(angle) * y1 + Math.sin(angle) * x1; for (var i:int = 0; i < 6; i++) {//顺时针方向创建6个顶点 var vec2:b2Vec2 = new b2Vec2(theX,6); var fixtureDef:b2FixtureDef = new b2FixtureDef(); //夹具(fixture)用于将形状绑定到刚体上 fixtureDef.density = 1; //密度 fixtureDef.restitution = 0.1; //弹性系数 fixtureDef.friction = 0.1; //摩擦系数 fixtureDef.shape = polygonShape; var polygon:b2Body = world.CreateBody(bodyDef); //创建刚体 polygon.CreateFixture(fixtureDef); } private function createFloor():void { var bodyDef:b2BodyDef = new b2BodyDef(); //刚体定义 bodyDef.type = b2Body.b2_staticBody; //静态刚体 //设置刚体坐标,注册点是中心点 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.1; //弹性系数 fixtureDef.friction = 0.1; //摩擦系数 fixtureDef.shape = polygonShape; var theFloor:b2Body = world.CreateBody(bodyDef); theFloor.CreateFixture(fixtureDef); } 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); } private function onUpdate(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(); } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |