box2d滑轮关节(Pulley Joint)

Box2D everyinch 7101℃ 0评论

滑轮关节通过地面彼此连接在一起。当一个物体抬起,则另一个物体落下;滑轮绳索的长度为构造函数中配置的常量
length1 + length2 == constant
length1 + ratio * length2 == constant

基本示例代码如下:

1package{
2    import Box2D.Collision.Shapes.b2PolygonShape;
3    import Box2D.Collision.Shapes.b2Shape;
4    import Box2D.Collision.b2AABB;
5    import Box2D.Common.Math.b2Vec2;
6    import Box2D.Dynamics.Joints.b2MouseJoint;
7    import Box2D.Dynamics.Joints.b2MouseJointDef;
8    import Box2D.Dynamics.Joints.b2PulleyJoint;
9    import Box2D.Dynamics.Joints.b2PulleyJointDef;
10    import Box2D.Dynamics.b2Body;
11    import Box2D.Dynamics.b2BodyDef;
12    import Box2D.Dynamics.b2DebugDraw;
13    import Box2D.Dynamics.b2Fixture;
14    import Box2D.Dynamics.b2FixtureDef;
15    import Box2D.Dynamics.b2World;
16     
17    import flash.display.Sprite;
18    import flash.events.Event;
19    import flash.events.MouseEvent;
20     
21    [SWF(width="800",height="600",frameRate="32",backgroundColor="0x000000")]
22    public class PulleyJoint extends Sprite{
23        private var world:b2World;
24        private var timeStep:Number;
25        private var iterations:uint;
26        private var pixelsPerMeter:Number = 30;
27        private var mouseJoint:b2MouseJoint;
28        private var mousePVec:b2Vec2 = new b2Vec2();
29         
30        public function PulleyJoint(){
31            createWorld();
32            makeDebugDraw();
33            createWall();
34            createJoint();
35            addEventListener(Event.ENTER_FRAME,onEnterframe);
36            stage.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
37            stage.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
38        }
39         
40        private function createWorld():void{
41            var gravity:b2Vec2 = new b2Vec2(0.0,9.8);
42            var doSleep:Boolean = true;
43            world = new b2World(gravity,doSleep);
44            world.SetWarmStarting(true);
45            timeStep = 1.0/30.0;
46            iterations = 10;
47        }
48                 
49        private function makeDebugDraw():void{
50            var debugDraw:b2DebugDraw = new b2DebugDraw();
51            var debugSprite:Sprite = new Sprite();
52            addChild(debugSprite);
53            debugDraw.SetSprite(debugSprite);
54            debugDraw.SetDrawScale(30.0);
55            debugDraw.SetFillAlpha(0.5);
56            debugDraw.SetLineThickness(1.0);
57            debugDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit);
58            world.SetDebugDraw(debugDraw);
59        }
60         
61        private function createWall():void{
62            var bodyDef:b2BodyDef = new b2BodyDef();
63            // Body的中心点
64            bodyDef.position.Set(400/pixelsPerMeter,585/pixelsPerMeter);
65            var body:b2Body = world.CreateBody(bodyDef);
66            var shape:b2PolygonShape = new b2PolygonShape();
67            // 几何体的半宽和半高
68            shape.SetAsBox(400/pixelsPerMeter,15/pixelsPerMeter);
69            body.CreateFixture2(shape);
70        }
71         
72        private function createJoint():void{
73            var bodyDef:b2BodyDef = new b2BodyDef();
74            bodyDef.type = b2Body.b2_dynamicBody;
75            bodyDef.position.Set(420/pixelsPerMeter,300/pixelsPerMeter);
76            var boxShape1:b2PolygonShape = new b2PolygonShape();
77            boxShape1.SetAsBox(45/pixelsPerMeter,45/pixelsPerMeter);
78            var fixtureDef:b2FixtureDef = new b2FixtureDef();
79            fixtureDef.shape = boxShape1;
80            fixtureDef.density = 5;
81            var box1:b2Body = world.CreateBody(bodyDef);
82            box1.CreateFixture(fixtureDef);
83             
84            bodyDef = new b2BodyDef();
85            bodyDef.type = b2Body.b2_dynamicBody;
86            bodyDef.position.Set(300/pixelsPerMeter,300/pixelsPerMeter);
87            var boxShape2:b2PolygonShape = new b2PolygonShape();
88            boxShape2.SetAsBox(45/pixelsPerMeter,45/pixelsPerMeter);
89            fixtureDef.shape = boxShape2;
90            var box2:b2Body = world.CreateBody(bodyDef);
91            box2.CreateFixture(fixtureDef);
92             
93            var jointDef:b2PulleyJointDef = new b2PulleyJointDef();
94            var anchor1:b2Vec2 = box1.GetWorldCenter();
95            var anchor2:b2Vec2 = box2.GetWorldCenter();
96            var groundAnchor1:b2Vec2 = new b2Vec2(anchor1.x,anchor1.y-200/pixelsPerMeter);
97            var groundAnchor2:b2Vec2 = new b2Vec2(anchor2.x,anchor2.y-200/pixelsPerMeter);
98            var ratio:Number = 1.0;
99            jointDef.Initialize(box1,box2,groundAnchor1,groundAnchor2,anchor1,anchor2,ratio);
100            jointDef.maxLengthA = 300/pixelsPerMeter;
101            jointDef.maxLengthB = 300/pixelsPerMeter;
102            var joint:b2PulleyJoint = world.CreateJoint(jointDef) as b2PulleyJoint;
103        }
104         
105        private function onMouseDown(e:MouseEvent):void{
106            var body:b2Body = getBodyAtMouse();
107            if(body){
108                var mouseJointDef:b2MouseJointDef = new b2MouseJointDef();
109                mouseJointDef.bodyA = world.GetGroundBody();
110                mouseJointDef.bodyB = body;
111                mouseJointDef.target.Set(mouseX/pixelsPerMeter,mouseY/pixelsPerMeter);
112                mouseJointDef.maxForce = 30000;
113                mouseJoint = world.CreateJoint(mouseJointDef) as b2MouseJoint;
114            }
115        }
116         
117        private function onMouseUp(e:MouseEvent):void{
118            if(mouseJoint){
119                world.DestroyJoint(mouseJoint);
120                mouseJoint = null;
121            }
122        }
123         
124        private function getBodyAtMouse(includeStatic:Boolean = false):b2Body{
125            var mouseXWorldPhys:Number = mouseX/pixelsPerMeter;
126            var mouseYWorldPhys:Number = mouseY/pixelsPerMeter;
127            mousePVec.Set(mouseXWorldPhys,mouseYWorldPhys);
128            var aabb:b2AABB = new b2AABB();
129            aabb.lowerBound.Set(mouseXWorldPhys-0.001,mouseYWorldPhys-0.001);
130            aabb.upperBound.Set(mouseXWorldPhys+0.001,mouseYWorldPhys+0.001);
131            var body:b2Body = null;
132            var fixture:b2Fixture;
133            function GetBodyCallBack(fixture:b2Fixture):Boolean{
134                var shape:b2Shape = fixture.GetShape();
135                if(fixture.GetBody().GetType() != b2Body.b2_staticBody || includeStatic){
136                    var inside:Boolean = shape.TestPoint(fixture.GetBody().GetTransform(),mousePVec);
137                    if(inside){
138                        body = fixture.GetBody();
139                        return false;
140                    }
141                }
142                return true;
143            }
144            world.QueryAABB(GetBodyCallBack,aabb);
145            return body;
146        }
147         
148        private function onEnterframe(e:Event):void{
149            world.Step(timeStep,iterations,iterations);
150            world.ClearForces();
151            world.DrawDebugData();
152            if(mouseJoint){
153                var xpos:Number = mouseX/pixelsPerMeter;
154                var ypos:Number = mouseY/pixelsPerMeter;
155                var v2:b2Vec2 = new b2Vec2(xpos,ypos);
156                mouseJoint.SetTarget(v2);
157            }
158        }
159    }
160}

分享&收藏

转载请注明:陈童的博客 » box2d滑轮关节(Pulley Joint)

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

表情

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
(2)个小伙伴在吐槽
  1. 我不太理解那个滑轮的锚点的设置,是根据body1,body2两个物体的锚点然后转换比例设置的吗? PulleyJointDef pjd=new PulleyJiointDef(); Vec2 ga1 =new Vec2(anchor1x/RATE, anchor1y/RATE); Vec2 ga2 =new Vec2(anchor2x/RATE,anchor2y/RATE); pjd.initailize( body1 ,body2 ,ga1, ga2,body1.getWorldCenter(),body2.getWorldCenter(),1f); 滑轮的锚点ga1和ga2不太理解!!!!!!
    汤圆2013-05-12 13:09 回复
    • @汤圆首先理解错了,后来自己看懂了!!嘻嘻~~~~
      汤圆2013-05-12 23:09 回复
'; } if( dopt('d_footcode_b') ) echo dopt('d_footcode'); ?>