Skip to content

Commit 7c00bd4

Browse files
committed
Added Pointer.velocity and Pointer.angle as they're so common for gesture calculations.
1 parent f1fdc5d commit 7c00bd4

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
* `Pointer.smoothFactor` is a float-value that allows you to automatically apply smoothing to the Pointer position as it moves. This is ideal when you want something smoothly tracking a pointer in a game, or are need a smooth drawing motion for an art package. The default value is zero, meaning disabled. Set to a small number, such as 0.2, to enable.
2525
* `Config.inputSmoothFactor` is a new property that allows you to set the smoothing factor for all Pointers the game creators. The default value is zero, which is disabled. Set in the game config as `input: { smoothFactor: value }`.
2626
* `InputManager.transformPointer` has a new boolean argument `wasMove`, which controls if the pointer is being transformed after a move or up/down event.
27+
* `Pointer.velocity` is a new Vector2 that contains the velocity of the Pointer, based on the previous and current position. This is updated whenever the Pointer moves, regardless of button states. If you find the velocity is too erratic, consider enabling the `smoothFactor`.
28+
* `Pointer.angle` is a new property that contains the angle of the Pointer, in radians, based on the previous and current position. This is updated whenever the Pointer moves, regardless of button states. If you find the angle is too erratic, consider enabling the `smoothFactor`.
2729

2830
### New Features
2931

src/input/Pointer.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,32 @@ var Pointer = new Class({
120120
*/
121121
this.prevPosition = new Vector2();
122122

123+
/**
124+
* The current velocity of the Pointer, based on its previous and current position.
125+
*
126+
* This is updated whenever the Pointer moves, regardless of the state of any Pointer buttons.
127+
*
128+
* If you are finding the velocity value too erratic, then consider enabling the `Pointer.smoothFactor`.
129+
*
130+
* @name Phaser.Input.Pointer#velocity
131+
* @type {Phaser.Math.Vector2}
132+
* @since 3.16.0
133+
*/
134+
this.velocity = new Vector2();
135+
136+
/**
137+
* The current angle the Pointer is moving, in radians, based on its previous and current position.
138+
*
139+
* This is updated whenever the Pointer moves, regardless of the state of any Pointer buttons.
140+
*
141+
* If you are finding the angle value too erratic, then consider enabling the `Pointer.smoothFactor`.
142+
*
143+
* @name Phaser.Input.Pointer#angle
144+
* @type {number}
145+
* @since 3.16.0
146+
*/
147+
this.angle = new Vector2();
148+
123149
/**
124150
* The smoothing factor to apply to the Pointer position.
125151
*
@@ -502,6 +528,16 @@ var Pointer = new Class({
502528
// Sets the local x/y properties
503529
this.manager.transformPointer(this, event.pageX, event.pageY, true);
504530

531+
var x1 = this.position.x;
532+
var y1 = this.position.y;
533+
534+
var x2 = this.prevPosition.x;
535+
var y2 = this.prevPosition.y;
536+
537+
this.velocity.x = x1 - x2;
538+
this.velocity.y = y1 - y2;
539+
this.angle = Math.atan2(y2 - y1, x2 - x1);
540+
505541
if (this.manager.mouse.locked)
506542
{
507543
// Multiple DOM events may occur within one frame, but only one Phaser event will fire

0 commit comments

Comments
 (0)