注:点击鼠标添加Body,R键清空Body

  原来博客园要注册才能看到swf的,附有截图

package {import Box2D.Collision.b2AABB;import Box2D.Collision.Shapes.b2CircleDef;import Box2D.Collision.Shapes.b2PolygonDef;import Box2D.Common.Math.b2Math;import Box2D.Common.Math.b2Vec2;import Box2D.Common.Math.b2XForm;import Box2D.Dynamics.b2Body;import Box2D.Dynamics.b2BodyDef;import Box2D.Dynamics.b2DebugDraw;import Box2D.Dynamics.b2World;import flash.display.Bitmap;import flash.display.BitmapData;import flash.display.Sprite;import flash.events.Event;import flash.events.KeyboardEvent;import flash.events.MouseEvent;import flash.geom.Point;import flash.ui.Keyboard;import ipk.bmpd.CutCtrl;/*** ...* @description HelloWorldの多边形* @author ispooky* @date 2010.11.09* * @see http://ispooky.cnblogs.com*/public class PolygonsApp extends Sprite{public static const ITERATIONS :int = 30;public static const TIME_STEP :Number = 1 / 30;public static const RATIO :int = 30;

private static var world :b2World;private static var spriteToDrawOn :Sprite;

public function PolygonsApp() {if (stage) init();else addEventListener(Event.ADDED_TO_STAGE, init);}

private function init(e:Event = null):void {removeEventListener(Event.ADDED_TO_STAGE, init);

initB2D();stage.addEventListener(MouseEvent.CLICK, onClickHandler);stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDownHandler);}

private function onKeyDownHandler(e:KeyboardEvent):void {if (e.keyCode == 82/*R*/){/////// 销毁Body/////for (var bb:b2Body = world.m_bodyList; bb; bb = bb.m_next){if (bb.m_userData is Sprite){removeChild(bb.m_userData);world.DestroyBody(bb);}}}}

private function onClickHandler(e:MouseEvent):void {if (world == null) return;createPolygon();}

private function initB2D():void{addEventListener(Event.ENTER_FRAME, onLoopHandler, false, 0, true);createWorld();//showDebug();createGrounds();createPolygon();createPolygon();createPolygon();}

/*** 添加Body* @param _type*/private function createPolygon(_type:int = -1):void{var bodyDef:b2BodyDef = new b2BodyDef();var boxDef:b2PolygonDef = new b2PolygonDef();var circleDef:b2CircleDef = new b2CircleDef();var body:b2Body;var type:intif (_type == -1){var tmp_arr:Array = [0, 3, 4, 6, 8];type = tmp_arr[int(Math.random() * 5)];}else{type = _type;}

var skin:Sprite = new Sprite();var tmp_asset:Bitmap;var radius:int;var i:int;var tmp_radius:int = Math.random() * 40 + 30;var start_pos:Point = new Point(Math.random() * 100 - 50 + 275, Math.random() * 100 - 50 -100);

switch(type){case 0:bodyDef.position.Set(start_pos.x / RATIO, start_pos.y / RATIO);radius = int(Math.random() * 25) + 10;circleDef.radius = radius / RATIO;circleDef.density = 1;circleDef.friction = 0.2;circleDef.restitution = 0.3;tmp_asset = getAssets(0, radius);tmp_asset.x = -tmp_asset.width * 0.5;tmp_asset.y = -tmp_asset.height * 0.5;skin.addChild(tmp_asset);bodyDef.userData = skin;addChild(bodyDef.userData);body = world.CreateBody(bodyDef);body.CreateShape(circleDef);body.SetMassFromShapes();break;case 3:createPolygonTypeOf(start_pos.x, start_pos.y, 3, tmp_radius);break;case 4:createPolygonTypeOf(start_pos.x, start_pos.y, 4, tmp_radius);break;case 5:createPolygonTypeOf(start_pos.x, start_pos.y, 5, tmp_radius);break;case 6:createPolygonTypeOf(start_pos.x, start_pos.y, 6, tmp_radius);break;case 7:createPolygonTypeOf(start_pos.x, start_pos.y, 7, tmp_radius);break;case 8:createPolygonTypeOf(start_pos.x, start_pos.y, 8, tmp_radius);break;}}

/*** 创建正多边形Body* @param xpos* @param ypos* @param vertexCount* @param radius*/private function createPolygonTypeOf(xpos:Number, ypos:Number, vertexCount:int, radius:Number):void{var boxDef:b2PolygonDef = new b2PolygonDef();boxDef.vertexCount = vertexCount;var angle:Number = Math.PI * 2 / vertexCount;var radius:Number = radius / RATIO;for (var i:int = 0; i < vertexCount; i++){var dx:Number = radius * Math.cos(angle * i);var dy:Number = radius * Math.sin(angle * i);boxDef.vertices[i] = b2Math.b2MulX(new b2XForm(),new b2Vec2(dx, dy));}boxDef.density = 2;boxDef.friction = 0.3;boxDef.restitution = 0.3;

var bodyDef:b2BodyDef = new b2BodyDef();bodyDef.angle = Math.random() * 360;bodyDef.position.Set(xpos / RATIO, ypos / RATIO);

var skin:Sprite = new Sprite();var tmp_asset:Bitmap;switch(vertexCount){case 3:tmp_asset = getAssets(vertexCount, radius * RATIO);tmp_asset.x = -(tmp_asset.width - radius * RATIO);tmp_asset.y = -tmp_asset.height * 0.5;skin.addChild(tmp_asset);break;case 4:tmp_asset = getAssets(vertexCount, radius * RATIO);tmp_asset.x = -tmp_asset.width*0.5;tmp_asset.y = -tmp_asset.height * 0.5;skin.addChild(tmp_asset);break;case 6:tmp_asset = getAssets(vertexCount, radius * RATIO);tmp_asset.x = -tmp_asset.width*0.5;tmp_asset.y = -tmp_asset.height * 0.5;skin.addChild(tmp_asset);break;case 8:tmp_asset = getAssets(vertexCount, radius * RATIO);tmp_asset.x = -tmp_asset.width*0.5;tmp_asset.y = -tmp_asset.height * 0.5;skin.addChild(tmp_asset);break;}

bodyDef.userData = skin;addChild(bodyDef.userData);

var body:b2Body = world.CreateBody(bodyDef);body.CreateShape(boxDef);body.SetMassFromShapes();}

/*** 创建地板*/private function createGrounds():void{var bodyDef:b2BodyDef = new b2BodyDef();var boxDef:b2PolygonDef = new b2PolygonDef();var body:b2Body;

bodyDef.position.Set(275 / RATIO, 360 / RATIO);boxDef.SetAsBox(900 / 2 / RATIO, 30 / 2 / RATIO);boxDef.density = 0;boxDef.friction = 0.3;boxDef.restitution = 0.2;

body = world.CreateBody(bodyDef);body.CreateShape(boxDef);

boxDef.SetAsOrientedBox(30 / 2 / RATIO, 90 / 2 / RATIO, new b2Vec2( -200 / RATIO, -60 / RATIO), 0);body = world.CreateBody(bodyDef);body.CreateShape(boxDef);

boxDef.SetAsOrientedBox(30 / 2 / RATIO, 90 / 2 / RATIO, new b2Vec2( 200 / RATIO, -60 / RATIO), 0);body = world.CreateBody(bodyDef);body.CreateShape(boxDef);

boxDef.SetAsOrientedBox(30 / 2 / RATIO, 45 / 2 / RATIO, new b2Vec2( -435 / RATIO, -37.5 / RATIO), 0);body = world.CreateBody(bodyDef);body.CreateShape(boxDef);

boxDef.SetAsOrientedBox(30 / 2 / RATIO, 45 / 2 / RATIO, new b2Vec2( 435 / RATIO, -37.5 / RATIO), 0);body = world.CreateBody(bodyDef);body.CreateShape(boxDef);}

/*** 显示Debug图*/private function showDebug():void{spriteToDrawOn = new Sprite();addChild(spriteToDrawOn);var dbgDraw:b2DebugDraw = new b2DebugDraw();dbgDraw.m_sprite = spriteToDrawOn;dbgDraw.m_drawScale = RATIO;dbgDraw.m_fillAlpha = 0.6;dbgDraw.m_lineThickness = 1.0;dbgDraw.m_drawFlags = b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit;

world.SetDebugDraw(dbgDraw);}

/*** 创建物理空间*/private function createWorld():void{var worldAABB:b2AABB = new b2AABB();worldAABB.lowerBound.Set( -3000 / RATIO, -3000 / RATIO);worldAABB.upperBound.Set(3000 / RATIO, 3000 / RATIO);

var gravity:b2Vec2 = new b2Vec2(0, 10);var doSleep:Boolean = true;

world = new b2World(worldAABB, gravity, doSleep);}

/*** 刷新物理空间* @param e*/private function onLoopHandler(e:Event):void {if (world == null) return;world.Step(TIME_STEP, ITERATIONS);for (var bb:b2Body = world.m_bodyList; bb; bb = bb.m_next){if (bb.m_userData is Sprite){bb.m_userData.x = bb.GetPosition().x * RATIO;bb.m_userData.y = bb.GetPosition().y * RATIO;bb.m_userData.rotation = bb.GetAngle() * (180 / Math.PI);}}}

/*** 获取贴图* @param type* @param radius* @return*/private function getAssets(type:int = 0, radius:int = 20):Bitmap{

var target_mc:Sprite = new Sprite();target_mc.visible = false;addChild(target_mc);var tmp_bmpd:BitmapData = new Alsace_Pic();var tmp_bmp:Bitmap = new Bitmap(tmp_bmpd);target_mc.addChild(tmp_bmp);var tmp_mc:Sprite = new Sprite();tmp_mc.visible = false;tmp_mc.x = target_mc.x;tmp_mc.y = target_mc.y;addChild(tmp_mc);var new_bmp:Bitmap;

var i:int;var pos:Point = new Point(Math.random() * 600 - 300 + 720, Math.random() * 200 - 100 + 450);tmp_mc.graphics.clear();tmp_mc.graphics.beginFill(0x900000);switch(type){case 0:tmp_mc.graphics.drawCircle(pos.x, pos.y, radius);tmp_mc.graphics.endFill();

new_bmp = new Bitmap(CutCtrl.cutOutSuper(target_mc, tmp_mc).clone());

break;

case 3:tmp_mc.graphics.moveTo(pos.x + radius, pos.y);for (i = 1; i < 3; i++){tmp_mc.graphics.lineTo(pos.x + radius * Math.cos(Math.PI * 2 / 3 * i), pos.y + radius * Math.sin(Math.PI * 2 / 3 * i));}

new_bmp = new Bitmap(CutCtrl.cutOutSuper(target_mc, tmp_mc).clone());break;case 4:tmp_mc.graphics.moveTo(pos.x + radius, pos.y);for (i = 1; i < 4; i++){tmp_mc.graphics.lineTo(pos.x + radius * Math.cos(Math.PI * 2 / 4 * i), pos.y + radius * Math.sin(Math.PI * 2 / 4 * i));}

new_bmp = new Bitmap(CutCtrl.cutOutSuper(target_mc, tmp_mc).clone());break;case 5:tmp_mc.graphics.moveTo(pos.x + radius, pos.y);for (i = 1; i < 5; i++){tmp_mc.graphics.lineTo(pos.x + radius * Math.cos(Math.PI * 2 / 5 * i), pos.y + radius * Math.sin(Math.PI * 2 / 5 * i));}

new_bmp = new Bitmap(CutCtrl.cutOutSuper(target_mc, tmp_mc).clone());break;case 6:tmp_mc.graphics.moveTo(pos.x + radius, pos.y);for (i = 1; i < 6; i++){tmp_mc.graphics.lineTo(pos.x + radius * Math.cos(Math.PI * 2 / 6 * i), pos.y + radius * Math.sin(Math.PI * 2 / 6 * i));}

new_bmp = new Bitmap(CutCtrl.cutOutSuper(target_mc, tmp_mc).clone());break;case 7:tmp_mc.graphics.moveTo(pos.x + radius, pos.y);for (i = 1; i < 7; i++){tmp_mc.graphics.lineTo(pos.x + radius * Math.cos(Math.PI * 2 / 7 * i), pos.y + radius * Math.sin(Math.PI * 2 / 7 * i));}

new_bmp = new Bitmap(CutCtrl.cutOutSuper(target_mc, tmp_mc).clone());break;case 8:tmp_mc.graphics.moveTo(pos.x + radius, pos.y);for (i = 1; i < 8; i++){tmp_mc.graphics.lineTo(pos.x + radius * Math.cos(Math.PI * 2 / 8 * i), pos.y + radius * Math.sin(Math.PI * 2 / 8 * i));}

new_bmp = new Bitmap(CutCtrl.cutOutSuper(target_mc, tmp_mc).clone());break;}tmp_bmpd.dispose();removeChild(tmp_mc);tmp_mc = null;removeChild(target_mc);target_mc = null;return new_bmp;}

}

}

转载于:https://www.cnblogs.com/pengyingh/articles/2395647.html

Box2dの自定义多边形相关推荐

  1. Python使用matplotlib可视化气泡图、并使用encircle函数自定义多边形圈定可视化图像中的指定区域(Bubble plot with Encircling)

    Python使用matplotlib可视化气泡图.并使用encircle函数自定义多边形圈定可视化图像中的指定区域(Bubble plot with Encircling) 目录

  2. R语言ggplot2可视化散点图、并使用geom_encircle函数自定义多边形圈定可视化图像中的指定区域、使用geom_smooth函数基于loess方法拟合数据点之间的趋势关系曲线

    R语言ggplot2可视化散点图.并使用geom_encircle函数自定义多边形圈定可视化图像中的指定区域(Scatterplot With Encircling).使用geom_smooth函数基 ...

  3. boost::geometry模块自定义多边形示例

    boost::geometry模块自定义多边形示例 实现功能 C++实现代码 实现功能 boost::geometry模块自定义多边形示例 C++实现代码 #include <boost/geo ...

  4. Vue关于天地图的使用 自定义标注、自定义折线工具、自定义多边形工具

    一.天地图的引用 其实天地图的使用与其他地图(百度等),个人觉得大同小异,个人的方法是在项目里面的public目录下的index.html文件下导入 然后在你想要使用的页面上把地图显示出来就好了 注意 ...

  5. Android 八角图 蛛网图 自定义多边形

    自定义view 初始化画笔paint mainPaint = new Paint(); mainPaint.setAntiAlias(true); mainPaint.setColor(Color.r ...

  6. android 自定义多边形,Android:自定义view之Canvas绘制图形

    前面讲解了onMeasure,接下来讲解onDraw,onDraw主要就是绘制,也就是我们真正关心的部分,使用的是Canvas绘图. @Override protected void onDraw(C ...

  7. 关于box2d相关学习教程记录一下

    Box2D 2.0.1版本 认识Box2D世界 掉落的苹果--b2Body刚体 创建圆形刚体 创建静止不动的刚体 在运行时创建刚体 刚体的上衣--b2BodyDef.userData Box2D能再简 ...

  8. (老文章)Box2D新手入门顺阶教程

    注意: 本教程只适用于BOX2D 2.0以下版本,由于2.0版API的改动,所以有部分代码会并不适用.但是主要思想还是一样的. 一. HelloWorld 开始之前,我假想你已经看过了HelloWor ...

  9. android高德地图绘制多边形_exlive1.0BS网上查车完善电子围栏:行政区域、多边形、规划线路...

    exlive1.0位置服务平台--BS网上查车完善行政区域.多边形.规划线路三种电子围栏.已创建围栏支持在围栏列表选择,地图展示,直观查看. ①行政区域围栏:支持行政区域围栏创建,可设置驶入.驶出报警 ...

最新文章

  1. 线上办公室 x 音视频会议最佳实践
  2. DeepI2P:基于深度分类的图像对点云配准
  3. 开发日记-20190609 关键词 记录一次失败,感悟,畅想未来
  4. mac 安装配置java环境变量
  5. 论文阅读计划2(Deep Joint Rain Detection and Removal from a Single Image)
  6. vb获取textbox数字_Spectrum仪器PCIe数字化仪可额外扩展8个数字输入
  7. qml入门学习(六):Component组件
  8. AndroidGUI24:TabHost常用技巧
  9. 关系型数据库(八),数据库其他面试题
  10. 正大国际琪貨纯手:期货投资中该如何看懂趋势线?
  11. (sn0wbreeze保基带升级,redsnow,absinthe越狱)常用越狱工具下载:redsnow,absinthe,tinyumbrella,sn0wbreeze
  12. python PIL生成gif帧率问题
  13. matlab 判断矩阵是否正定
  14. 大陆、港澳台身份证、护照、军官证的正则表达式
  15. ettercap 中间人攻击
  16. 一起谈.NET技术,.NET十年(下)
  17. 滴滴收购优步谈判过程_如何为未来安排优步
  18. 【对标TensorFlow】阿里公开内部超大规模分布式机器学习平台,对此你怎么看?
  19. gPTP时钟同步(时间同步)协议简介
  20. ABB软件的robotstudio怎么解决仿真问题(1)

热门文章

  1. 10年Python大牛倾力打造系统Python学习流程图!
  2. 如何修改访问vnc服务器的密码,修改VNC访问的密码
  3. ios 打印 详细错误日志_关于Xcode不能打印崩溃日志
  4. plsql执行command命令控制台出现乱码_设计模式系列 — 命令模式
  5. hooks 使用dva_Taro3 中使用dva
  6. tcpdump 命令的个常用选项:三
  7. Splay ---- 区间翻转 区间最大值 区间加 P4146 序列终结者
  8. 烽火服务器装系统,烽火 FitServer R2200 V5 机架式服务器
  9. 渡神纪帧数测试软件,渡神纪芬尼斯崛起配置要求高吗 渡神纪配置要求详细介绍_游侠网...
  10. ICPC 2005 hangzhou Generator (UVA1358)KMP + 期望DP / 高斯消元