-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
Velocity.js
153 lines (133 loc) · 3.96 KB
/
Velocity.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* @author Richard Davey <rich@phaser.io>
* @copyright 2013-2024 Phaser Studio Inc.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var Body = require('../lib/body/Body');
/**
* 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
*/
var Velocity = {
/**
* Sets the horizontal velocity of the physics body.
*
* @method Phaser.Physics.Matter.Components.Velocity#setVelocityX
* @since 3.0.0
*
* @param {number} x - The horizontal velocity value.
*
* @return {this} This Game Object instance.
*/
setVelocityX: function (x)
{
this._tempVec2.set(x, this.body.velocity.y);
Body.setVelocity(this.body, this._tempVec2);
return this;
},
/**
* Sets vertical velocity of the physics body.
*
* @method Phaser.Physics.Matter.Components.Velocity#setVelocityY
* @since 3.0.0
*
* @param {number} y - The vertical velocity value.
*
* @return {this} This Game Object instance.
*/
setVelocityY: function (y)
{
this._tempVec2.set(this.body.velocity.x, y);
Body.setVelocity(this.body, this._tempVec2);
return this;
},
/**
* Sets both the horizontal and vertical velocity of the physics body.
*
* @method Phaser.Physics.Matter.Components.Velocity#setVelocity
* @since 3.0.0
*
* @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.
*
* @return {this} This Game Object instance.
*/
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
*
* @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.
*
* @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);
}
};
module.exports = Velocity;