Skip to content

Commit 51a9b3b

Browse files
committed
Recording positon tests
1 parent 21fa196 commit 51a9b3b

1 file changed

Lines changed: 83 additions & 9 deletions

File tree

src/input/Pointer.js

Lines changed: 83 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
var Class = require('../utils/Class');
88
var Distance = require('../math/distance/DistanceBetween');
9+
var Linear = require('../math/Linear');
10+
var FuzzyEqual = require('../math/fuzzy/Equal');
911
var SmoothStepInterpolation = require('../math/interpolation/SmoothStepInterpolation');
1012
var Vector2 = require('../math/Vector2');
1113

@@ -117,8 +119,8 @@ var Pointer = new Class({
117119
* @name Phaser.Input.Pointer#prevPosition
118120
* @type {Phaser.Math.Vector2}
119121
* @since 3.11.0
120-
*/
121122
this.prevPosition = new Vector2();
123+
*/
122124

123125
/**
124126
* The current velocity of the Pointer, based on its previous and current position.
@@ -185,6 +187,16 @@ var Pointer = new Class({
185187
*/
186188
this.worldY = 0;
187189

190+
/**
191+
* Time when this Pointer was most recently moved (regardless of the state of its buttons, if any)
192+
*
193+
* @name Phaser.Input.Pointer#moveTime
194+
* @type {number}
195+
* @default 0
196+
* @since 3.0.0
197+
*/
198+
this.moveTime = 0;
199+
188200
/**
189201
* X coordinate of the Pointer when Button 1 (left button), or Touch, was pressed, used for dragging objects.
190202
*
@@ -389,6 +401,8 @@ var Pointer = new Class({
389401
* @since 3.10.0
390402
*/
391403
this.active = (id === 0) ? true : false;
404+
405+
this.history = [];
392406
},
393407

394408
/**
@@ -428,6 +442,60 @@ var Pointer = new Class({
428442
this.movementY = 0;
429443
},
430444

445+
recordPosition: function (time)
446+
{
447+
var history = this.history;
448+
449+
var msSinceLastMove = time - this.moveTime;
450+
451+
if (msSinceLastMove > 50)
452+
{
453+
// Use acceleration instead of velocity
454+
}
455+
456+
history.push({ x: this.x, y: this.y });
457+
458+
if (history.length > 1)
459+
{
460+
history.unshift();
461+
}
462+
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++)
468+
{
469+
x += history[i].x;
470+
y += history[i].y;
471+
}
472+
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);
483+
484+
// Or
485+
this.velocity.x *= 0.9;
486+
this.velocity.y *= 0.9;
487+
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+
}
497+
},
498+
431499
/**
432500
* Internal method to handle a Mouse Up Event.
433501
*
@@ -516,7 +584,7 @@ var Pointer = new Class({
516584
* @param {MouseEvent} event - The Mouse Event to process.
517585
* @param {integer} time - The current timestamp as generated by the Request Animation Frame or SetTimeout.
518586
*/
519-
move: function (event)
587+
move: function (event, time)
520588
{
521589
if (event.buttons)
522590
{
@@ -528,15 +596,21 @@ var Pointer = new Class({
528596
// Sets the local x/y properties
529597
this.manager.transformPointer(this, event.pageX, event.pageY, true);
530598

531-
var x1 = this.position.x;
532-
var y1 = this.position.y;
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;
533607

534-
var x2 = this.prevPosition.x;
535-
var y2 = this.prevPosition.y;
608+
// var x2 = this.prevPosition.x;
609+
// var y2 = this.prevPosition.y;
536610

537-
this.velocity.x = x1 - x2;
538-
this.velocity.y = y1 - y2;
539-
this.angle = Math.atan2(y2 - y1, x2 - x1);
611+
// this.velocity.x = x1 - x2;
612+
// this.velocity.y = y1 - y2;
613+
// this.angle = Math.atan2(y2 - y1, x2 - x1);
540614

541615
if (this.manager.mouse.locked)
542616
{

0 commit comments

Comments
 (0)