Skip to content

Commit cced09b

Browse files
committed
Added Pointer.prevPosition and getInterpolatedPosition method
1 parent 1a7d726 commit cced09b

3 files changed

Lines changed: 87 additions & 10 deletions

File tree

CHANGELOG.md

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,24 @@ There is a third game config property called `pixelArt`. If set to `true` it's t
4545

4646
### Texture Tint Pipeline - New Features, Updates and Fixes
4747

48-
The Texture Tint Pipeline has been rewritten to tidy up hundreds of lines of duplicate code and to move the responsibility of drawing to the Game Objects themselves. Previously, had you excluded say Tilemaps from your build of Phaser, the renderer would still include masses of code dealing with the drawing of them. Now, this task has been moved to the Game Objects and the pipeline just provides a set of clean utility functions for batching, flushing and drawing.
48+
The Texture Tint Pipeline has been rewritten to tidy up hundreds of lines of duplicate code and to move the responsibility of drawing to the Game Objects themselves. Previously, had you excluded say Tilemaps from your build of Phaser, the renderer would still include masses of code dealing with the drawing of them. This task has been moved to the Game Objects and the pipeline just provides a set of clean utility functions for batching, flushing and drawing.
49+
50+
The decision to make this change was not taken lightly. However, I felt that none of the pipelines actually lived up to their name. You could never actually pass objects through one pipeline to another as they didn't have entry and exit points and were instead just glorified singular batches. Although you could change the pipeline being used on a Game Object this action meant that every pipeline had to be responsible for every single type of Game Object, both now and in the future, and they were full of redundant stub functions as a result. The payload size was also considerable.
4951

5052
* You can now set the WebGL batch size in the Game Config via the property `batchSize`. The default is 2000 before the batch will flush, which is a happy average between desktop and mobile. If targeting desktop specifically, you may wish to increase this value to reduce draw calls.
5153
* There is a new method `batchVertices` which will add a vertices block to the current batch. This is now used internally by nearly all render functions.
52-
* All of the rendering functions now use the `TransformMatrix` class far more than before. This allows the matrix operations to be run-time compiled and cut down on masses of code.
53-
* The `batchTileSprite` method has been removed from the `TextureTintPipeline` class, because it is now handled in the TileSprite WebGL Render function.
54-
* The `drawStaticTilemapLayer` method has been removed from the `TextureTintPipeline` class, because it is now handled in the Static Tilemap Layer WebGL Render function.
55-
* The `batchText` method has been removed from the `TextureTintPipeline` class, because it is now handled in the Static Text WebGL Render function.
56-
* The `batchDynamicTilemapLayer` method has been removed from the `TextureTintPipeline` class, because it is now handled in the Dynamic Tilemap Layer WebGL Render function.
57-
* The `batchMesh` method has been removed from the `TextureTintPipeline` class, because it is now handled in the Mesh WebGL Render function.
58-
* The `batchBitmapText` method has been removed from the `TextureTintPipeline` class, because it is now handled in the BitmapText WebGL Render function.
59-
* The `batchDynamicBitmapText` method has been removed from the `TextureTintPipeline` class, because it is now handled in the DynamicBitmapText WebGL Render function.
60-
* The `batchBlitter` method has been removed from the `TextureTintPipeline` class, because it is now handled in the Blitter WebGL Render function.
6154
* The shader has a new attribute: `tintEffect`. This is a single FLOAT.
6255
* The vertex size has increased by 1 FLOAT to account for the extra shader attribute.
56+
* All of the rendering functions now use the `TransformMatrix` class far more than before. This allows the matrix operations to be run-time compiled and cut down on masses of code.
57+
* The `drawTexture` method has been removed. It has been replaced by `drawTextureFrame` which has a new and more concise signature. See the API docs for details.
58+
* The `batchTileSprite` method has been removed. It is now handled in the TileSprite WebGL Render function.
59+
* The `drawStaticTilemapLayer` method has been removed. It is now handled in the Static Tilemap Layer WebGL Render function.
60+
* The `batchText` method has been removed. It is now handled in the Static Text WebGL Render function.
61+
* The `batchDynamicTilemapLayer` method has been removed. It is now handled in the Dynamic Tilemap Layer WebGL Render function.
62+
* The `batchMesh` method has been removed. It is now handled in the Mesh WebGL Render function.
63+
* The `batchBitmapText` method has been removed. It is now handled in the BitmapText WebGL Render function.
64+
* The `batchDynamicBitmapText` method has been removed. It is now handled in the DynamicBitmapText WebGL Render function.
65+
* The `batchBlitter` method has been removed. It is now handled in the Blitter WebGL Render function.
6366

6467
Due to the changes in the Texture Tint Pipeline the `Textures.Frame` class has also been updated. The following changes concern the Frame UV data:
6568

