开发学院图形图像Flash Two ways to make Box2D cars 阅读

Two ways to make Box2D cars

 2009-10-27 00:00:00 来源:WEB开发网   
核心提示:I am showing you two ways to make cars with Box2d, with source codes includedThese are examples I found on the web – I will make my ones later – you

I am showing you two ways to make cars with Box2d, with source codes included

These are examples I found on the web – I will make my ones later – you can find interesting in order to make cars using Box2D.

Top Down Car Simulation

Making a top down game in Box2D is not that different than making a side view game… you just don’t have to set gravity.

Here it is a top-down racing game

And this is the script to handle it

import Box2D.Collision.*;// bounding box of our world
import Box2D.Common.Math.*;// for vector(define gravity)
import Box2D.Dynamics.*;// define bodies and define world
import Box2D.Dynamics.Joints.*;
import Box2D.Collision.Shapes.*;// define our shapes
import flash.display.*;// sprite class
 
const MAX_STEER_ANGLE:Number = Math.PI/3;
const STEER_SPEED = 1.5;
const SIDEWAYS_FRICTION_FORCE:Number = 10;
const HORSEPOWERS:Number = 40
const CAR_STARTING_POS:b2Vec2 = new b2Vec2(10,10);
 
const leftRearWheelPosition:b2Vec2 = new b2Vec2(-1.5,1.90);
const rightRearWheelPosition:b2Vec2 = new b2Vec2(1.5,1.9);
const leftFrontWheelPosition:b2Vec2 = new b2Vec2(-1.5,-1.9);
const rightFrontWheelPosition:b2Vec2 = new b2Vec2(1.5,-1.9);
 
var engineSpeed:Number =0;
var steeringAngle:Number = 0
 
var myWorld:b2World;
 
var worldBox:b2AABB = new b2AABB();
worldBox.lowerBound.Set(-100,-100);
worldBox.upperBound.Set(100,100);
 
myWorld = new b2World(worldBox, new b2Vec2(0,0) , true);
 
//Create some static stuff
var staticDef = new b2BodyDef();
staticDef.position.Set(5,20);
var staticBox = new b2PolygonDef();
staticBox.SetAsBox(5,5);
myWorld.CreateBody(staticDef).CreateShape(staticBox);
staticDef.position.x = 25;
myWorld.CreateBody(staticDef).CreateShape(staticBox);
staticDef.position.Set(15, 24);
myWorld.CreateBody(staticDef).CreateShape(staticBox);
 
 
// define our body
var bodyDef:b2BodyDef = new b2BodyDef();
bodyDef.linearDamping = 1;
bodyDef.angularDamping = 1;
bodyDef.position = CAR_STARTING_POS.Copy()
 
var body:b2Body = myWorld.CreateBody(bodyDef);
body.SetMassFromShapes();
 
var leftWheelDef:b2BodyDef = new b2BodyDef();
leftWheelDef.position = CAR_STARTING_POS.Copy();
leftWheelDef.position.Add(leftFrontWheelPosition);
var leftWheel:b2Body = myWorld.CreateBody(leftWheelDef);
 
var rightWheelDef:b2BodyDef = new b2BodyDef();
rightWheelDef.position = CAR_STARTING_POS.Copy();
rightWheelDef.position.Add(rightFrontWheelPosition);
var rightWheel:b2Body = myWorld.CreateBody(rightWheelDef);
 
var leftRearWheelDef:b2BodyDef = new b2BodyDef();
leftRearWheelDef.position = CAR_STARTING_POS.Copy();
leftRearWheelDef.position.Add(leftRearWheelPosition);
var leftRearWheel:b2Body = myWorld.CreateBody(leftRearWheelDef);
 
var rightRearWheelDef:b2BodyDef = new b2BodyDef();
rightRearWheelDef.position = CAR_STARTING_POS.Copy();
rightRearWheelDef.position.Add(rightRearWheelPosition);
var rightRearWheel:b2Body = myWorld.CreateBody(rightRearWheelDef);
 
// define our shapes
var boxDef:b2PolygonDef = new b2PolygonDef();
boxDef.SetAsBox(1.5,2.5);
boxDef.density = 1;
body.CreateShape(boxDef);
 
//Left Wheel shape
var leftWheelShapeDef = new b2PolygonDef();
leftWheelShapeDef.SetAsBox(0.2,0.5);
leftWheelShapeDef.density = 1;
leftWheel.CreateShape(leftWheelShapeDef);
 
//Right Wheel shape
var rightWheelShapeDef = new b2PolygonDef();
rightWheelShapeDef.SetAsBox(0.2,0.5);
rightWheelShapeDef.density = 1;
rightWheel.CreateShape(rightWheelShapeDef);
 
//Left Wheel shape
var leftRearWheelShapeDef = new b2PolygonDef();
leftRearWheelShapeDef.SetAsBox(0.2,0.5);
leftRearWheelShapeDef.density = 1;
leftRearWheel.CreateShape(leftRearWheelShapeDef);
 
//Right Wheel shape
var rightRearWheelShapeDef = new b2PolygonDef();
rightRearWheelShapeDef.SetAsBox(0.2,0.5);
rightRearWheelShapeDef.density = 1;
rightRearWheel.CreateShape(rightRearWheelShapeDef);
 
