Box2DFlash Hello World

Box2D everyinch 3818℃ 0评论

Box2DFlash物理引擎的基本程序包括:

b2World:b2World(gravity:b2Vec2, doSleep:Boolean)
b2BodyDef:通过物体定义来指定物体的类型(type)、初始位置(position)、角度(angle)、线速度(linearVelocity)、线速度阻尼 (linearDamping)、角速度(angularVelocity)和角速度阻尼(angularDamping)等
B2body:通过B2BodyDef在b2World中创建物体
b2PolygonShape:多边形。包括:
    SetAsBox(hx:Number, hy:Number):void
    SetAsOrientedBox(hx:Number,hy:Number,center:b2Vec2 = null, angle:Number = 0.0):void
    SetAsArray(vertices:Array, vertexCount:Number = 0):void
b2CircleShape:圆形
b2FixtureDef:将Shape绑定到Body,指定形状的定义(shape)、密度(density)、摩擦(friction)和弹力(restitution)

它们之间的关系如下:

定义世界:
 var gravity:b2Vec2 = new b2Vec2(0.0,9.8);
 var doSleep:Boolean = true;
 world = new b2World(gravity,doSleep);
静态物体:
 var bodyDef:b2BodyDef = new b2BodyDef();
 bodyDef.position.Set(10,10);
 var body:b2Body = world.CreateBody(bodyDef);
 var shape:b2PolygonShape = new b2PolygonShape();
 shape.SetAsBox(10,10);
 body.CreateFixture2(shape);
定义动态物体:
 var bodyDef:b2BodyDef = new b2BodyDef();
 bodyDef.type = b2Body.b2_dynamicBody;
 bodyDef.position.Set(10,10);
 bodyDef.angle = Math.random()*Math.PI;
 var shape:b2PolygonShape = new b2PolygonShape();
 shape.SetAsBox(10,10);
 var fixtureDef:b2FixtureDef = new b2FixtureDef();
 fixtureDef.shape = shape;
 fixtureDef.density = 1.0;
 fixtureDef.friction = 0.3;
 fixtureDef.restitution = 0.1;
 var body:b2Body = world.CreateBody(bodyDef);
 body.CreateFixture(fixtureDef);

分享&收藏

转载请注明:陈童的博客 » Box2DFlash Hello World

喜欢 (0)
发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
'; } if( dopt('d_footcode_b') ) echo dopt('d_footcode'); ?>