Skip to content

Commit cf2095f

Browse files
committed
The Arcade Physics Body has a new property maxSpeed which limits the vector length of the Body velocity. You can set it via the method setMaxSpeed and it is applied in the World.computeVelocity method
1 parent 60fbd95 commit cf2095f

3 files changed

Lines changed: 37 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ one set of bindings ever created, which makes things a lot cleaner.
270270
* `Vector3.BACK` is a new constant that can be used in Vector comparison operations (thanks @Aedalus)
271271
* `Vector3.ONE` is a new constant that can be used in Vector comparison operations (thanks @Aedalus)
272272
* Geometery Mask has a new property called `invertAlpha` in WebGL, which works in the same way as the flag on the Bitmap Mask and allows you to invert the function of the stencil buffer, i.e. non-drawn shapes become invisible, and drawn shapes visible (thanks @tfelix)
273+
* The Arcade Physics Body has a new property `maxSpeed` which limits the vector length of the Body velocity. You can set it via the method `setMaxSpeed` and it is applied in the `World.computeVelocity` method (thanks @Edwin222 @rexrainbow)
273274

274275
### Updates
275276

src/physics/arcade/Body.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,19 @@ var Body = new Class({
438438
*/
439439
this.maxVelocity = new Vector2(10000, 10000);
440440

441+
/**
442+
* The maximum speed this Body is allowed to reach.
443+
*
444+
* If not negative it limits the scalar value of speed.
445+
*
446+
* Any negative value means no maximum is being applied.
447+
*
448+
* @name Phaser.Physics.Arcade.Body#maxSpeed
449+
* @type {number}
450+
* @since 3.16.0
451+
*/
452+
this.maxSpeed = -1;
453+
441454
/**
442455
* If this Body is `immovable` and in motion, `friction` is the proportion of this Body's motion received by the riding Body on each axis, relative to 1.
443456
* The default value (1, 0) moves the riding Body horizontally in equal proportion to this Body and vertically not at all.
@@ -1554,6 +1567,23 @@ var Body = new Class({
15541567
return this;
15551568
},
15561569

1570+
/**
1571+
* Sets the maximum speed the Body can move.
1572+
*
1573+
* @method Phaser.Physics.Arcade.Body#setMaxSpeed
1574+
* @since 3.16.0
1575+
*
1576+
* @param {number} value - The maximum speed value, in pixels per second. Set to a negative value to disable.
1577+
*
1578+
* @return {Phaser.Physics.Arcade.Body} This Body object.
1579+
*/
1580+
setMaxSpeed: function (value)
1581+
{
1582+
this.maxSpeed = value;
1583+
1584+
return this;
1585+
},
1586+
15571587
/**
15581588
* Sets the Body's bounce.
15591589
*

src/physics/arcade/World.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,6 +1287,7 @@ var World = new Class({
12871287
var maxY = body.maxVelocity.y;
12881288

12891289
var speed = body.speed;
1290+
var maxSpeed = body.maxSpeed;
12901291
var allowDrag = body.allowDrag;
12911292
var useDamping = body.useDamping;
12921293

@@ -1372,6 +1373,11 @@ var World = new Class({
13721373
velocityY = Clamp(velocityY, -maxY, maxY);
13731374

13741375
body.velocity.set(velocityX, velocityY);
1376+
1377+
if (maxSpeed > -1 && body.velocity.length() > maxSpeed)
1378+
{
1379+
body.velocity.normalize().scale(maxSpeed);
1380+
}
13751381
},
13761382

13771383
/**

0 commit comments

Comments
 (0)