Advertisement
Guest User

Phaser P2 physics test

a guest
Jul 25th, 2014
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var ns = ns || {};
  2.  
  3. (function(ns){
  4.  
  5.     // ref to the engine's keyboard cursors
  6.     var cursors;
  7.  
  8.     // world info
  9.     var worldInfo = {
  10.         'w': 10000,
  11.         'h': 5980,
  12.         'friction': 0.3,
  13.         'restitution': 0.5,
  14.         'stiffness': 1e7,
  15.         'relaxation': 5,
  16.         'frictionStiffness': 1e7,
  17.         'frictionRelaxation': 5,
  18.         'surfaceVelocity': 0.0,
  19.         'material': null
  20.     };
  21.     // car reference
  22.     var raceCar;
  23.  
  24.     /**
  25.      * RaceCar class
  26.      * @param x:int             init x position of the car
  27.      * @param y:int             init y position of the car
  28.      * @param worldInfo:object  world info object
  29.      */
  30.     var RaceCar = function(x, y, worldInfo)
  31.     {
  32.         // debug sprites
  33.         this.debug = false;
  34.         this.damping = 0.5;
  35.         this.angularDamping = 0.5;
  36.         // car material ref
  37.         this.material = null;
  38.         // contact material ref
  39.         this.contactMaterial = null;
  40.         // world info ref
  41.         this.worldInfo = worldInfo;
  42.         // car sprite ref
  43.         this.car = null;
  44.         // tire sprites ref
  45.         this.tires = {
  46.             'left': null,
  47.             'right': null
  48.         };
  49.         // sprite ids for car parts
  50.         this.spriteIds = {
  51.             'car': 'car',
  52.             'tire': 'tire'
  53.         };
  54.         // tire offets (relative to the car's center of mass offsets used for positioning the tires and for constrait points)
  55.         // mind that the 'x' get sum/subs with the car.width/2, so that the x be dynamic
  56.         // so the real x offset will be offsetRealX = car.width/2 + x
  57.         this.tireOffsets = {
  58.             'left': { x:2, y:-16 },
  59.             'right': { x:-2, y:-16 }
  60.         };
  61.  
  62.         // create sprites
  63.         this.createSprites(x, y);
  64.         // enable physics
  65.         this.enablePhysics();
  66.         // create materials
  67.         this.createMaterials();
  68.     };
  69.     /**
  70.      * Creates car part sprites (should be call before other methods)
  71.      * @param x:int     init x position of the car
  72.      * @param y:int  init y position of the car
  73.      */
  74.     RaceCar.prototype.createSprites = function(x, y)
  75.     {
  76.         // create sprites
  77.         this.car = game.add.sprite(x, y, this.spriteIds['car'], 0);
  78.         this.tires.left = game.add.sprite(x - this.car.width/2 + this.tireOffsets.left.x, y + this.tireOffsets.left.y, this.spriteIds['tire']);
  79.         this.tires.right = game.add.sprite(x + this.car.width/2 + this.tireOffsets.right.x, y + this.tireOffsets.right.y, this.spriteIds['tire']);
  80.         this.car.bringToTop();
  81.     };
  82.     /**
  83.      * Enables physics for the car (should be called after @see this.createSprites())
  84.      */
  85.     RaceCar.prototype.enablePhysics = function()
  86.     {
  87.         // enable physics
  88.         game.physics.p2.enable(this.car, this.debug);
  89.         game.physics.p2.enable(this.tires.left, this.debug);
  90.         game.physics.p2.enable(this.tires.right, this.debug);      
  91.         // revolutes
  92.         //
  93.         // @TODO now this is the bug place
  94.         // when we create revolutes there are 2 problems need to be solved:
  95.         // - tires are wrong positioned (even thought the force is 200 000)
  96.         // - car starts rotating despite that the tires are positioned symmetrically relative to the car's center of mass which is 50% 50% by default
  97.         //
  98.         game.physics.p2.createRevoluteConstraint(this.tires.left, [0,0], this.car, [-this.car.width/2 + this.tireOffsets.left.x, this.tireOffsets.left.y], 20000);
  99.         game.physics.p2.createRevoluteConstraint(this.tires.right, [0,0], this.car, [this.car.width/2 + this.tireOffsets.right.x, this.tireOffsets.right.y], 20000);
  100.         // set damping
  101.         this.tires.left.body.damping = this.damping;
  102.         this.tires.right.body.damping = this.damping;
  103.         this.car.body.damping = this.damping;
  104.         // set angularDamping;
  105.         this.tires.left.body.angularDamping = this.angularDamping;
  106.         this.tires.right.body.angularDamping = this.angularDamping;
  107.         this.car.body.angularDamping = this.angularDamping;
  108.         // allow sleep     
  109.         this.tires.left.body.allowSleep = true;
  110.         this.tires.right.body.allowSleep = true;
  111.         this.car.body.allowSleep = true;
  112.         // collide with world
  113.         this.tires.left.body.collideWorldBounds = true;
  114.         this.tires.right.body.collideWorldBounds = true;
  115.         this.car.body.collideWorldBounds = true;
  116.         // revolutes seem work only if the tires' mass is significantly less than the car's
  117.         this.tires.left.body.mass = 1;  // .01 seems to work, kind of, still buggy coz tires rotate like 1 degree why?!
  118.         this.tires.right.body.mass = 1; // .01 seems to work, kind of, still buggy coz tires rotate like 1 degree why?!
  119.         this.car.body.mass = 1;
  120.     };
  121.     /**
  122.      * Creates the car material + the contact material with the world (should be called after @see this.enablePhysics())
  123.      */
  124.     RaceCar.prototype.createMaterials = function()
  125.     {
  126.         this.material = game.physics.p2.createMaterial('carMaterial');
  127.         // assign material
  128.         this.car.body.setMaterial(this.material);
  129.         this.tires.left.body.setMaterial(this.material);
  130.         this.tires.right.body.setMaterial(this.material);
  131.         // create contactMaterial
  132.         this.contactMaterial = game.physics.p2.createContactMaterial(this.material, this.worldInfo['material']);
  133.         this.contactMaterial.friction = this.worldInfo['friction'];                     // Friction to use in the contact of these two materials.
  134.         this.contactMaterial.restitution = this.worldInfo['restitution'];               // Restitution (i.e. how bouncy it is!) to use in the contact of these two materials.
  135.         this.contactMaterial.stiffness = this.worldInfo['stiffness'];                   // упругость - Stiffness of the resulting ContactEquation that this ContactMaterial generate.
  136.         this.contactMaterial.relaxation = this.worldInfo['relaxation'];                 // Relaxation of the resulting ContactEquation that this ContactMaterial generate.
  137.         this.contactMaterial.frictionStiffness = this.worldInfo['frictionStiffness'];   // Stiffness of the resulting FrictionEquation that this ContactMaterial generate.
  138.         this.contactMaterial.frictionRelaxation = this.worldInfo['frictionRelaxation']; // Relaxation of the resulting FrictionEquation that this ContactMaterial generate.
  139.         this.contactMaterial.surfaceVelocity = this.worldInfo['surfaceVelocity'];       // Will add surface velocity to this material. If bodyA rests on top if bodyB, and the surface velocity is positive, bodyA will slide to the right.
  140.     };
  141.  
  142.     var preload = function()
  143.     {
  144.         game.load.image('car', 'assets/i/car.png');
  145.         game.load.image('tire', 'assets/i/tire.png');
  146.         game.load.image('map', 'assets/i/map.jpg');
  147.     };
  148.     var create = function()
  149.     {
  150.         // resize our game world to be
  151.         game.world.setBounds(0, 0, worldInfo.w, worldInfo.h);
  152.         game.stage.backgroundColor = '#ffffff';
  153.         game.stage.disableVisibilityChange = true;
  154.  
  155.         // physics
  156.         game.physics.startSystem(Phaser.Physics.P2JS);
  157.         game.physics.p2.setBounds(0, 0, worldInfo.w, worldInfo.h, true, true, true, true, false);
  158.         game.physics.p2.friction = worldInfo['friction'];
  159.         game.physics.p2.restitution = worldInfo['restitution'];
  160.         game.physics.p2.applyDamping = true;
  161.  
  162.         // world material
  163.         worldInfo['material'] = game.physics.p2.createMaterial('worldMaterial');
  164.         game.physics.p2.setWorldMaterial(worldInfo['material'], true, true, true, true);
  165.  
  166.         // create a race car
  167.         raceCar = new RaceCar(200, 200, worldInfo);
  168.  
  169.         // camera follow car
  170.         game.camera.follow(raceCar.car, Phaser.Camera.FOLLOW_LOCKON);
  171.  
  172.         // input controls
  173.         cursors = game.input.keyboard.createCursorKeys();
  174.     };
  175.     var update = function()
  176.     {
  177.         // do nothing for now
  178.     };
  179.     var render = function()
  180.     {
  181.         // do nothing for now
  182.     };
  183.  
  184.     // start the game
  185.     var game = new Phaser.Game(800, 600, Phaser.AUTO, 'game', { preload: preload, create: create, update: update, render: render });
  186. })(ns);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement