package { /** * ... * @author Pramod */ import flash.display.MovieClip; import flash.display.Sprite; import flash.events.Event; import flash.events.KeyboardEvent; import Box2D.Dynamics.*; import Box2D.Collision.*; import Box2D.Collision.Shapes.*; import Box2D.Common.Math.*; import Box2D.Dynamics.Joints.*; public class LandRover extends MovieClip { private var world:b2World = new b2World(new b2Vec2(0, 10), true); private var worldScale:Number = 30; private var car:b2Body; private var rearWheelRevoluteJoint:b2RevoluteJoint; private var frontWheelRevoluteJoint:b2RevoluteJoint; private var left:Boolean = false; private var right:Boolean = false; private var up:Boolean = false; private var down:Boolean = false; private var motorSpeed:Number = 0; private var frontAxlePrismaticJoint:b2PrismaticJoint; private var rearAxlePrismaticJoint:b2PrismaticJoint; private var debugSprite:Sprite; public function LandRover() { var debugDraw:b2DebugDraw = new b2DebugDraw; debugSprite = new Sprite(); addChild(debugSprite); debugDraw.SetSprite(debugSprite); debugDraw.SetDrawScale(30.0); debugDraw.SetFillAlpha(0.3); debugDraw.SetLineThickness(1.0); debugDraw.SetFlags(b2DebugDraw.e_shapeBit); world.SetDebugDraw(debugDraw); //The Floor createGround(); //puma var backShape:b2PolygonShape = new b2PolygonShape(); var p1:b2Vec2 = new b2Vec2(-112.6 / worldScale, -42 / worldScale); var p2:b2Vec2 = new b2Vec2(-6.6 / worldScale, -40 / worldScale); var p3:b2Vec2 = new b2Vec2( 37.4 / worldScale, -36 / worldScale); var p4:b2Vec2 = new b2Vec2( 60.4 / worldScale, -5 / worldScale); var p5:b2Vec2 = new b2Vec2( 66.4/ worldScale, 39 / worldScale); var p6:b2Vec2 = new b2Vec2( -86.6/ worldScale, 39/ worldScale); var p7:b2Vec2 = new b2Vec2(-117.6 / worldScale, 30/ worldScale); var backVertices:Array = [p1,p2,p3,p4,p5,p6,p7]; backShape.SetAsArray(backVertices); var backFixture:b2FixtureDef = new b2FixtureDef(); backFixture.shape = backShape; backFixture.density = 1; backFixture.friction = 3; backFixture.restitution = 0.3; backFixture.filter.groupIndex = -1; var frontShape:b2PolygonShape = new b2PolygonShape(); var p8:b2Vec2 = new b2Vec2(114.4/ worldScale, 2/ worldScale); var p9:b2Vec2 = new b2Vec2(118.4 / worldScale, 33/ worldScale); var p10:b2Vec2 = new b2Vec2( 66.4 / worldScale, 39 / worldScale); var p11:b2Vec2 = new b2Vec2(60.4 / worldScale, -5 / worldScale); var frontVertices:Array = [p8,p9,p10,p11]; frontShape.SetAsArray(frontVertices); var frontFixture:b2FixtureDef = new b2FixtureDef(); frontFixture.shape = frontShape; frontFixture.density = 1; frontFixture.friction = 3; frontFixture.restitution = 0.3; frontFixture.filter.groupIndex = -1; var carBodyDef:b2BodyDef = new b2BodyDef(); carBodyDef.type = b2Body.b2_dynamicBody; carBodyDef.position.Set(200 / worldScale, 50 / worldScale); car = world.CreateBody(carBodyDef); car.CreateFixture(backFixture); car.CreateFixture(frontFixture); car.SetUserData(new puma()); debugSprite.addChild(car.GetUserData()); //Axles var axleShape:b2PolygonShape = new b2PolygonShape(); axleShape.SetAsBox(10 / worldScale, 10 / worldScale); var axleFixture:b2FixtureDef = new b2FixtureDef(); axleFixture.density = 0.5; axleFixture.friction = 3; axleFixture.restitution = 0.3; axleFixture.shape = axleShape; axleFixture.filter.groupIndex = -1; var axleBodyDef:b2BodyDef = new b2BodyDef(); axleBodyDef.type = b2Body.b2_dynamicBody; //The Rear Axle axleBodyDef.position.Set(car.GetWorldCenter().x - (52 / worldScale), car.GetWorldCenter().y + (50 / worldScale)); var rearAxle:b2Body = world.CreateBody(axleBodyDef); rearAxle.CreateFixture(axleFixture); //The Front Axle axleBodyDef.position.Set(car.GetWorldCenter().x + (100/ worldScale), car.GetWorldCenter().y + (50/ worldScale)); var frontAxle:b2Body = world.CreateBody(axleBodyDef); frontAxle.CreateFixture(axleFixture); //The Wheels var wheelShape:b2CircleShape = new b2CircleShape(25/worldScale); var wheelFixture:b2FixtureDef = new b2FixtureDef(); wheelFixture.shape = wheelShape; wheelFixture.density = 1; wheelFixture.friction = 3; wheelFixture.restitution = 0.1; wheelFixture.filter.groupIndex = -1; var wheelBodyDef:b2BodyDef = new b2BodyDef(); wheelBodyDef.type = b2Body.b2_dynamicBody; //Rear Wheel wheelBodyDef.position.Set(car.GetWorldCenter().x - (52 / worldScale), car.GetWorldCenter().y + (50 / worldScale)); var rearWheel:b2Body = world.CreateBody(wheelBodyDef); rearWheel.CreateFixture(wheelFixture); rearWheel.SetUserData(new wheel()); debugSprite.addChild(rearWheel.GetUserData()); //Front Wheel wheelBodyDef.position.Set(car.GetWorldCenter().x + (100/ worldScale), car.GetWorldCenter().y + (50 / worldScale)); var frontWheel:b2Body = world.CreateBody(wheelBodyDef); frontWheel.CreateFixture(wheelFixture); frontWheel.SetUserData(new wheel()); debugSprite.addChild(frontWheel.GetUserData()); //Revolute Joints //rear wheel joint var rearWheelRevoluteJointDef:b2RevoluteJointDef = new b2RevoluteJointDef(); rearWheelRevoluteJointDef.Initialize(rearWheel, rearAxle, rearWheel.GetWorldCenter()); rearWheelRevoluteJointDef.enableMotor = true; rearWheelRevoluteJointDef.maxMotorTorque = 100; rearWheelRevoluteJoint = world.CreateJoint(rearWheelRevoluteJointDef) as b2RevoluteJoint; //front wheel joint var frontWheelRevoluteJointDef:b2RevoluteJointDef = new b2RevoluteJointDef(); frontWheelRevoluteJointDef.Initialize(frontWheel, frontAxle, frontWheel.GetWorldCenter()); frontWheelRevoluteJointDef.enableMotor = true; frontWheelRevoluteJointDef.maxMotorTorque = 100; frontWheelRevoluteJoint = world.CreateJoint(frontWheelRevoluteJointDef) as b2RevoluteJoint; //Prismatic Joints var axlePrismaticJointDef:b2PrismaticJointDef = new b2PrismaticJointDef(); axlePrismaticJointDef.lowerTranslation = -1 / worldScale; axlePrismaticJointDef.upperTranslation = 1/ worldScale; axlePrismaticJointDef.enableLimit = true; axlePrismaticJointDef.enableMotor = true; //Front Shocks axlePrismaticJointDef.Initialize(car, frontAxle, frontAxle.GetWorldCenter(), new b2Vec2(0, 1)); frontAxlePrismaticJoint = world.CreateJoint(axlePrismaticJointDef) as b2PrismaticJoint; //Rear Shocks axlePrismaticJointDef.Initialize(car, rearAxle, rearAxle.GetWorldCenter(), new b2Vec2(0, 1)); rearAxlePrismaticJoint = world.CreateJoint(axlePrismaticJointDef) as b2PrismaticJoint; var sprite1:Sprite=new Sprite(); addChild(sprite1); setChildIndex(sprite1,numChildren-1); var health:HealthBar=new HealthBar(); sprite1.addChild(health); addEventListener(Event.ENTER_FRAME, update); stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed); stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased); } //Ground private function createGround():void { //.... } //key press private function keyPressed(e:KeyboardEvent):void { if (e.keyCode == 37) { left = true; } if (e.keyCode == 39) { right = true; } if (e.keyCode == 40) { up = true; } if (e.keyCode == 38) { down = true; } } //key release private function keyReleased(e:KeyboardEvent):void { if (e.keyCode == 37) { left = false; } if (e.keyCode == 39) { right = false; } if (e.keyCode == 40) { up = false; } if (e.keyCode == 38) { down = false; } } private function update(e:Event):void { world.Step(1 / 30, 10, 10); var stageX:Number; var stageY:Number; for (var bb:b2Body = world.GetBodyList(); bb; bb = bb.GetNext()) { if (bb.GetUserData() is puma) { bb.GetUserData().y = bb.GetPosition().y*30 ; bb.GetUserData().x = bb.GetPosition().x * 30; bb.GetUserData().rotation = bb.GetAngle() * (180 / Math.PI); stageX = 200 - bb.GetPosition().x * worldScale; stageY=200-bb.GetPosition().y*worldScale; }else if(bb.GetUserData() is wheel) { bb.GetUserData().y = bb.GetPosition().y*30 ; bb.GetUserData().x = bb.GetPosition().x * 30; bb.GetUserData().rotation = bb.GetAngle() * (180 / Math.PI); } } if (up) { motorSpeed += 0.5; } if (down) { motorSpeed -= 0.5; } if (left) { car.ApplyTorque(-80); } if (right) { car.ApplyTorque(80); } if (car.GetAngularVelocity() > 0.1) car.SetAngularVelocity (0.1); if (car.GetAngularVelocity() < -0.1) car.SetAngularVelocity (-0.1); motorSpeed *= 1; if (motorSpeed > 150) motorSpeed = 150; rearWheelRevoluteJoint.SetMotorSpeed(motorSpeed); frontWheelRevoluteJoint.SetMotorSpeed(motorSpeed); frontAxlePrismaticJoint.SetMaxMotorForce(Math.abs(600 * frontAxlePrismaticJoint.GetJointTranslation())); frontAxlePrismaticJoint.SetMotorSpeed((frontAxlePrismaticJoint.GetMotorSpeed() - 2 * frontAxlePrismaticJoint.GetJointTranslation())); rearAxlePrismaticJoint.SetMaxMotorForce(Math.abs(600 * rearAxlePrismaticJoint.GetJointTranslation())); rearAxlePrismaticJoint.SetMotorSpeed((rearAxlePrismaticJoint.GetMotorSpeed() - 2 * rearAxlePrismaticJoint.GetJointTranslation())); //world.ClearForces(); world.DrawDebugData(); debugSprite.x = stageX; debugSprite.y = stageY; } } }