box2d旋转关节之汽车

Box2D everyinch 6235℃ 0评论
package{
	import Box2D.Collision.Shapes.b2CircleShape;
	import Box2D.Collision.Shapes.b2PolygonShape;
	import Box2D.Common.Math.b2Vec2;
	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.b2FixtureDef;
	import Box2D.Dynamics.b2World;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	
	[SWF(width="800",height="600",frameRate="32",backgroundColor="0x000000")]
	public class RevoluteJoint_Vehicle extends Sprite{
		private var world:b2World;
		private var timeStep:Number;
		private var iterations:uint;
		private var pixelsPerMeter:Number = 30;
		private var frontJoint:b2RevoluteJoint;
		private var rearJoint:b2RevoluteJoint;
		
		public function RevoluteJoint_Vehicle(){
			createWorld();
			makeDebugDraw();
			createGround();
			createVehicle();
			stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDown);
			addEventListener(Event.ENTER_FRAME,onEnterframe);
		}
		
		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);
			world.SetDebugDraw(debugDraw);
		}
		
		private function createGround():void{
			var bodyDef:b2BodyDef = new b2BodyDef();
			bodyDef.position.Set(400/pixelsPerMeter,585/pixelsPerMeter);
			var box:b2PolygonShape = new b2PolygonShape();
			box.SetAsBox(400/pixelsPerMeter,15/pixelsPerMeter);
			var fixtureDef:b2FixtureDef = new b2FixtureDef();
			fixtureDef.shape = box;
			fixtureDef.density = 1;
			fixtureDef.friction = 1;
			var body:b2Body = world.CreateBody(bodyDef);
			body.CreateFixture(fixtureDef);
			
			bodyDef.position.Set(50/pixelsPerMeter,550/pixelsPerMeter);
			box.SetAsBox(50/pixelsPerMeter,50/pixelsPerMeter);
			fixtureDef.shape = box;
			fixtureDef.density = 1;
			fixtureDef.friction = 1;
			body = world.CreateBody(bodyDef);
			body.CreateFixture(fixtureDef);
			
			bodyDef.position.Set(750/pixelsPerMeter,550/pixelsPerMeter);
			box.SetAsBox(50/pixelsPerMeter,50/pixelsPerMeter);
			fixtureDef.shape = box;
			fixtureDef.density = 1;
			fixtureDef.friction = 1;
			body = world.CreateBody(bodyDef);
			body.CreateFixture(fixtureDef);
		}
		
		private function createVehicle():void{
			var bodyDef:b2BodyDef = new b2BodyDef();
			bodyDef.position.Set(250/pixelsPerMeter,35/pixelsPerMeter);
			bodyDef.type = b2Body.b2_dynamicBody;
			var box:b2PolygonShape = new b2PolygonShape();
			box.SetAsBox(50/pixelsPerMeter,10/pixelsPerMeter);
			var fixtureDef:b2FixtureDef = new b2FixtureDef();
			fixtureDef.shape = box;
			fixtureDef.density = 1;
			fixtureDef.friction = 1;
			fixtureDef.restitution = 0.1;
			var body:b2Body = world.CreateBody(bodyDef);
			body.CreateFixture(fixtureDef);
			
			var circle:b2CircleShape = new b2CircleShape(20/pixelsPerMeter);
			fixtureDef.shape = circle;
			fixtureDef.density = 1;
			fixtureDef.friction = 5;
			fixtureDef.restitution = 0.1;
			bodyDef.allowSleep = false;
			bodyDef.position.Set(body.GetWorldCenter().x+40/pixelsPerMeter,body.GetWorldCenter().y);
			var frontWheel:b2Body = world.CreateBody(bodyDef);
			frontWheel.CreateFixture(fixtureDef);
			
			bodyDef.position.Set(body.GetWorldCenter().x-40/pixelsPerMeter,body.GetWorldCenter().y);
			var rearWheel:b2Body = world.CreateBody(bodyDef);
			rearWheel.CreateFixture(fixtureDef);
			
			var frontJointDef:b2RevoluteJointDef = new b2RevoluteJointDef();
			frontJointDef.enableMotor = true;
			frontJointDef.maxMotorTorque = 8;
			frontJointDef.Initialize(body,frontWheel,frontWheel.GetWorldCenter());
			frontJoint = world.CreateJoint(frontJointDef) as b2RevoluteJoint;
			
			var rearJointDef:b2RevoluteJointDef = new b2RevoluteJointDef();
			rearJointDef.enableMotor = true;
			rearJointDef.maxMotorTorque = 8;
			rearJointDef.Initialize(body,rearWheel,rearWheel.GetWorldCenter());
			rearJoint = world.CreateJoint(rearJointDef) as b2RevoluteJoint;
		}
		
		private function onKeyDown(e:KeyboardEvent):void{
			switch(e.keyCode){
				case Keyboard.LEFT:
					rearJoint.SetMotorSpeed(-8);
					frontJoint.SetMotorSpeed(-8);
					break;
				case Keyboard.RIGHT:
					rearJoint.SetMotorSpeed(8);
					frontJoint.SetMotorSpeed(8);
					break;
			}
		}
		
		private function onEnterframe(e:Event):void{
			world.Step(timeStep,iterations,iterations);
			world.ClearForces();
			world.DrawDebugData();
		}
	}
}

分享&收藏

转载请注明:陈童的博客 » box2d旋转关节之汽车

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

表情

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

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