2 | import Box2D.Collision.Shapes.b2CircleShape; |
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; |
11 | import flash.display.Sprite; |
12 | import flash.events.Event; |
13 | import flash.events.MouseEvent; |
15 | [SWF(width= "800" ,height= "600" ,frameRate= "32" ,backgroundColor= "0x000000" )] |
16 | public class DrawCircle1 extends Sprite{ |
17 | private var world:b2World; |
18 | private var timeStep: Number ; |
19 | private var iterations: uint ; |
20 | private var pixelsPerMeter: Number = 30 ; |
21 | private var initX: Number = 0.0 ; |
22 | private var initY: Number = 0.0 ; |
23 | private var drawing: Boolean = false ; |
25 | public function DrawCircle1(){ |
28 | addGround( 400 /pixelsPerMeter, 585 /pixelsPerMeter, 400 /pixelsPerMeter, 15 /pixelsPerMeter); |
30 | addEventListener(Event.ENTER_FRAME,onEnterframe); |
31 | stage.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown); |
32 | stage.addEventListener(MouseEvent.MOUSE_UP,onMouseUp); |
35 | private function createWorld(): void { |
36 | var gravity:b2Vec2 = new b2Vec2( 0.0 , 9.8 ); |
37 | var doSleep: Boolean = true ; |
38 | world = new b2World(gravity,doSleep); |
39 | world.SetWarmStarting( true ); |
44 | private function makeDebugDraw(): void { |
45 | var debugDraw:b2DebugDraw = new b2DebugDraw(); |
46 | var debugSprite:Sprite = new Sprite(); |
47 | addChild(debugSprite); |
48 | debugDraw.SetSprite(debugSprite); |
49 | debugDraw.SetDrawScale( 30.0 ); |
50 | debugDraw.SetFillAlpha( 0.5 ); |
51 | debugDraw.SetLineThickness( 1.0 ); |
52 | debugDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit); |
53 | world.SetDebugDraw(debugDraw); |
56 | private function addGround(x: Number ,y: Number ,hx: Number ,hy: Number ): void { |
57 | var bodyDef:b2BodyDef = new b2BodyDef(); |
58 | bodyDef.position.Set(x,y); |
59 | var body:b2Body = world.CreateBody(bodyDef); |
60 | var shape:b2PolygonShape = new b2PolygonShape(); |
61 | shape.SetAsBox(hx,hy); |
62 | body.CreateFixture2(shape); |
65 | private function onMouseDown(e:MouseEvent): void { |
69 | stage.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove); |
72 | private function onMouseMove(e:MouseEvent): void { |
75 | graphics.beginFill( 0xFF0000 , 0.5 ); |
76 | graphics.drawCircle(initX,initY,e.localX-initX); |
80 | private function onMouseUp(e:MouseEvent): void { |
84 | var finalX: Number = e.localX; |
85 | var finalY: Number = e.localY; |
87 | var radius: Number = Math.abs(finalX-initX); |
89 | addCircle(initX/pixelsPerMeter,initY/pixelsPerMeter,radius/pixelsPerMeter); |
93 | private function addCircle(x: Number ,y: Number ,radius: Number ): void { |
94 | var bodyDef:b2BodyDef = new b2BodyDef(); |
95 | bodyDef.type = b2Body.b2_dynamicBody; |
96 | bodyDef.position.Set(x,y); |
97 | var circle:b2CircleShape = new b2CircleShape(); |
98 | circle.SetRadius(radius); |
99 | var fixtureDef:b2FixtureDef = new b2FixtureDef(); |
100 | fixtureDef.shape = circle; |
101 | fixtureDef.density = 1.0 ; |
102 | fixtureDef.friction = 0.3 ; |
103 | fixtureDef.restitution = 0.2 ; |
104 | var body:b2Body = world.CreateBody(bodyDef); |
105 | body.CreateFixture(fixtureDef); |
108 | private function onEnterframe(e:Event): void { |
109 | world.Step(timeStep,iterations,iterations); |
111 | world.DrawDebugData(); |

转载请注明:陈童的博客 » box2d实时绘制圆形方法之一:鼠标拖放方式