-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
Force.js
150 lines (124 loc) · 4.16 KB
/
Force.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
/**
* @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');
/**
* A component to apply force to Matter.js bodies.
*
* @namespace Phaser.Physics.Matter.Components.Force
* @since 3.0.0
*/
var Force = {
// force = vec2 / point
/**
* Applies a force to a body.
*
* @method Phaser.Physics.Matter.Components.Force#applyForce
* @since 3.0.0
*
* @param {Phaser.Math.Vector2} force - A Vector that specifies the force to apply.
*
* @return {this} This Game Object instance.
*/
applyForce: function (force)
{
this._tempVec2.set(this.body.position.x, this.body.position.y);
Body.applyForce(this.body, this._tempVec2, force);
return this;
},
/**
* Applies a force to a body from a given position.
*
* @method Phaser.Physics.Matter.Components.Force#applyForceFrom
* @since 3.0.0
*
* @param {Phaser.Math.Vector2} position - The position in which the force comes from.
* @param {Phaser.Math.Vector2} force - A Vector that specifies the force to apply.
*
* @return {this} This Game Object instance.
*/
applyForceFrom: function (position, force)
{
Body.applyForce(this.body, position, force);
return this;
},
/**
* Apply thrust to the forward position of the body.
*
* Use very small values, such as 0.1, depending on the mass and required speed.
*
* @method Phaser.Physics.Matter.Components.Force#thrust
* @since 3.0.0
*
* @param {number} speed - A speed value to be applied to a directional force.
*
* @return {this} This Game Object instance.
*/
thrust: function (speed)
{
var angle = this.body.angle;
this._tempVec2.set(speed * Math.cos(angle), speed * Math.sin(angle));
Body.applyForce(this.body, { x: this.body.position.x, y: this.body.position.y }, this._tempVec2);
return this;
},
/**
* Apply thrust to the left position of the body.
*
* Use very small values, such as 0.1, depending on the mass and required speed.
*
* @method Phaser.Physics.Matter.Components.Force#thrustLeft
* @since 3.0.0
*
* @param {number} speed - A speed value to be applied to a directional force.
*
* @return {this} This Game Object instance.
*/
thrustLeft: function (speed)
{
var angle = this.body.angle - Math.PI / 2;
this._tempVec2.set(speed * Math.cos(angle), speed * Math.sin(angle));
Body.applyForce(this.body, { x: this.body.position.x, y: this.body.position.y }, this._tempVec2);
return this;
},
/**
* Apply thrust to the right position of the body.
*
* Use very small values, such as 0.1, depending on the mass and required speed.
*
* @method Phaser.Physics.Matter.Components.Force#thrustRight
* @since 3.0.0
*
* @param {number} speed - A speed value to be applied to a directional force.
*
* @return {this} This Game Object instance.
*/
thrustRight: function (speed)
{
var angle = this.body.angle + Math.PI / 2;
this._tempVec2.set(speed * Math.cos(angle), speed * Math.sin(angle));
Body.applyForce(this.body, { x: this.body.position.x, y: this.body.position.y }, this._tempVec2);
return this;
},
/**
* Apply thrust to the back position of the body.
*
* Use very small values, such as 0.1, depending on the mass and required speed.
*
* @method Phaser.Physics.Matter.Components.Force#thrustBack
* @since 3.0.0
*
* @param {number} speed - A speed value to be applied to a directional force.
*
* @return {this} This Game Object instance.
*/
thrustBack: function (speed)
{
var angle = this.body.angle - Math.PI;
this._tempVec2.set(speed * Math.cos(angle), speed * Math.sin(angle));
Body.applyForce(this.body, { x: this.body.position.x, y: this.body.position.y }, this._tempVec2);
return this;
}
};
module.exports = Force;