Skip to content

Commit c32a69a

Browse files
committed
Added midPoint and updateMotion calculations
1 parent 0f97be3 commit c32a69a

1 file changed

Lines changed: 40 additions & 60 deletions

File tree

src/input/Pointer.js

Lines changed: 40 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,18 @@ var Pointer = new Class({
119119
* @name Phaser.Input.Pointer#prevPosition
120120
* @type {Phaser.Math.Vector2}
121121
* @since 3.11.0
122+
*/
122123
this.prevPosition = new Vector2();
124+
125+
/**
126+
* An internal vector used for calculations of the pointer speed and angle.
127+
*
128+
* @name Phaser.Input.Pointer#midPoint
129+
* @type {Phaser.Math.Vector2}
130+
* @private
131+
* @since 3.16.0
123132
*/
133+
this.midPoint = new Vector2();
124134

125135
/**
126136
* The current velocity of the Pointer, based on its previous and current position.
@@ -156,13 +166,12 @@ var Pointer = new Class({
156166
*
157167
* The default value of zero means 'no smoothing'.
158168
* Set to a small value, such as 0.2, to apply an average level of smoothing between positions.
159-
* Values above 1 will introduce excess jitter into the positions.
160169
*
161170
* Positions are only smoothed when the pointer moves. Up and Down positions are always precise.
162171
*
163172
* @name Phaser.Input.Pointer#smoothFactor
164173
* @type {number}
165-
* @default 0
174+
* @default 1
166175
* @since 3.16.0
167176
*/
168177
this.smoothFactor = 0;
@@ -424,7 +433,7 @@ var Pointer = new Class({
424433

425434
/**
426435
* Resets the temporal properties of this Pointer.
427-
* Called automatically by the Input Plugin each update.
436+
* This method is called automatically each frame by the Input Manager.
428437
*
429438
* @method Phaser.Input.Pointer#reset
430439
* @private
@@ -442,58 +451,41 @@ var Pointer = new Class({
442451
this.movementY = 0;
443452
},
444453

445-
recordPosition: function (time)
454+
/**
455+
* Calculates the motion of this Pointer, including its velocity and angle of movement.
456+
* This method is called automatically each frame by the Input Manager.
457+
*
458+
* @method Phaser.Input.Pointer#updateMotion
459+
* @private
460+
* @since 3.16.0
461+
*/
462+
updateMotion: function ()
446463
{
447-
var history = this.history;
464+
var px = this.midPoint.x;
465+
var py = this.midPoint.y;
448466

449-
var msSinceLastMove = time - this.moveTime;
467+
var cx = this.position.x;
468+
var cy = this.position.y;
450469

451-
if (msSinceLastMove > 50)
452-
{
453-
// Use acceleration instead of velocity
454-
}
470+
// Moving towards our goal ...
471+
var vx = SmoothStepInterpolation(0.2, px, cx);
472+
var vy = SmoothStepInterpolation(0.2, py, cy);
455473

456-
history.push({ x: this.x, y: this.y });
457-
458-
if (history.length > 1)
474+
if (FuzzyEqual(vx, cx, 0.1))
459475
{
460-
history.unshift();
476+
vx = cx;
461477
}
462478

463-
// Average out the positions to get the delta and angle
464-
var x = 0;
465-
var y = 0;
466-
467-
for (var i = 0; i < history.length; i++)
479+
if (FuzzyEqual(vy, cy, 0.1))
468480
{
469-
x += history[i].x;
470-
y += history[i].y;
481+
vy = cy;
471482
}
472483

473-
this.velocity.x = this.x - (x / history.length);
474-
this.velocity.y = this.y - (y / history.length);
475-
476-
this.moveTime = time;
477-
},
478-
479-
updateMotion: function ()
480-
{
481-
// this.velocity.x = Linear(this.velocity.x, 0, 0.1);
482-
// this.velocity.y = Linear(this.velocity.y, 0, 0.1);
484+
this.midPoint.set(vx, vy);
483485

484-
// Or
485-
this.velocity.x *= 0.9;
486-
this.velocity.y *= 0.9;
486+
this.velocity.set(cx - vx, cy - vy);
487487

488-
if (FuzzyEqual(this.velocity.x, 0, 0.1))
489-
{
490-
this.velocity.x = 0;
491-
}
492-
493-
if (FuzzyEqual(this.velocity.y, 0, 0.1))
494-
{
495-
this.velocity.y = 0;
496-
}
488+
this.angle = Math.atan2(cy - vy, cx - vx);
497489
},
498490

499491
/**
@@ -596,22 +588,6 @@ var Pointer = new Class({
596588
// Sets the local x/y properties
597589
this.manager.transformPointer(this, event.pageX, event.pageY, true);
598590

599-
this.recordPosition(time);
600-
601-
// this.velocity.x = event._deltaX;
602-
// this.velocity.y = event._deltaY;
603-
// this.angle = event._angle;
604-
605-
// var x1 = this.position.x;
606-
// var y1 = this.position.y;
607-
608-
// var x2 = this.prevPosition.x;
609-
// var y2 = this.prevPosition.y;
610-
611-
// this.velocity.x = x1 - x2;
612-
// this.velocity.y = y1 - y2;
613-
// this.angle = Math.atan2(y2 - y1, x2 - x1);
614-
615591
if (this.manager.mouse.locked)
616592
{
617593
// Multiple DOM events may occur within one frame, but only one Phaser event will fire
@@ -621,6 +597,8 @@ var Pointer = new Class({
621597

622598
this.justMoved = true;
623599

600+
this.moveTime = time;
601+
624602
this.dirty = true;
625603

626604
this.wasTouch = false;
@@ -678,7 +656,7 @@ var Pointer = new Class({
678656
* @param {TouchEvent} event - The Touch Event to process.
679657
* @param {integer} time - The current timestamp as generated by the Request Animation Frame or SetTimeout.
680658
*/
681-
touchmove: function (event)
659+
touchmove: function (event, time)
682660
{
683661
this.event = event;
684662

@@ -687,6 +665,8 @@ var Pointer = new Class({
687665

688666
this.justMoved = true;
689667

668+
this.moveTime = time;
669+
690670
this.dirty = true;
691671

692672
this.wasTouch = true;

0 commit comments

Comments
 (0)