phaser/src/physics/matter-js/components/Velocity.js

154 lines
4 KiB
JavaScript
Raw Normal View History

2018-02-12 16:01:20 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
2023-01-02 17:36:27 +00:00
* @copyright 2013-2023 Photon Storm Ltd.
2019-05-10 15:15:04 +00:00
* @license {@link https://opensource.org/licenses/MIT|MIT License}
2018-02-12 16:01:20 +00:00
*/
2017-11-21 16:50:56 +00:00
var Body = require('../lib/body/Body');
/**
2020-01-04 12:27:04 +00:00
* Contains methods for changing the velocity of a Matter Body. Should be used as a mixin and not called directly.
*
* @namespace Phaser.Physics.Matter.Components.Velocity
* @since 3.0.0
*/
2017-11-21 16:50:56 +00:00
var Velocity = {
/**
2018-10-19 11:32:43 +00:00
* Sets the horizontal velocity of the physics body.
*
* @method Phaser.Physics.Matter.Components.Velocity#setVelocityX
* @since 3.0.0
*
2018-10-19 11:32:43 +00:00
* @param {number} x - The horizontal velocity value.
*
2022-11-03 16:03:51 +00:00
* @return {this} This Game Object instance.
*/
2017-11-21 16:50:56 +00:00
setVelocityX: function (x)
{
this._tempVec2.set(x, this.body.velocity.y);
Body.setVelocity(this.body, this._tempVec2);
return this;
},
/**
2018-10-19 11:32:43 +00:00
* Sets vertical velocity of the physics body.
*
* @method Phaser.Physics.Matter.Components.Velocity#setVelocityY
* @since 3.0.0
*
2018-10-19 11:32:43 +00:00
* @param {number} y - The vertical velocity value.
*
2022-11-03 16:03:51 +00:00
* @return {this} This Game Object instance.
*/
2017-11-21 16:50:56 +00:00
setVelocityY: function (y)
{
this._tempVec2.set(this.body.velocity.x, y);
Body.setVelocity(this.body, this._tempVec2);
return this;
},
/**
2018-10-19 11:32:43 +00:00
* Sets both the horizontal and vertical velocity of the physics body.
*
* @method Phaser.Physics.Matter.Components.Velocity#setVelocity
* @since 3.0.0
*
2018-10-19 11:32:43 +00:00
* @param {number} x - The horizontal velocity value.
* @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.
*
2022-11-03 16:03:51 +00:00
* @return {this} This Game Object instance.
*/
2017-11-21 16:50:56 +00:00
setVelocity: function (x, y)
{
this._tempVec2.set(x, y);
Body.setVelocity(this.body, this._tempVec2);
return this;
},
/**
* Gets the current linear velocity of the physics body.
*
* @method Phaser.Physics.Matter.Components.Velocity#getVelocity
* @since 3.60.0
*
2023-04-04 16:55:56 +00:00
* @return {Phaser.Types.Math.Vector2Like} The current linear velocity of the body.
*/
getVelocity: function ()
{
return Body.getVelocity(this.body);
},
/**
* Sets the angular velocity of the body instantly.
* Position, angle, force etc. are unchanged.
*
* @method Phaser.Physics.Matter.Components.Velocity#setAngularVelocity
* @since 3.0.0
*
* @param {number} velocity - The angular velocity.
*
* @return {this} This Game Object instance.
*/
setAngularVelocity: function (velocity)
{
Body.setAngularVelocity(this.body, velocity);
return this;
},
/**
* Gets the current rotational velocity of the body.
*
* @method Phaser.Physics.Matter.Components.Velocity#getAngularVelocity
* @since 3.60.0
*
* @return {number} The current angular velocity of the body.
*/
getAngularVelocity: function ()
{
return Body.getAngularVelocity(this.body);
},
/**
* Sets the current rotational speed of the body.
* Direction is maintained. Affects body angular velocity.
*
* @method Phaser.Physics.Matter.Components.Velocity#setAngularSpeed
* @since 3.60.0
*
* @param {number} speed - The angular speed.
*
* @return {this} This Game Object instance.
*/
setAngularSpeed: function (speed)
{
Body.setAngularSpeed(this.body, speed);
return this;
},
/**
* Gets the current rotational speed of the body.
* Equivalent to the magnitude of its angular velocity.
*
2023-04-04 16:55:56 +00:00
* @method Phaser.Physics.Matter.Components.Velocity#getAngularSpeed
* @since 3.60.0
*
* @return {number} The current angular velocity of the body.
*/
getAngularSpeed: function ()
{
return Body.getAngularSpeed(this.body);
2017-11-21 16:50:56 +00:00
}
};
module.exports = Velocity;