Box2DFlash笔记(二)--b2DebugDraw类的使用

声明:欢迎任何人和组织转载本blog中文章,但必须标记文章原始链接和作者信息。

本文链接:http://blog.csdn.net/li_007/archive/2009/04/09/4060162.aspx

开拓进取的小乌龟------->CSDN点滴点点滴滴Blog

在上一篇所有的body都是用flash做的,想ground,circle etc。其实在Box2DFlash中提供了一个Debug用的类,b2DebugDraw来Draw这些body,这篇blog中说明怎么使用b2DebugDraw这个类,代码如下:

// // DbgDraw.as // // a simple application test Box2DFlash used b2DebugDraw class. // // Written by Leezhm, 9st April, 2009 // Contact : Leezhm@126.com // Last Modified by Leezhm on 9st April, 2009 // package Srcs { 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; public class DbgDraw extends Sprite { private var _drawSprite:Sprite; private var _world:b2World; private var _dbgDraw:b2DebugDraw; private var _body:b2Body; private var _bodyDef:b2BodyDef; private var _boxShape:b2PolygonDef; private var _timeStep:Number = 1.0 / 60.0; private var _iterations:uint = 10; public function DbgDraw() { if (!this.hasEventListener(Event.ENTER_FRAME)) { this.addEventListener(Event.ENTER_FRAME, OnMainEnterFrameToUpdate); } InitApplication(); } private function InitApplication():void { createWorld(); setDebugDraw(); createGroundBody(); createDynamicBodies(); } private function createWorld():void { // define the b2AABB coordinate object var _bAABB:b2AABB = new b2AABB(); _bAABB.lowerBound.Set( -1000.0, -1000.0); _bAABB.upperBound.Set( 1000.0, 1000.0); // define the gravity vector var _gravity:b2Vec2 = new b2Vec2(0.0, 10.0); // Allow bodis to sleep var _bSleep:Boolean = true; // create the world if (null == this._world) { this._world = new b2World(_bAABB, _gravity, _bSleep); } } private function setDebugDraw():void { if (null == this._dbgDraw) { this._dbgDraw = new b2DebugDraw(); } if (null == this._drawSprite) { this._drawSprite = new Sprite(); } this._dbgDraw.m_sprite = this._drawSprite; this._dbgDraw.m_drawScale = 30.0; this._dbgDraw.m_drawFlags = b2DebugDraw.e_jointBit | b2DebugDraw.e_shapeBit; this._dbgDraw.m_fillAlpha = 0.3; this._dbgDraw.m_lineThickness = 2.0; this._world.SetDebugDraw(this._dbgDraw); this.addChild(this._drawSprite); } private function createGroundBody():void { this._bodyDef = new b2BodyDef(); this._bodyDef.position.Set(10, 20); this._boxShape = new b2PolygonDef(); this._boxShape.density = 0; this._boxShape.friction = 0.6; this._boxShape.restitution = 0.2; this._boxShape.SetAsBox(30, 0.5); this._body = this._world.CreateBody(this._bodyDef); this._body.CreateShape(this._boxShape); this._body.SetMassFromShapes(); } private function createDynamicBodies():void { for (var i:uint = 0; i < 66; i++) { this._bodyDef = new b2BodyDef(); this._bodyDef.position.Set(Math.random() * 30, Math.random() * 5); this._body = this._world.CreateBody(this._bodyDef); if (Math.random() <= 0.5) { this._boxShape = new b2PolygonDef(); this._boxShape.density = 2.1; this._boxShape.friction = 0.3; this._boxShape.SetAsBox(Math.random() + 0.4, Math.random() + 0.4); this._body.CreateShape(this._boxShape); } else { var _circle:b2CircleDef = new b2CircleDef(); _circle.radius = Math.random() + 0.4; _circle.density = 1.8; _circle.friction = 0.2; this._body.CreateShape(_circle); } this._body.SetMassFromShapes(); } } private function OnMainEnterFrameToUpdate(evt:Event = null):void { this._world.Step(this._timeStep, this._iterations); for (var bd:b2Body = this._world.m_bodyList; bd; bd = bd.GetNext()) { if (bd is Sprite) { bd.m_userData.x = bd.GetPosition().x * 30; bd.m_userData.y = bd.GetPosition().y * 30; bd.m_userData.rotation = bd.GetAngle() * (180/Math.PI); } } } } }

PS:用flash professional建立一个Actionscript 3的工程,用上面的这个类来做文档类,编译运行就可以看到结果,运行结果如下:

Draw Body used b2DebugDraw class

 

BTW:不知道么样才能往csdn的blog上传swf文件啊,这样就更有说服力了。

转载于:https://www.cnblogs.com/leezhm/archive/2009/04/09/2560319.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值