最近一直没写东西,今天分享学习Box2D的学习吧。看的是官方上的用户手册
首先几个基本概念
world ,就是整个物理世界了,是下面所有东西的集合。
rigid body, they are hard like diamonds.也简称body,也就是实际的一个物体。
shape , shape have material properties of friction and restitution.很多物理上的性质都在shape中给予定义。
constraint ,物体之间的限制,joint is a kind of constraint.
joint 很重要,有很多种joint.
然后是一些小细节
Box2D中没有像素的概念,由C++那边移植过来的,默认的长度单位是meter,1meter = 30 pixel;
world要尽可能的大,大的比小的好,如果body到达了world的边界,就会被“冻结”并停止模拟。
Box2D是模拟真实的物理世界,如果质量设置为0,body就为static,碰撞就不会动。
Box2D不会保存shape或者body的定义,它把数据复制给b2Body结构。(就像bitmap和bitmapData)
Integrator simulate the physics equations at discrete points of time.越少的迭代,性能越好,准确性越差。
Body的建立主要是采用工厂模式,Factories do not retain references to the definition.
只介绍一些感觉比较重要的,详细的得看官网的用户手册,下面贴一下我在flex下做的Demo
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="demo()" width="1100" height="600" layout="absolute"> <mx:Script> <![CDATA[ import mx.controls.Image; import mx.containers.Box; import mx.core.UIComponent; import flash.display.Sprite; import flash.events.Event; import flash.utils.Timer; import flash.events.TimerEvent; import Box2D.Dynamics.*; import Box2D.Collision.*; import Box2D.Collision.Shapes.*; import Box2D.Common.Math.*; private var ui:UIComponent=new UIComponent; public var the_world:b2World; private var time_count:Timer=new Timer(500); public function demo():void { //init b2AABB var environment:b2AABB=new b2AABB(); environment.lowerBound.Set(-100.0, -100.0); environment.upperBound.Set(100.0, 100.0); //gravity var gravity:b2Vec2=new b2Vec2(0.0, 20.0); //init b2world the_world=new b2World(environment, gravity, true); the_world.DrawDebugData(); var debug_draw:b2DebugDraw=new b2DebugDraw(); var debug_sprite:Sprite=new Sprite(); debug_sprite.x=50; this.addChild(ui); ui.addChild(debug_sprite); debug_draw.m_sprite=debug_sprite; debug_draw.m_drawScale=40; debug_draw.m_fillAlpha=0.3; debug_draw.m_lineThickness=1; debug_draw.m_drawFlags=b2DebugDraw.e_shapeBit; the_world.SetDebugDraw(debug_draw); var final_body:b2Body; var the_body:b2BodyDef; //bodyDefination,定义了位置 the_body=new b2BodyDef(); the_body.position.Set(13, 13); //original Position var the_box:b2PolygonDef; //shape,定义了形状、摩擦力、密度 the_box=new b2PolygonDef(); the_box.SetAsBox(8.5, 0.5); //half meter the_box.friction=0.5; the_box.density=0; //static body ,mass == 0 final_body=the_world.CreateBody(the_body); final_body.CreateShape(the_box); final_body.SetMassFromShapes(); //compute mass addEventListener(Event.ENTER_FRAME, on_enter_frame); time_count.addEventListener(TimerEvent.TIMER, on_time); time_count.start(); } public function on_time(e:Event):void { var final_body:b2Body; var bodyDef:b2BodyDef; var shape:b2PolygonDef; bodyDef=new b2BodyDef(); bodyDef.position.Set(Math.random() * 10 + 6, 0); //矩形 shape=new b2PolygonDef(); shape.SetAsBox(Math.random() + 0.1, Math.random() + 0.1); shape.friction=0.3; shape.density=1; shape.restitution=0.5; final_body=the_world.CreateBody(bodyDef); //圆形 var circleShape:b2CircleDef=new b2CircleDef; circleShape.density=1; circleShape.friction=0.5; circleShape.restitution=0.5; circleShape.radius=Math.random() + 0.1; //三角 var triangleShape:b2PolygonDef=new b2PolygonDef; triangleShape.vertexCount=3; triangleShape.density=1; triangleShape.friction=0.5; triangleShape.restitution=0.5; var aaa:Number=Math.random() + 0.1; triangleShape.vertices[0].Set(-aaa, 0.0); triangleShape.vertices[1].Set(aaa, 0.0); triangleShape.vertices[2].Set(0.0, 2 * aaa); var tempN:Number = Math.random(); if(tempN <= 0.4) { final_body.CreateShape(triangleShape); } else if(tempN >= 0.7) { final_body.CreateShape(circleShape); } else { final_body.CreateShape(shape); } final_body.SetMassFromShapes(); } public function on_enter_frame(e:Event):void { the_world.Step(1 / 30, 20); for (var bb:b2Body=the_world.m_bodyList; bb; bb=bb.m_next) { //可以遍历world中的body } } ]]> </mx:Script> </mx:Application>