body.SetMassFromShapes();
leftWheel.SetMassFromShapes();
rightWheel.SetMassFromShapes();
leftRearWheel.SetMassFromShapes();
rightRearWheel.SetMassFromShapes();
 
var leftJointDef:b2RevoluteJointDef = new b2RevoluteJointDef();
leftJointDef.Initialize(body, leftWheel, leftWheel.GetWorldCenter());
leftJointDef.enableMotor = true;
leftJointDef.maxMotorTorque = 100;
 
var rightJointDef = new b2RevoluteJointDef();
rightJointDef.Initialize(body, rightWheel, rightWheel.GetWorldCenter());
rightJointDef.enableMotor = true;
rightJointDef.maxMotorTorque = 100;
 
var leftJoint:b2RevoluteJoint = b2RevoluteJoint(myWorld.CreateJoint(leftJointDef));
var rightJoint:b2RevoluteJoint = b2RevoluteJoint(myWorld.CreateJoint(rightJointDef));
 
var leftRearJointDef:b2PrismaticJointDef = new b2PrismaticJointDef();
leftRearJointDef.Initialize(body, leftRearWheel, leftRearWheel.GetWorldCenter(), new b2Vec2(1,0));
leftRearJointDef.enableLimit = true;
leftRearJointDef.lowerTranslation = leftRearJointDef.upperTranslation = 0;
 
var rightRearJointDef:b2PrismaticJointDef = new b2PrismaticJointDef();
rightRearJointDef.Initialize(body, rightRearWheel, rightRearWheel.GetWorldCenter(), new b2Vec2(1,0));
rightRearJointDef.enableLimit = true;
rightRearJointDef.lowerTranslation = rightRearJointDef.upperTranslation = 0;
 
myWorld.CreateJoint(leftRearJointDef);
myWorld.CreateJoint(rightRearJointDef);
 
 
// debug draw
var dbgDraw:b2DebugDraw = new b2DebugDraw();
dbgDraw.m_sprite = new Sprite();
addChild(dbgDraw.m_sprite);
dbgDraw.m_drawScale = 20.0;
dbgDraw.m_fillAlpha = 0.3;
dbgDraw.m_lineThickness = 1.0;
dbgDraw.m_drawFlags = b2DebugDraw.e_shapeBit |b2DebugDraw.e_centerOfMassBit;
myWorld.SetDebugDraw(dbgDraw);
 
 
//========== FUNCTIONS ==========
 
//This function applies a "friction" in a direction orthogonal to the body's axis.
function killOrthogonalVelocity(targetBody:b2Body){
 var localPoint = new b2Vec2(0,0);
 var velocity:b2Vec2 = targetBody.GetLinearVelocityFromLocalPoint(localPoint);
 
 var sidewaysAxis = targetBody.GetXForm().R.col2.Copy();
 sidewaysAxis.Multiply(b2Math.b2Dot(velocity,sidewaysAxis))
 
 targetBody.SetLinearVelocity(sidewaysAxis);//targetBody.GetWorldPoint(localPoint));
}
 
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed_handler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased_handler);
 
function keyPressed_handler(e:KeyboardEvent) {
 if(e.keyCode == Keyboard.UP){
  body.WakeUp();
  engineSpeed = -HORSEPOWERS;
 }
 if(e.keyCode == Keyboard.DOWN){
  engineSpeed = HORSEPOWERS;
 }
 if(e.keyCode == Keyboard.RIGHT){
  steeringAngle = MAX_STEER_ANGLE
 }
 if(e.keyCode == Keyboard.LEFT){
  steeringAngle = -MAX_STEER_ANGLE
 }
}
 
function keyReleased_handler(e:KeyboardEvent){
 if(e.keyCode == Keyboard.UP || e.keyCode == Keyboard.DOWN){
  engineSpeed = 0;
 } 
 if(e.keyCode == Keyboard.LEFT || e.keyCode == Keyboard.RIGHT){
  steeringAngle = 0;
 }
}
 
addEventListener(Event.ENTER_FRAME, Update, false, 0 , true);
 
function Update(e:Event):void {
 myWorld.Step(1/30, 8);
 killOrthogonalVelocity(leftWheel);
 killOrthogonalVelocity(rightWheel);
 killOrthogonalVelocity(leftRearWheel);
 killOrthogonalVelocity(rightRearWheel);
 
 //Driving
 var ldirection = leftWheel.GetXForm().R.col2.Copy();
 ldirection.Multiply(engineSpeed);
 var rdirection = rightWheel.GetXForm().R.col2.Copy()
 rdirection.Multiply(engineSpeed);
 leftWheel.ApplyForce(ldirection, leftWheel.GetPosition());
 rightWheel.ApplyForce(rdirection, rightWheel.GetPosition());
 
 //Steering
 var mspeed:Number;
 mspeed = steeringAngle - leftJoint.GetJointAngle();
 leftJoint.SetMotorSpeed(mspeed * STEER_SPEED);
 mspeed = steeringAngle - rightJoint.GetJointAngle();
 rightJoint.SetMotorSpeed(mspeed * STEER_SPEED);
}

1 2  下一页

Tags:Two ways to

编辑录入:爽爽 [复制链接] [打 印]
[]
  • 好
  • 好的评价 如果觉得好,就请您
      0%(0)
  • 差
  • 差的评价 如果觉得差,就请您
      0%(0)
更多精彩
    赞助商链接

    热点阅读
      焦点图片
        最新推荐
          精彩阅读