1: package
2: {
3: import Box2D.Collision.Shapes.b2PolygonShape;
4: import Box2D.Common.Math.b2Vec2;
5: import Box2D.Dynamics.b2Body;
6: import Box2D.Dynamics.b2BodyDef;
7: import Box2D.Dynamics.b2DebugDraw;
8: import Box2D.Dynamics.b2FixtureDef;
9: import Box2D.Dynamics.b2World;
10:
11: import flash.display.Sprite;
12: import flash.events.Event;
13:
14: public class Initialization extends Sprite
15: {
16: private var _world:b2World;
17: private var scale:uint=30;
18:
19: public function Initialization()
20: {
21: super();
22: this.addEventListener(Event.ENTER_FRAME, function():void
23: {
24: _world.Step(1 / 30, 10, 10);
25: _world.ClearForces();
26: _world.DrawDebugData();
27: });
28:
29: this._world=new b2World(new b2Vec2(0, 1), true);
30:
31:
32: var dbgDraw:b2DebugDraw=new b2DebugDraw();
33:
34: dbgDraw=new b2DebugDraw();
35: dbgDraw.SetSprite(this);
36: dbgDraw.SetDrawScale(30.0);
37: dbgDraw.SetFillAlpha(0.5);
38: dbgDraw.SetLineThickness(1.0);
39: dbgDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit);
40:
41: _world.SetDebugDraw(dbgDraw);
42:
43: this.fixRect(); //add fix shape
44: this.dinamicRect(); //add dynamic shape
45: }
46:
47: private function fixRect():void
48: {
49: var bd:b2BodyDef=new b2BodyDef();
50: bd.position.Set(10, 10);
51:
52: var rect:b2PolygonShape=new b2PolygonShape();
53: rect.SetAsBox(2, 2);
54:
55: var body:b2Body=_world.CreateBody(bd);
56:
57: var fd:b2FixtureDef=new b2FixtureDef();
58: fd.shape=rect;
59: fd.friction=0.3;
60: fd.density=0;
61:
62: body.CreateFixture(fd);
63: }
64:
65: private function dinamicRect():void
66: {
67: var bd:b2BodyDef=new b2BodyDef();
68: bd.position.Set(5, 5);
69: bd.type=b2Body.b2_dynamicBody;
70:
71: var rect:b2PolygonShape=new b2PolygonShape();
72: rect.SetAsBox(2, 2);
73:
74: var body:b2Body=_world.CreateBody(bd);
75:
76: var fd:b2FixtureDef=new b2FixtureDef();
77: fd.shape=rect;
78: fd.friction=0.3;
79: fd.density=0;
80:
81: body.CreateFixture(fd);
82: }
83: }
84: }
85:
沒有留言:
張貼留言