box2d齿轮关节(Gear Joint)

Box2D everyinch 5384℃ 0评论

齿轮关节需要两个物体通过旋转关节或平移关节和地面连接起来。和滑轮关节类似,齿轮关节也可以指定比率

var gearJointDef:b2GearJointDef = new b2GearJointDef();
gearJointDef.bodyA = gear1;
gearJointDef.bodyB = gear2;
gearJointDef.joint1 = joint1;
gearJointDef.joint2 = joint2;
gearJointDef.ratio = 1;
world.CreateJoint(gearJointDef);


基本示例代码如下:

package{
	import Box2D.Collision.Shapes.b2CircleShape;
	import Box2D.Collision.Shapes.b2PolygonShape;
	import Box2D.Collision.Shapes.b2Shape;
	import Box2D.Collision.b2AABB;
	import Box2D.Common.Math.b2Vec2;
	import Box2D.Dynamics.Joints.b2GearJoint;
	import Box2D.Dynamics.Joints.b2GearJointDef;
	import Box2D.Dynamics.Joints.b2MouseJoint;
	import Box2D.Dynamics.Joints.b2MouseJointDef;
	import Box2D.Dynamics.Joints.b2RevoluteJoint;
	import Box2D.Dynamics.Joints.b2RevoluteJointDef;
	import Box2D.Dynamics.b2Body;
	import Box2D.Dynamics.b2BodyDef;
	import Box2D.Dynamics.b2DebugDraw;
	import Box2D.Dynamics.b2Fixture;
	import Box2D.Dynamics.b2FixtureDef;
	import Box2D.Dynamics.b2World;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	[SWF(width="800",height="600",frameRate="32",backgroundColor="0x000000")]
	public class GearJoint extends Sprite{
		private var world:b2World;
		private var timeStep:Number;
		private var iterations:uint;
		private var pixelsPerMeter:Number = 30;
		private var mouseJoint:b2MouseJoint;
		private var mousePVec:b2Vec2 = new b2Vec2();
		
		public function GearJoint(){
			createWorld();
			makeDebugDraw();
			createWall();
			createJoint();
			addEventListener(Event.ENTER_FRAME,onEnterframe);
			stage.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
			stage.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
		}
		
		private function createWorld():void{
			var gravity:b2Vec2 = new b2Vec2(0.0,9.8);
			var doSleep:Boolean = true;
			world = new b2World(gravity,doSleep);
			world.SetWarmStarting(true);
			timeStep = 1.0/30.0;
			iterations = 10;
		}
				
		private function makeDebugDraw():void{
			var debugDraw:b2DebugDraw = new b2DebugDraw();
			var debugSprite:Sprite = new Sprite();
			addChild(debugSprite);
			debugDraw.SetSprite(debugSprite);
			debugDraw.SetDrawScale(30.0);
			debugDraw.SetFillAlpha(0.5);
			debugDraw.SetLineThickness(1.0);
			debugDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit);
			world.SetDebugDraw(debugDraw);
		}
		
		private function createWall():void{
			var bodyDef:b2BodyDef = new b2BodyDef();
			// Body的中心点
			bodyDef.position.Set(400/pixelsPerMeter,585/pixelsPerMeter);
			var body:b2Body = world.CreateBody(bodyDef);
			var shape:b2PolygonShape = new b2PolygonShape();
			// 几何体的半宽和半高
			shape.SetAsBox(400/pixelsPerMeter,15/pixelsPerMeter);
			body.CreateFixture2(shape);
		}
		
		private function createJoint():void{
			var bodyDef:b2BodyDef = new b2BodyDef();
			bodyDef.type = b2Body.b2_dynamicBody;
			bodyDef.position.Set(300/pixelsPerMeter,195/pixelsPerMeter);
			var circleShape1:b2CircleShape = new b2CircleShape();
			circleShape1.SetRadius(60/pixelsPerMeter);
			var fixtureDef:b2FixtureDef = new b2FixtureDef();
			fixtureDef.shape = circleShape1;
			fixtureDef.density = 5;
			var gear1:b2Body = world.CreateBody(bodyDef);
			gear1.CreateFixture(fixtureDef);
			
			bodyDef = new b2BodyDef();
			bodyDef.type = b2Body.b2_dynamicBody;
			bodyDef.position.Set(480/pixelsPerMeter,195/pixelsPerMeter);
			var circleShape2:b2CircleShape = new b2CircleShape();
			circleShape2.SetRadius(90/pixelsPerMeter);
			fixtureDef.shape = circleShape2;
			var gear2:b2Body = world.CreateBody(bodyDef);
			gear2.CreateFixture(fixtureDef);
			
			var jointDef:b2RevoluteJointDef = new b2RevoluteJointDef();
			jointDef.Initialize(world.GetGroundBody(),gear1,gear1.GetWorldCenter());
			var joint1:b2RevoluteJoint = world.CreateJoint(jointDef) as b2RevoluteJoint;
			jointDef.Initialize(world.GetGroundBody(),gear2,gear2.GetWorldCenter());
			var joint2:b2RevoluteJoint = world.CreateJoint(jointDef) as b2RevoluteJoint;
			
			var gearJointDef:b2GearJointDef = new b2GearJointDef();
			gearJointDef.bodyA = gear1;
			gearJointDef.bodyB = gear2;
			gearJointDef.joint1 = joint1;
			gearJointDef.joint2 = joint2;
			gearJointDef.ratio = 1;
			var gearJoint:b2GearJoint = world.CreateJoint(gearJointDef) as b2GearJoint;
		}
		
		private function onMouseDown(e:MouseEvent):void{
			var body:b2Body = getBodyAtMouse();
			if(body){
				var mouseJointDef:b2MouseJointDef = new b2MouseJointDef();
				mouseJointDef.bodyA = world.GetGroundBody();
				mouseJointDef.bodyB = body;
				mouseJointDef.target.Set(mouseX/pixelsPerMeter,mouseY/pixelsPerMeter);
				mouseJointDef.maxForce = 30000;
				mouseJoint = world.CreateJoint(mouseJointDef) as b2MouseJoint;
			}
		}
		
		private function onMouseUp(e:MouseEvent):void{
			if(mouseJoint){
				world.DestroyJoint(mouseJoint);
				mouseJoint = null;
			}
		}
		
		private function getBodyAtMouse(includeStatic:Boolean = false):b2Body{
			var mouseXWorldPhys:Number = mouseX/pixelsPerMeter;
			var mouseYWorldPhys:Number = mouseY/pixelsPerMeter;
			mousePVec.Set(mouseXWorldPhys,mouseYWorldPhys);
			var aabb:b2AABB = new b2AABB();
			aabb.lowerBound.Set(mouseXWorldPhys-0.001,mouseYWorldPhys-0.001);
			aabb.upperBound.Set(mouseXWorldPhys+0.001,mouseYWorldPhys+0.001);
			var body:b2Body = null;
			var fixture:b2Fixture;
			function GetBodyCallBack(fixture:b2Fixture):Boolean{
				var shape:b2Shape = fixture.GetShape();
				if(fixture.GetBody().GetType() != b2Body.b2_staticBody || includeStatic){
					var inside:Boolean = shape.TestPoint(fixture.GetBody().GetTransform(),mousePVec);
					if(inside){
						body = fixture.GetBody();
						return false;
					}
				}
				return true;
			}
			world.QueryAABB(GetBodyCallBack,aabb);
			return body;
		}
		
		private function onEnterframe(e:Event):void{
			world.Step(timeStep,iterations,iterations);
			world.ClearForces();
			world.DrawDebugData();
			if(mouseJoint){
				var xpos:Number = mouseX/pixelsPerMeter;
				var ypos:Number = mouseY/pixelsPerMeter;
				var v2:b2Vec2 = new b2Vec2(xpos,ypos);
				mouseJoint.SetTarget(v2);
			}
		}
	}
}

分享&收藏

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

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

表情

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

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