JAVASCRIPT   8

Velocity.js

Guest on 31st May 2021 02:27:53 AM

  1. /**
  2.  * @author       Richard Davey <rich@photonstorm.com>
  3.  * @copyright    Photon Storm Ltd.
  4.  * @license      {@link https://opensource.org/licenses/MIT|MIT License}
  5.  */
  6.  
  7. var Body = require('../lib/body/Body');
  8.  
  9. /**
  10.  * Contains methods for changing the velocity of a Matter Body. Should be used as a mixin and not called directly.
  11.  *
  12.  * @namespace Phaser.Physics.Matter.Components.Velocity
  13.  * @since 3.0.0
  14.  */
  15. var Velocity = {
  16.  
  17.     /**
  18.      * Sets the angular velocity of the body instantly.
  19.      * Position, angle, force etc. are unchanged.
  20.      *
  21.      * @method Phaser.Physics.Matter.Components.Velocity#setAngularVelocity
  22.      * @since 3.0.0
  23.      *
  24.      * @param {number} value - The angular velocity.
  25.      *
  26.      * @return {Phaser.GameObjects.GameObject} This Game Object.
  27.      */
  28.     setAngularVelocity: function (value)
  29.     {
  30.         Body.setAngularVelocity(this.body, value);
  31.  
  32.         return this;
  33.     },
  34.  
  35.     /**
  36.      * Sets the horizontal velocity of the physics body.
  37.      *
  38.      * @method Phaser.Physics.Matter.Components.Velocity#setVelocityX
  39.      * @since 3.0.0
  40.      *
  41.      * @param {number} x - The horizontal velocity value.
  42.      *
  43.      * @return {Phaser.GameObjects.GameObject} This Game Object.
  44.      */
  45.     setVelocityX: function (x)
  46.     {
  47.         this._tempVec2.set(x, this.body.velocity.y);
  48.  
  49.         Body.setVelocity(this.body, this._tempVec2);
  50.  
  51.         return this;
  52.     },
  53.  
  54.     /**
  55.      * Sets vertical velocity of the physics body.
  56.      *
  57.      * @method Phaser.Physics.Matter.Components.Velocity#setVelocityY
  58.      * @since 3.0.0
  59.      *
  60.      * @param {number} y - The vertical velocity value.
  61.      *
  62.      * @return {Phaser.GameObjects.GameObject} This Game Object.
  63.      */
  64.     setVelocityY: function (y)
  65.     {
  66.         this._tempVec2.set(this.body.velocity.x, y);
  67.  
  68.         Body.setVelocity(this.body, this._tempVec2);
  69.  
  70.         return this;
  71.     },
  72.  
  73.     /**
  74.      * Sets both the horizontal and vertical velocity of the physics body.
  75.      *
  76.      * @method Phaser.Physics.Matter.Components.Velocity#setVelocity
  77.      * @since 3.0.0
  78.      *
  79.      * @param {number} x - The horizontal velocity value.
  80.      * @param {number} [y=x] - The vertical velocity value, it can be either positive or negative. If not given, it will be the same as the `x` value.
  81.      *
  82.      * @return {Phaser.GameObjects.GameObject} This Game Object.
  83.      */
  84.     setVelocity: function (x, y)
  85.     {
  86.         this._tempVec2.set(x, y);
  87.  
  88.         Body.setVelocity(this.body, this._tempVec2);
  89.  
  90.         return this;
  91.     }
  92.  
  93. };
  94.  
  95. module.exports = Velocity;

Raw Paste


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