Box2DFlash笔记(一)

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

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

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

 玩了一段时间的Box2D这个C++的Physicas Engine,突然发现有人把它写成了Flash版的,官网为:http://box2dflash.sourceforge.net/,大家可以去玩玩,很不错的。Download下来后,自己也照着做了个,其实很简单的,没C++哪么复杂。由于没有flash版的Docs,所以大家去找C++版的Manual看吧,下载C++版源码,解压就有Documents,C++的官网是打不开的,不知道为什么国内把它屏蔽了,好友日本就能打开这个网站的。废话不说,贴上我的代码:

// // PhysEngine.as // // the entry of application // // Written by Leezhm, 8st April, 2009 // Contact : [email protected] // Last Modified by Leezhm on 8st April, 2009 // package Srcs { import Box2D.Collision.Shapes.b2CircleDef; import Box2D.Collision.Shapes.b2PolygonDef; import Box2D.Collision.Shapes.b2ShapeDef; import Box2D.Common.Math.b2Vec2; import Box2D.Dynamics.b2Body; import Box2D.Dynamics.b2BodyDef; import Box2D.Collision.b2AABB; import Box2D.Dynamics.b2World; import flash.display.Sprite; import flash.events.Event; public class PhysEngine extends Sprite { private var _b2World:b2World; private var _b2WorldAABB:b2AABB; private var _gravityVector:b2Vec2; private var _b2TimerStep:Number = 1.0 / 30.0; private var _b2Interations:uint = 10; public function PhysEngine() { if (!stage) { InitApplication(); } else { this.addEventListener(Event.ADDED_TO_STAGE, InitApplication); } } private function InitApplication(evt:Event = null):void { this.removeEventListener(Event.ADDED_TO_STAGE, InitApplication); this.addEventListener(Event.ENTER_FRAME, OnMainEnterFrame, false, 0, true); InitPhysicsEngine(); AddBodiesToWorld(); } private function InitPhysicsEngine():void { // create the world AABB if (null == this._b2WorldAABB) { this._b2WorldAABB = new b2AABB(); this._b2WorldAABB.lowerBound.Set( -100.0, -100.0); this._b2WorldAABB.upperBound.Set( 100.0, 100.0); } // define the gravity vector if (null == this._gravityVector) { this._gravityVector = new b2Vec2(0.0, 10.0); } // allow body can be sleep var _b2Sleep:Boolean = true; // create the b2World if (null == this._b2World) { this._b2World = new b2World(this._b2WorldAABB, this._gravityVector, _b2Sleep); } } private function AddBodiesToWorld():void { var _body:b2Body; var _bodyDef:b2BodyDef; var _boxDef:b2PolygonDef; var _circleDef:b2CircleDef; // create the ground body _bodyDef = new b2BodyDef(); _bodyDef.position.Set(10, 20); _boxDef = new b2PolygonDef(); _boxDef.SetAsBox(30, 3); _boxDef.friction = 0.3; _boxDef.density = 0; _bodyDef.userData = new CPhysRectangle(); _bodyDef.userData.width = 1800; _bodyDef.userData.height = 180; this.addChild(_bodyDef.userData); _body = this._b2World.CreateBody(_bodyDef); _body.CreateShape(_boxDef); _body.SetMassFromShapes(); // create dynamic body for (var i:uint = 0; i < 36; i ++) { var _xScale:Number = Math.random() + 0.5; var _yScale:Number = Math.random() + 0.5; _bodyDef = new b2BodyDef(); _bodyDef.position.x = Math.random() * 15 + 5; _bodyDef.position.y = Math.random() * 10; switch(uint(Math.random() * 3)) { case 0: { // create a circle _circleDef = new b2CircleDef(); _circleDef.radius = _xScale; _circleDef.friction = 0.5; _circleDef.density = 1.0; _circleDef.restitution = 0.2; _bodyDef.userData = new CCircle(); _bodyDef.userData.width = _xScale * 2 * 30; _bodyDef.userData.height = _xScale * 2 * 30; _body = _b2World.CreateBody(_bodyDef); _body.CreateShape(_circleDef); break; } case 1: { // create a square _boxDef = new b2PolygonDef(); _boxDef.SetAsBox(_xScale, _xScale); _boxDef.friction = 0.5; _boxDef.density = 1.0; _boxDef.restitution = 0.2; _bodyDef.userData = new CSquare(); _bodyDef.userData.width = _xScale * 2 * 30; _bodyDef.userData.height = _xScale * 2 * 30; _body = _b2World.CreateBody(_bodyDef); _body.CreateShape(_boxDef); break; } case 2: { // create a rectangle _boxDef = new b2PolygonDef(); _boxDef.SetAsBox(_xScale, _yScale); _boxDef.friction = 0.5; _boxDef.density = 1.0; _boxDef.restitution = 0.2; _bodyDef.userData = new CRectangle(); _bodyDef.userData.width = _xScale * 2 * 30; _bodyDef.userData.height = _yScale * 2 * 30; _body = _b2World.CreateBody(_bodyDef); _body.CreateShape(_boxDef); break; } default: { trace("No Boby can be created!!!"); } } _body.SetMassFromShapes(); this.addChild(_bodyDef.userData); } } private function OnMainEnterFrame(evt:Event = null):void { this._b2World.Step(this._b2TimerStep, this._b2Interations); for (var bb:b2Body = this._b2World.m_bodyList; bb; bb = bb.GetNext()) { if (bb.m_userData is Sprite) { bb.m_userData.x = bb.GetPosition().x * 30; bb.m_userData.y = bb.GetPosition().y * 30; bb.m_userData.rotation = bb.GetAngle() * (180/Math.PI); } } } } }

 

不好意思啊,fla文件是没法上传了。运行截图如下:

Box2DFlash笔记(一)_第1张图片

 

BTW:不是很喜欢Box2DFlash的源码编码风格,特别是变量都放在最后定义。变量的名字很像C++的风格,嘿嘿,还没正式开始研究源码,不是很多的。

你可能感兴趣的:(function,Blog,Flash,null,import,download)