Skip to content

Commit 19e6091

Browse files
committed
Math.rotateToAngle takes two angles (in radians), and an interpolation value, and returns a new angle, based on the shortest rotational distance between the two.
1 parent 8d4a72c commit 19e6091

3 files changed

Lines changed: 53 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ You can read all about the philosophy behind Lazer [here](http://phaser.io/news/
314314
* Group.getAll will return all children in the Group, or a section of the Group, with the optional ability to test if the child has a property matching the given value or not.
315315
* Group.iterate has a new `returnType`: `RETURN_ALL`. This allows you to return all children that pass the iteration test in an array.
316316
* The property `checkCollision.none` in the ArcadePhysics.Body class was available, but never used internally. It is now used and checked by the `separate` method. By setting `checkCollision.none = true` you can disable all collision and overlap checks on a Body, but still retain its motion updates (thanks @samme #2661)
317+
* Math.rotateToAngle takes two angles (in radians), and an interpolation value, and returns a new angle, based on the shortest rotational distance between the two.
317318

318319
### Updates
319320

src/math/Math.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,57 @@ Phaser.Math = {
320320

321321
},
322322

323+
/**
324+
* Rotates currentAngle towards targetAngle, taking the shortest rotation distance.
325+
* The lerp argument is the amount to rotate by in this call.
326+
*
327+
* @method Phaser.Math#rotateToAngle
328+
* @param {number} currentAngle - The current angle, in radians.
329+
* @param {number} targetAngle - The target angle to rotate to, in radians.
330+
* @param {number} [lerp=0.05] - The lerp value to add to the current angle.
331+
* @return {number} The adjusted angle.
332+
*/
333+
rotateToAngle: function (currentAngle, targetAngle, lerp) {
334+
335+
if (lerp === undefined) { lerp = 0.05; }
336+
337+
if (currentAngle === targetAngle)
338+
{
339+
return currentAngle;
340+
}
341+
342+
if (Math.abs(targetAngle - currentAngle) <= lerp || Math.abs(targetAngle - currentAngle) >= (Phaser.Math.PI2 - lerp))
343+
{
344+
currentAngle = targetAngle;
345+
}
346+
else
347+
{
348+
if (Math.abs(targetAngle - currentAngle) > Math.PI)
349+
{
350+
if (targetAngle < currentAngle)
351+
{
352+
targetAngle += Phaser.Math.PI2;
353+
}
354+
else
355+
{
356+
targetAngle -= Phaser.Math.PI2;
357+
}
358+
}
359+
360+
if (targetAngle > currentAngle)
361+
{
362+
currentAngle += lerp;
363+
}
364+
else if (targetAngle < currentAngle)
365+
{
366+
currentAngle -= lerp;
367+
}
368+
}
369+
370+
return currentAngle;
371+
372+
},
373+
323374
/**
324375
* Find the angle of a segment from (x1, y1) -> (x2, y2).
325376
*

typescript/phaser.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2600,6 +2600,7 @@ declare module Phaser {
26002600
static PI2: number;
26012601
static radToDeg(radians: number): number;
26022602
static reverseAngle(angleRed: number): number;
2603+
static rotateToAngle(currentAngle: number, targetAngle: number, lerp?: number): number;
26032604
static roundAwayFromZero(value: number): number;
26042605
static roundTo(value: number, place?: number, base?: number): number;
26052606
static shear(n: number): number;

0 commit comments

Comments
 (0)