@@ -117,6 +120,8 @@ There is a new Game Object Component called `TextureCrop`. It replaces the Textu
117120
* `TransformMatrix.copyFrom` is a new method that will copy the given matrix into the values of the current one.
118121
* `TransformMatrix.multiplyWithOffset` is a new method that will multiply the given matrix with the current one, factoring in an additional offset to the results. This is used internally by the renderer code in various places.
119122
* `Rectangle.Intersection` will take two Rectangle objects and return the area of intersection between them. If there is no intersection, an empty Rectangle is returned.
123+
* `Pointer.prevPosition` is a new Vector2 that stores the previous position of the Pointer, prior to the most recent DOM event. You can use this when performing calculations between the old and current positions, such as for tracking the pointer speed.
124+
* `Pointer.getInterpolatedPosition` is a new method that will return an array of smoothly interpolated values between the old and previous position of the Pointer. You can configure how many interpolation steps should take place (the default is 10) and provide an output array to store them in. This method is handy if you've got an object tracking a pointer and you want to ensure it has smooth movement (as the DOM will often process pointer events at a faster rate than the game loop can update).
120125

121126
### Updates
122127

src/input/InputManager.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,6 +1250,11 @@ var InputManager = new Class({
12501250
*/
12511251
transformPointer: function (pointer, pageX, pageY)
12521252
{
1253+
// Store the previous position
1254+
pointer.prevPosition.x = pointer.x;
1255+
pointer.prevPosition.y = pointer.y;
1256+
1257+
// Set the new position
12531258
pointer.x = (pageX - this.bounds.left) * this.scale.x;
12541259
pointer.y = (pageY - this.bounds.top) * this.scale.y;
12551260
},

src/input/Pointer.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
*/
66

77
var Class = require('../utils/Class');
8+
var SmoothStepInterpolation = require('../math/interpolation/SmoothStepInterpolation');
89
var Vector2 = require('../math/Vector2');
910

11+
1012
/**
1113
* @classdesc
1214
* A Pointer object encapsulates both mouse and touch input within Phaser.
@@ -104,6 +106,20 @@ var Pointer = new Class({
104106
*/
105107
this.position = new Vector2();
106108

109+
/**
110+
* The previous position of the Pointer in screen space.
111+
*
112+
* The old x and y values are stored in here during the InputManager.transformPointer call.
113+
*
114+
* You can use it to track how fast the pointer is moving, or to smoothly interpolate between the old and current position.
115+
* See the `Pointer.getInterpolatedPosition` method to assist in this.
116+
*
117+
* @name Phaser.Input.Pointer#prevPosition
118+
* @type {Phaser.Math.Vector2}
119+
* @since 3.11.0
120+
*/
121+
this.prevPosition = new Vector2();
122+
107123
/**
108124
* The x position of this Pointer, translated into the coordinate space of the most recent Camera it interacted with.
109125
*
@@ -646,6 +662,57 @@ var Pointer = new Class({
646662
return (this.buttons & 16);
647663
},
648664

665+
/**
666+
* Takes the previous and current Pointer positions and then generates an array of interpolated values between
667+
* the two. The array will be populated up to the size of the `steps` argument.
668+
*
669+
* ```javaScript
670+
* var points = pointer.getInterpolatedPosition(4);
671+
*
672+
* // points[0] = { x: 0, y: 0 }
673+
* // points[1] = { x: 2, y: 1 }
674+
* // points[2] = { x: 3, y: 2 }
675+
* // points[3] = { x: 6, y: 3 }
676+
* ```
677+
*
678+
* Use this if you need to get smoothed values between the previous and current pointer positions. DOM pointer
679+
* events can often fire faster than the main browser loop, and this will help you avoid janky movement
680+
* especially if you have an object following a Pointer.
681+
*
682+
* Note that if you provide an output array it will only be populated up to the number of steps provided.
683+
* It will not clear any previous data that may have existed beyond the range of the steps count.
684+
*
685+
* Internally it uses the Smooth Step interpolation calculation.
686+
*
687+
* @method Phaser.Input.Pointer#getInterpolatedPosition
688+
* @since 3.11.0
689+
*
690+
* @param {integer} [steps=10] - The number of interpolation steps to use.
691+
* @param {array} [out] - An array to store the results in. If not provided a new one will be created.
692+
*
693+
* @return {array} An array of interpolated values.
694+
*/
695+
getInterpolatedPosition: function (steps, out)
696+
{
697+
if (steps === undefined) { steps = 10; }
698+
if (out === undefined) { out = []; }
699+
700+
var prevX = this.prevPosition.x;
701+
var prevY = this.prevPosition.y;
702+
703+
var curX = this.position.x;
704+
var curY = this.position.y;
705+
706+
for (var i = 0; i < steps; i++)
707+
{
708+
var t = (1 / steps) * i;
709+
710+
out[i] = { x: SmoothStepInterpolation(t, prevX, curX), y: SmoothStepInterpolation(t, prevY, curY) };
711+
}
712+
713+
return out;
714+
},
715+
649716
/**
650717
* Destroys this Pointer instance and resets its external references.
651718
*

0 commit comments

Comments
 (0)