Advertisement
Guest User

Untitled

a guest
Mar 30th, 2013
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //////////////////////////////////////
  2. // Player item entity               //
  3. //////////////////////////////////////
  4.  
  5. ig.module(
  6.     'game.entities.player'
  7. )
  8. .requires(
  9.     'impact.entity',
  10.     'plugins.box2d.entity'
  11. )
  12. .defines(function(){
  13.  
  14.     EntityPlayer = ig.Box2DEntity.extend({
  15.        
  16.         //box2d handles collisions so turn off impact collision handling
  17.         collides: ig.Entity.COLLIDES.NEVER,
  18.         checkAgainst: ig.Entity.TYPE.NONE,
  19.                
  20.         MAX_STEER_ANGLE : Math.PI/3,
  21.         STEER_SPEED : 3,
  22.         SIDEWAYS_FRICTION_FORCE : 10,
  23.         HORSEPOWERS : 40,
  24.         CAR_STARTING_POS : new b2.Vec2(30,140),
  25.          
  26.         leftRearWheelPosition : new b2.Vec2(-1.1,1.4),
  27.         rightRearWheelPosition : new b2.Vec2(1.1,1.4),
  28.         leftFrontWheelPosition : new b2.Vec2(-1.1,-1),
  29.         rightFrontWheelPosition : new b2.Vec2(1.1,-1),
  30.        
  31.         init: function( x, y, settings ) {
  32.             this.parent( x, y, settings );
  33.            
  34.         },
  35.    
  36.    
  37.         createBody: function(x,y,settings) {
  38.             this.parent(x,y,settings);
  39.            
  40.            
  41.             this.engineSpeed=0;
  42.             this.steeringAngle=0;
  43.            
  44.             // define our body
  45.             var bodyDef = new b2.BodyDef();
  46.             bodyDef.type = b2.dynamicBody;
  47.             bodyDef.linearDamping = 1;
  48.             bodyDef.angularDamping = 1;
  49.             bodyDef.position = this.CAR_STARTING_POS.Copy()
  50.              
  51.             this.body = ig.world.CreateBody(bodyDef);
  52.             this.body.SetMassFromShapes();
  53.              
  54.             var leftWheelDef = new b2.BodyDef();
  55.             leftWheelDef.position = this.CAR_STARTING_POS.Copy();
  56.             leftWheelDef.position.Add(this.leftFrontWheelPosition);
  57.             this.leftWheel = ig.world.CreateBody(leftWheelDef);
  58.              
  59.             var rightWheelDef = new b2.BodyDef();
  60.             rightWheelDef.position = this.CAR_STARTING_POS.Copy();
  61.             rightWheelDef.position.Add(this.rightFrontWheelPosition);
  62.              
  63.             this.rightWheel = ig.world.CreateBody(rightWheelDef);
  64.             var leftRearWheelDef = new b2.BodyDef();
  65.             leftRearWheelDef.position = this.CAR_STARTING_POS.Copy();
  66.             leftRearWheelDef.position.Add(this.leftRearWheelPosition);
  67.             this.leftRearWheel = ig.world.CreateBody(leftRearWheelDef);
  68.              
  69.             var rightRearWheelDef = new b2.BodyDef();
  70.             rightRearWheelDef.position = this.CAR_STARTING_POS.Copy();
  71.             rightRearWheelDef.position.Add(this.rightRearWheelPosition);
  72.             this.rightRearWheel = ig.world.CreateBody(rightRearWheelDef);
  73.            
  74.             // define our shapes
  75.             var shapeDef = new b2.PolygonDef();
  76.             shapeDef.SetAsBox(0.5,1.5);
  77.             shapeDef.density = 1;
  78.             this.body.CreateShape(shapeDef);
  79.              
  80.             //Left Wheel shape
  81.             var leftWheelShapeDef = new b2.PolygonDef();
  82.             leftWheelShapeDef.SetAsBox(0.2,0.5);
  83.             leftWheelShapeDef.density = 1;
  84.             //leftWheelShapeDef.isSensor = true;
  85.             this.leftWheel.CreateShape(leftWheelShapeDef);
  86.              
  87.             //Right Wheel shape
  88.             var rightWheelShapeDef = new b2.PolygonDef();
  89.             rightWheelShapeDef.SetAsBox(0.2,0.5);
  90.             rightWheelShapeDef.density = 1;
  91.             //rightWheelShapeDef.isSensor = true;
  92.             this.rightWheel.CreateShape(rightWheelShapeDef);
  93.              
  94.             //Left Wheel shape
  95.             var leftRearWheelShapeDef = new b2.PolygonDef();
  96.             leftRearWheelShapeDef.SetAsBox(0.2,0.5);
  97.             leftRearWheelShapeDef.density = 1;
  98.             //leftRearWheelShapeDef.isSensor = true;
  99.             this.leftRearWheel.CreateShape(leftRearWheelShapeDef);
  100.              
  101.             //Right Wheel shape
  102.             var rightRearWheelShapeDef = new b2.PolygonDef();
  103.             rightRearWheelShapeDef.SetAsBox(0.2,0.5);
  104.             rightRearWheelShapeDef.density = 1;
  105.             //rightRearWheelShapeDef.isSensor = true;
  106.             this.rightRearWheel.CreateShape(rightRearWheelShapeDef);
  107.              
  108.             this.body.SetMassFromShapes();
  109.             this.leftWheel.SetMassFromShapes();
  110.             this.rightWheel.SetMassFromShapes();
  111.             this.leftRearWheel.SetMassFromShapes();
  112.             this.rightRearWheel.SetMassFromShapes();
  113.            
  114.             // Joints
  115.             var leftJointDef = new b2.RevoluteJointDef();
  116.             leftJointDef.Initialize(this.body, this.leftWheel, this.leftWheel.GetWorldCenter());
  117.             leftJointDef.enableMotor = true;
  118.             leftJointDef.maxMotorTorque = 100;
  119.              
  120.             var rightJointDef = new b2.RevoluteJointDef();
  121.             rightJointDef.Initialize(this.body, this.rightWheel, this.rightWheel.GetWorldCenter());
  122.             rightJointDef.enableMotor = true;
  123.             rightJointDef.maxMotorTorque = 100;
  124.            
  125.             this.leftJoint = ig.world.CreateJoint(leftJointDef);
  126.             this.rightJoint= ig.world.CreateJoint(rightJointDef);
  127.              
  128.             var leftRearJointDef = new b2.PrismaticJointDef();
  129.             leftRearJointDef.Initialize(this.body, this.leftRearWheel, this.leftRearWheel.GetWorldCenter(), new b2.Vec2(1,0));
  130.             leftRearJointDef.enableLimit = true;
  131.             leftRearJointDef.lowerTranslation = leftRearJointDef.upperTranslation = 0;
  132.              
  133.             var rightRearJointDef = new b2.PrismaticJointDef();
  134.             rightRearJointDef.Initialize(this.body, this.rightRearWheel, this.rightRearWheel.GetWorldCenter(), new b2.Vec2(1,0));
  135.             rightRearJointDef.enableLimit = true;
  136.             rightRearJointDef.lowerTranslation = rightRearJointDef.upperTranslation = 0;
  137.              
  138.             ig.world.CreateJoint(leftRearJointDef);
  139.             ig.world.CreateJoint(rightRearJointDef);
  140.             console.log(this);
  141.         },
  142.        
  143.         update: function(){
  144.                 //kill unwanted lateral wheel velocity
  145.                 this.killOrthogonalVelocity(this.leftWheel);
  146.                 this.killOrthogonalVelocity(this.rightWheel);
  147.                 this.killOrthogonalVelocity(this.leftRearWheel);
  148.                 this.killOrthogonalVelocity(this.rightRearWheel);
  149.                
  150.                 //input states
  151.                 if(ig.input.pressed("up")){
  152.                     this.body.WakeUp();
  153.                     this.engineSpeed = -this.HORSEPOWERS;
  154.                     console.log('enginespeed:'+this.engineSpeed);
  155.                 }
  156.                
  157.                 if(ig.input.pressed("down")){
  158.                     this.engineSpeed = this.HORSEPOWERS;
  159.                     console.log('enginespeed:'+this.engineSpeed);
  160.                 }
  161.                
  162.                 if (ig.input.pressed("left") || ig.input.pressed("leftButton")) {
  163.                     this.steeringAngle = -this.MAX_STEER_ANGLE;
  164.                     console.log('steeringangle:'+this.steeringAngle);
  165.                 }
  166.                      
  167.                 if (ig.input.pressed("right") || ig.input.pressed("rightButton")) {
  168.                     this.steeringAngle = +this.MAX_STEER_ANGLE;
  169.                     console.log('steeringangle:'+this.steeringAngle);
  170.                 }
  171.                
  172.                 // Key released
  173.                 if(( ig.input.released('up') ) || (ig.input.released('down'))) {
  174.                     this.engineSpeed = 0;
  175.                     console.log('enginespeed:'+this.engineSpeed);
  176.                 }
  177.                
  178.                 if(( ig.input.released('left') ) || (ig.input.released('right'))) {
  179.                     this.steeringAngle = 0;
  180.                     console.log('steeringangle:'+this.steeringAngle);
  181.                 }
  182.                
  183.                 var ldirection = this.leftWheel.GetXForm().R.col2.Copy();
  184.                 var rdirection = this.rightWheel.GetXForm().R.col2.Copy();
  185.                
  186.                 ldirection.Multiply(this.engineSpeed);
  187.                 rdirection.Multiply(this.engineSpeed);
  188.                
  189.                 this.leftWheel.ApplyForce(ldirection, this.leftWheel.GetPosition());
  190.                 this.rightWheel.ApplyForce(rdirection, this.rightWheel.GetPosition());
  191.                  
  192.                 //Steering
  193.                 var mspeed;
  194.                 mspeed = this.steeringAngle - this.leftJoint.GetJointAngle();
  195.                 this.leftJoint.SetMotorSpeed(mspeed * this.STEER_SPEED);
  196.                 mspeed = this.steeringAngle - this.rightJoint.GetJointAngle();
  197.                 this.rightJoint.SetMotorSpeed(mspeed * this.STEER_SPEED);
  198.            
  199.             //call the parent
  200.             this.parent();
  201.         },
  202.         killOrthogonalVelocity: function(targetBody) {
  203.             var localPoint = new b2.Vec2(0,0);
  204.             //console.log(targetBody);
  205.             var velocity = targetBody.GetLinearVelocityFromLocalPoint(localPoint);
  206.              
  207.             var sidewaysAxis = targetBody.GetXForm().R.col2.Copy();
  208.             sidewaysAxis.Multiply(b2.Math.Dot(velocity,sidewaysAxis));
  209.                 //sidewaysAxis.multiplyN(velocity.b2.Dot(sidewaysAxis));
  210.             targetBody.SetLinearVelocity(sidewaysAxis);
  211.             //targetBody.GetWorldPoint(localPoint);
  212.         },
  213.         draw: function(){
  214.             this.parent();
  215.         }
  216.        
  217.     });
  218.    
  219. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement