box2d实时绘制圆形方法之一:鼠标拖放方式

Box2D everyinch 3486℃ 0评论
1package{
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;
10     
11    import flash.display.Sprite;
12    import flash.events.Event;
13    import flash.events.MouseEvent;
14     
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;
24         
25        public function DrawCircle1(){
26            createWorld();
27            makeDebugDraw();
28            addGround(400/pixelsPerMeter,585/pixelsPerMeter,400/pixelsPerMeter,15/pixelsPerMeter);
29             
30            addEventListener(Event.ENTER_FRAME,onEnterframe);
31            stage.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
32            stage.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
33        }
34         
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);
40            timeStep = 1.0/30.0;
41            iterations = 10;
42        }
43 
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);
54        }
55         
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);
63        }
64                 
65        private function onMouseDown(e:MouseEvent):void{
66            initX = e.localX;
67            initY = e.localY;
68            drawing = true;
69            stage.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
70        }
71         
72        private function onMouseMove(e:MouseEvent):void{
73            if(drawing){
74                graphics.clear();
75                graphics.beginFill(0xFF0000,0.5);
76                graphics.drawCircle(initX,initY,e.localX-initX);
77            }
78        }
79         
80        private function onMouseUp(e:MouseEvent):void{
81            graphics.clear();
82            drawing = false;
83             
84            var finalX:Number = e.localX;
85            var finalY:Number = e.localY;
86             
87            var radius:Number = Math.abs(finalX-initX);
88            if(radius > 0){
89                addCircle(initX/pixelsPerMeter,initY/pixelsPerMeter,radius/pixelsPerMeter);
90            }
91        }
92         
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);
106        }
107 
108        private function onEnterframe(e:Event):void{
109            world.Step(timeStep,iterations,iterations);
110            world.ClearForces();
111            world.DrawDebugData();
112        }
113    }
114}

分享&收藏

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

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

表情

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

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