admin 管理员组

文章数量: 893695

box2d的部分心得

box2d的部分心得

 

在box2d世界里!我们并不需要让所有的对象都是box2d里的对象!我们需要造就box2d环境与flash player共存:

怎么理解:

 

box2d最大的优点的是碰撞以及碰撞带来的效果!!所以box2d主要是用来处理碰撞和其带来的相关效果!

 

当我们在box2d里某个对象不需要碰撞时,我们可以将其变为flash player里的对象:

 

下面的例子里的hitTestPoint就是:

 

demo:

.swf

 

 

 

package {import flash.display.Sprite;import flash.events.Event;import flash.events.MouseEvent;import Box2D.Dynamics.*;import Box2D.Collision.*;import Box2D.Collision.Shapes.*;import Box2D.Common.Math.*;public class stabilize extends Sprite {public var m_sprite: Sprite = new Sprite();public var m_world:b2World;// the_crate is the crate spritepublic var the_crate:crate=new crate();// droppable_area is the sprite representing the area your mouse// must overlap to drop a cratepublic var droppable_area:drop_area = new drop_area();public function stabilize() {var gravity:b2Vec2=new b2Vec2(0,9.8);var worldAABB:b2AABB = new b2AABB();worldAABB.lowerBound.Set(-1000,-1000);worldAABB.upperBound.Set(1000,1000);m_world=new b2World(worldAABB,gravity,true);m_sprite = new Sprite();addChild(m_sprite);addChild(droppable_area);addChild(the_crate);debug_draw();AddBox(250/30,350/30,10/30,10/30,0,false);AddBox(250/30,350/30,200/30,10/30,20,false);addEventListener(Event.ENTER_FRAME,Update);stage.addEventListener(MouseEvent.MOUSE_DOWN,mousePressed);stage.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoved);}public function mousePressed(e:MouseEvent) {// look at hitTestPoint... the final true value says it's going to to check against the actual pixels// instead of the bounding boxif (droppable_area.hitTestPoint(mouseX,mouseY,true)) {AddBox(mouseX/30,mouseY/30,0.5,0.5,3,true);}}public function mouseMoved(e:MouseEvent) {the_crate.x=mouseX;the_crate.y=mouseY;if (droppable_area.hitTestPoint(mouseX,mouseY,true)) {the_crate.alpha=1;} else {the_crate.alpha=0.5;}}// function AddBox// px: x position// py: y position// _halfwidth: half of the box width// _halfheight: half of the box height// density: density of the box (0: static)// is_crate: if true, it's a crate (and you should render the proper movieclippublic function AddBox(px:Number,py:Number,_halfwidth:Number,_halfheight:Number,density:Number,is_crate:Boolean) {var bodyDef:b2BodyDef = new b2BodyDef();bodyDef.position.Set(px,py);var boxDef:b2PolygonDef = new b2PolygonDef();boxDef.SetAsBox(_halfwidth,_halfheight);boxDef.density=density;boxDef.friction=0.3;boxDef.restitution=0.2;if (is_crate) {bodyDef.userData = new crate();}var body:b2Body=m_world.CreateBody(bodyDef);body.CreateShape(boxDef);body.SetMassFromShapes();if (is_crate) {addChild(bodyDef.userData);}}public function Update(e:Event) {m_world.Step(1/30,10);for (var bb:b2Body = m_world.m_bodyList; bb; bb = bb.m_next) {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);}}}public function debug_draw() {var dbgDraw:b2DebugDraw = new b2DebugDraw();var dbgSprite:Sprite = new Sprite();m_sprite.addChild(dbgSprite);dbgDraw.m_sprite=m_sprite;dbgDraw.m_drawScale=30;dbgDraw.m_alpha=1;dbgDraw.m_fillAlpha=0.5;dbgDraw.m_lineThickness=1;dbgDraw.m_drawFlags=b2DebugDraw.e_shapeBit;m_world.SetDebugDraw(dbgDraw);}}
}

本文标签: box2d的部分心得