WEB开发网
开发学院图形图像Flash Managing multiple gravities with Box2D 阅读

Managing multiple gravities with Box2D

 2009-10-20 00:00:00 来源:WEB开发网   
核心提示:One of the apparently hardest things to do with Box2D is assigning different gravity forces to different objects.For instance, you may want the world to be rule

One of the apparently hardest things to do with Box2D is assigning different gravity forces to different objects.

For instance, you may want the world to be ruled with regular gravity, while you don’t want the bullet fired by your character to be affected, or you may want regular gravity for all boxes and inverted one for all circles, like in the example I am talking about.

There is one way to simulate different gravity and one way to apply different gravity in your Box2D projects.

This is the main script, directly from the HelloWorld.as example provided with Box2D distribution (so just copy/paste the code)

In some case you should need to refresh the page to see the movies in action.

package {
 import flash.display.Sprite;
 import flash.events.Event;
 import Box2D.Dynamics.*;
 import Box2D.Collision.*;
 import Box2D.Collision.Shapes.*;
 import Box2D.Common.Math.*;
 public class HelloWorld extends Sprite {
  public var m_world:b2World;
  public var m_iterations:int=10;
  public var m_timeStep:Number=1.0/30.0;
  public function HelloWorld() {
   addEventListener(Event.ENTER_FRAME, Update, false, 0, true);
   var worldAABB:b2AABB = new b2AABB();
   worldAABB.lowerBound.Set(-100.0, -100.0);
   worldAABB.upperBound.Set(100.0, 100.0);
   var gravity:b2Vec2=new b2Vec2(0.0,10.0);
   var doSleep:Boolean=true;
   m_world=new b2World(worldAABB,gravity,doSleep);
   var dbgDraw:b2DebugDraw = new b2DebugDraw();
   var dbgSprite:Sprite = new Sprite();
   addChild(dbgSprite);
   dbgDraw.m_sprite=dbgSprite;
   dbgDraw.m_drawScale=30.0;
   dbgDraw.m_fillAlpha=0.5;
   dbgDraw.m_lineThickness=1.0;
   dbgDraw.m_drawFlags=b2DebugDraw.e_shapeBit;
   m_world.SetDebugDraw(dbgDraw);
   var body:b2Body;
   var bodyDef:b2BodyDef;
   var boxDef:b2PolygonDef;
   var circleDef:b2CircleDef;
   bodyDef = new b2BodyDef();
   bodyDef.position.Set(10, 12);
   boxDef = new b2PolygonDef();
   boxDef.SetAsBox(30, 0.5);
   boxDef.friction=0.3;
   boxDef.density=0;
   body=m_world.CreateBody(bodyDef);
   body.CreateShape(boxDef);
   body.SetMassFromShapes();
   bodyDef = new b2BodyDef();
   bodyDef.position.Set(10, 0);
   boxDef = new b2PolygonDef();
   boxDef.SetAsBox(30, 0.5);
   boxDef.friction=0.3;
   boxDef.density=0;
   body=m_world.CreateBody(bodyDef);
   body.CreateShape(boxDef);
   body.SetMassFromShapes();
   for (var i:int = 1; i < 10; i++) {
    bodyDef = new b2BodyDef();
    bodyDef.position.x=Math.random()*12+2;
    bodyDef.position.y=Math.random()+5;
    var rX:Number=Math.random()+0.2;
    var rY:Number=Math.random()+0.2;
    if (Math.random()<0.5) {
     boxDef = new b2PolygonDef();
     boxDef.SetAsBox(rX, rY);
     boxDef.density=1.0;
     boxDef.friction=0.5;
     boxDef.restitution=0.2;
     bodyDef.userData = new Sprite();
     bodyDef.userData.name="box";
     body=m_world.CreateBody(bodyDef);
     body.CreateShape(boxDef);
    } else {
     circleDef = new b2CircleDef();
     circleDef.radius=rX;
     circleDef.density=1.0;
     circleDef.friction=0.5;
     circleDef.restitution=0.2;
     bodyDef.userData = new Sprite();
     bodyDef.userData.name="circle";
     body=m_world.CreateBody(bodyDef);
     body.CreateShape(circleDef);
    }
    body.SetMassFromShapes();
    addChild(bodyDef.userData);
   }
  }
  public function Update(e:Event):void {
   m_world.Step(m_timeStep, m_iterations);
  }
 }
}

1 2 3  下一页

Tags:Managing multiple gravities

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接