JAVASCRIPT   10

Mass.js

Guest on 31st May 2021 02:34:58 AM

  1. /**
  2.  * @author       Richard Davey <rich@photonstorm.com>
  3.  * @copyright    2020 Photon Storm Ltd.
  4.  * @license      {@link https://opensource.org/licenses/MIT|MIT License}
  5.  */
  6.  
  7. var Body = require('../lib/body/Body');
  8. var Vector2 = require('../../../math/Vector2');
  9.  
  10. /**
  11.  * Allows accessing the mass, density, and center of mass of a Matter-enabled Game Object. Should be used as a mixin and not directly.
  12.  *
  13.  * @namespace Phaser.Physics.Matter.Components.Mass
  14.  * @since 3.0.0
  15.  */
  16. var Mass = {
  17.  
  18.     /**
  19.      * Sets the mass of the Game Object's Matter Body.
  20.      *
  21.      * @method Phaser.Physics.Matter.Components.Mass#setMass
  22.      * @since 3.0.0
  23.      *
  24.      * @param {number} value - The new mass of the body.
  25.      *
  26.      * @return {Phaser.GameObjects.GameObject} This Game Object.
  27.      */
  28.     setMass: function (value)
  29.     {
  30.         Body.setMass(this.body, value);
  31.  
  32.         return this;
  33.     },
  34.  
  35.     /**
  36.      * Sets density of the body.
  37.      *
  38.      * @method Phaser.Physics.Matter.Components.Mass#setDensity
  39.      * @since 3.0.0
  40.      *
  41.      * @param {number} value - The new density of the body.
  42.      *
  43.      * @return {Phaser.GameObjects.GameObject} This Game Object.
  44.      */
  45.     setDensity: function (value)
  46.     {
  47.         Body.setDensity(this.body, value);
  48.  
  49.         return this;
  50.     },
  51.  
  52.     /**
  53.      * The body's center of mass.
  54.      *
  55.      * Calling this creates a new `Vector2 each time to avoid mutation.
  56.      *
  57.      * If you only need to read the value and won't change it, you can get it from `GameObject.body.centerOfMass`.
  58.      *
  59.      * @name Phaser.Physics.Matter.Components.Mass#centerOfMass
  60.      * @type {Phaser.Math.Vector2}
  61.      * @readonly
  62.      * @since 3.10.0
  63.      *
  64.      * @return {Phaser.Math.Vector2} The center of mass.
  65.      */
  66.     centerOfMass: {
  67.  
  68.         get: function ()
  69.         {
  70.             return new Vector2(this.body.centerOfMass.x, this.body.centerOfMass.y);
  71.         }
  72.     }
  73.  
  74. };
  75.  
  76. module.exports = Mass;

Raw Paste


Login or Register to edit or fork this paste. It's free.