Skip to content

Commit 375db25

Browse files
committed
Quadratic Curve updates
* Quadratic Bezier Interpolation has been added to the Math.Interpolation functions * A new Quadratic Bezier Curve class has been added, expanding the available Curve types * Path.quadraticBezierTo allows you to add a Quadratic Bezier Curve into your Path.
1 parent 0653846 commit 375db25

5 files changed

Lines changed: 87 additions & 41 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
* The SceneManager has a new method: `remove` which allows you to remove and destroy a Scene, freeing up the Scene key for use by future scenes and potentially clearing the Scene from active memory for gc.
1616
* SceneManager.moveAbove will move a Scene to be directly above another Scene in the Scenes list. This is also exposed in the ScenePlugin.
1717
* SceneManager.moveBelow will move a Scene to be directly below another Scene in the Scenes list. This is also exposed in the ScenePlugin.
18+
* Quadratic Bezier Interpolation has been added to the Math.Interpolation functions (thanks @RiCoTeRoX)
19+
* A new Quadratic Bezier Curve class has been added, expanding the available Curve types (thanks @RiCoTeRoX)
20+
* Path.quadraticBezierTo allows you to add a Quadratic Bezier Curve into your Path.
1821

1922
### Bug Fixes
2023

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
55
*/
66

7-
87
var Class = require('../utils/Class');
9-
var QuadraticBezierInter = require('../math/interpolation/QuadraticBezierInterpolation');
108
var Curve = require('./Curve');
9+
var QuadraticBezierInterpolation = require('../math/interpolation/QuadraticBezierInterpolation');
1110
var Vector2 = require('../math/Vector2');
1211

1312
/**
@@ -18,12 +17,11 @@ var Vector2 = require('../math/Vector2');
1817
* @extends Phaser.Curves.Curve
1918
* @memberOf Phaser.Curves
2019
* @constructor
21-
* @since 3.0.0
20+
* @since 3.2.0
2221
*
2322
* @param {Phaser.Math.Vector2|Phaser.Math.Vector2[]} p0 - Start point, or an array of point pairs.
2423
* @param {Phaser.Math.Vector2} p1 - Control Point 1.
2524
* @param {Phaser.Math.Vector2} p2 - Control Point 2.
26-
* @param {Phaser.Math.Vector2} p3 - End Point.
2725
*/
2826
var QuadraticBezier = new Class({
2927

@@ -47,7 +45,7 @@ var QuadraticBezier = new Class({
4745
*
4846
* @name Phaser.Curves.QuadraticBezier#p0
4947
* @type {Phaser.Math.Vector2}
50-
* @since 3.0.0
48+
* @since 3.2.0
5149
*/
5250
this.p0 = p0;
5351

@@ -56,7 +54,7 @@ var QuadraticBezier = new Class({
5654
*
5755
* @name Phaser.Curves.QuadraticBezier#p1
5856
* @type {Phaser.Math.Vector2}
59-
* @since 3.0.0
57+
* @since 3.2.0
6058
*/
6159
this.p1 = p1;
6260

@@ -65,7 +63,7 @@ var QuadraticBezier = new Class({
6563
*
6664
* @name Phaser.Curves.QuadraticBezier#p2
6765
* @type {Phaser.Math.Vector2}
68-
* @since 3.0.0
66+
* @since 3.2.0
6967
*/
7068
this.p2 = p2;
7169
},
@@ -74,7 +72,7 @@ var QuadraticBezier = new Class({
7472
* [description]
7573
*
7674
* @method Phaser.Curves.QuadraticBezier#getStartPoint
77-
* @since 3.0.0
75+
* @since 3.2.0
7876
*
7977
* @param {Phaser.Math.Vector2} out - [description]
8078
*
@@ -91,11 +89,11 @@ var QuadraticBezier = new Class({
9189
* [description]
9290
*
9391
* @method Phaser.Curves.QuadraticBezier#getResolution
94-
* @since 3.0.0
92+
* @since 3.2.0
9593
*
96-
* @param {[type]} divisions - [description]
94+
* @param {number} divisions - [description]
9795
*
98-
* @return {[type]} [description]
96+
* @return {number} [description]
9997
*/
10098
getResolution: function (divisions)
10199
{
@@ -106,12 +104,12 @@ var QuadraticBezier = new Class({
106104
* [description]
107105
*
108106
* @method Phaser.Curves.QuadraticBezier#getPoint
109-
* @since 3.0.0
107+
* @since 3.2.0
110108
*
111-
* @param {[type]} t - [description]
112-
* @param {[type]} out - [description]
109+
* @param {number} t - [description]
110+
* @param {Phaser.Math.Vector2} [out] - [description]
113111
*
114-
* @return {[type]} [description]
112+
* @return {Phaser.Math.Vector2} [description]
115113
*/
116114
getPoint: function (t, out)
117115
{
@@ -121,14 +119,17 @@ var QuadraticBezier = new Class({
121119
var p1 = this.p1;
122120
var p2 = this.p2;
123121

124-
return out.set(QuadraticBezierInter(t, p0.x, p1.x, p2.x), QuadraticBezierInter(t, p0.y, p1.y, p2.y));
122+
return out.set(
123+
QuadraticBezierInterpolation(t, p0.x, p1.x, p2.x),
124+
QuadraticBezierInterpolation(t, p0.y, p1.y, p2.y)
125+
);
125126
},
126127

127128
/**
128129
* [description]
129130
*
130131
* @method Phaser.Curves.QuadraticBezier#draw
131-
* @since 3.0.0
132+
* @since 3.2.0
132133
*
133134
* @param {Phaser.GameObjects.Graphics} graphics - [description]
134135
* @param {integer} [pointsTotal=32] - [description]
@@ -159,7 +160,7 @@ var QuadraticBezier = new Class({
159160
* [description]
160161
*
161162
* @method Phaser.Curves.QuadraticBezier#toJSON
162-
* @since 3.0.0
163+
* @since 3.2.0
163164
*
164165
* @return {object} [description]
165166
*/

src/curves/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ module.exports = {
1515
Curve: require('./Curve'),
1616
Ellipse: require('./EllipseCurve'),
1717
Line: require('./LineCurve'),
18-
Spline: require('./SplineCurve'),
19-
QuadraticBezier: require('./QuadraticBezier')
18+
QuadraticBezier: require('./QuadraticBezierCurve'),
19+
Spline: require('./SplineCurve')
2020
};

src/curves/path/Path.js

Lines changed: 62 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var EllipseCurve = require('../EllipseCurve');
1212
var GameObjectFactory = require('../../gameobjects/GameObjectFactory');
1313
var LineCurve = require('../LineCurve');
1414
var MovePathTo = require('./MoveTo');
15+
var QuadraticBezierCurve = require('../QuadraticBezierCurve');
1516
var Rectangle = require('../../geom/rectangle/Rectangle');
1617
var SplineCurve = require('../SplineCurve');
1718
var Vector2 = require('../../math/Vector2');
@@ -140,8 +141,8 @@ var Path = new Class({
140141
* @since 3.0.0
141142
*
142143
* @param {number} radius - [description]
143-
* @param {boolean} [clockwise] - [description]
144-
* @param {number} [rotation] - [description]
144+
* @param {boolean} [clockwise=false] - [description]
145+
* @param {number} [rotation=0] - [description]
145146
*
146147
* @return {Phaser.Curves.Path} [description]
147148
*/
@@ -185,10 +186,10 @@ var Path = new Class({
185186
*
186187
* @param {number} x - [description]
187188
* @param {number} y - [description]
188-
* @param {Phaser.Math.Vector2} control1X - {Phaser.Math[description]
189-
* @param {Phaser.Math.Vector2} control1Y - {Phaser.Math[description]
190-
* @param {Phaser.Math.Vector2} control2X - {Phaser.Math[description]
191-
* @param {Phaser.Math.Vector2} control2Y - {Phaser.Math[description]
189+
* @param {Phaser.Math.Vector2} control1X - [description]
190+
* @param {Phaser.Math.Vector2} control1Y - [description]
191+
* @param {Phaser.Math.Vector2} control2X - [description]
192+
* @param {Phaser.Math.Vector2} control2Y - [description]
192193
*
193194
* @return {Phaser.Curves.Path} [description]
194195
*/
@@ -216,17 +217,41 @@ var Path = new Class({
216217
return this.add(new CubicBezierCurve(p0, p1, p2, p3));
217218
},
218219

220+
// Creates a quadratic bezier curve starting at the previous end point and ending at p2, using p1 as a control point
221+
219222
/**
220223
* [description]
221224
*
222-
* @method Phaser.Curves.Path#destroy
223-
* @since 3.0.0
225+
* @method Phaser.Curves.Path#quadraticBezierTo
226+
* @since 3.2.0
227+
*
228+
* @param {number|Phaser.Math.Vector2[]} x - [description]
229+
* @param {number} [y] - [description]
230+
* @param {number} [controlX] - [description]
231+
* @param {number} [controlY] - [description]
232+
*
233+
* @return {Phaser.Curves.Path} [description]
224234
*/
225-
destroy: function ()
235+
quadraticBezierTo: function (x, y, controlX, controlY)
226236
{
227-
this.curves.length = 0;
228-
this.cacheLengths.length = 0;
229-
this.startPoint = undefined;
237+
var p0 = this.getEndPoint();
238+
var p1;
239+
var p2;
240+
241+
// Assume they're all vec2s
242+
if (x instanceof Vector2)
243+
{
244+
p1 = x;
245+
p2 = y;
246+
p3 = controlX;
247+
}
248+
else
249+
{
250+
p1 = new Vector2(controlX, controlY);
251+
p2 = new Vector2(x, y);
252+
}
253+
254+
return this.add(new QuadraticBezierCurve(p0, p1, p2));
230255
},
231256

232257
/**
@@ -331,6 +356,10 @@ var Path = new Class({
331356
case 'CubicBezierCurve':
332357
this.add(CubicBezierCurve.fromJSON(curve));
333358
break;
359+
360+
case 'QuadraticBezierCurve':
361+
this.add(QuadraticBezierCurve.fromJSON(curve));
362+
break;
334363
}
335364
}
336365

@@ -425,9 +454,9 @@ var Path = new Class({
425454
* @method Phaser.Curves.Path#getEndPoint
426455
* @since 3.0.0
427456
*
428-
* @param {Phaser.Math.Vector2} [out] - {Phaser.Math[description]
457+
* @param {Phaser.Math.Vector2} [out] - [description]
429458
*
430-
* @return {Phaser.Math.Vector2} {Phaser.Math[description]
459+
* @return {Phaser.Math.Vector2} [description]
431460
*/
432461
getEndPoint: function (out)
433462
{
@@ -476,7 +505,7 @@ var Path = new Class({
476505
* @since 3.0.0
477506
*
478507
* @param {number} t - [description]
479-
* @param {Phaser.Math.Vector2} [out] - {Phaser.Math[description]
508+
* @param {Phaser.Math.Vector2} [out] - [description]
480509
*
481510
* @return {Phaser.Math.Vector2|null} [description]
482511
*/
@@ -568,9 +597,9 @@ var Path = new Class({
568597
* @method Phaser.Curves.Path#getRandomPoint
569598
* @since 3.0.0
570599
*
571-
* @param {Phaser.Math.Vector2} [out] - {Phaser.Math[description]
600+
* @param {Phaser.Math.Vector2} [out] - [description]
572601
*
573-
* @return {Phaser.Math.Vector2} {Phaser.Math[description]
602+
* @return {Phaser.Math.Vector2} [description]
574603
*/
575604
getRandomPoint: function (out)
576605
{
@@ -614,9 +643,9 @@ var Path = new Class({
614643
* @method Phaser.Curves.Path#getStartPoint
615644
* @since 3.0.0
616645
*
617-
* @param {Phaser.Math.Vector2} [out] - {Phaser.Math[description]
646+
* @param {Phaser.Math.Vector2} [out] - [description]
618647
*
619-
* @return {Phaser.Math.Vector2} {Phaser.Math[description]
648+
* @return {Phaser.Math.Vector2} [description]
620649
*/
621650
getStartPoint: function (out)
622651
{
@@ -633,7 +662,7 @@ var Path = new Class({
633662
* @method Phaser.Curves.Path#lineTo
634663
* @since 3.0.0
635664
*
636-
* @param {number|Phaser.Math.Vector2} x - {Phaser.Math[description]
665+
* @param {number|Phaser.Math.Vector2} x - [description]
637666
* @param {number} [y] - [description]
638667
*
639668
* @return {Phaser.Curves.Path} [description]
@@ -728,6 +757,19 @@ var Path = new Class({
728757
this.cacheLengths = [];
729758

730759
this.getCurveLengths();
760+
},
761+
762+
/**
763+
* [description]
764+
*
765+
* @method Phaser.Curves.Path#destroy
766+
* @since 3.0.0
767+
*/
768+
destroy: function ()
769+
{
770+
this.curves.length = 0;
771+
this.cacheLengths.length = 0;
772+
this.startPoint = undefined;
731773
}
732774

733775
});

src/math/interpolation/QuadraticBezierInterpolation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function P2 (t, p)
3131
* [description]
3232
*
3333
* @function Phaser.Math.Interpolation.QuadraticBezier
34-
* @since 3.0.0
34+
* @since 3.2.0
3535
*
3636
* @param {float} t - [description]
3737
* @param {number} p0 - [description]

0 commit comments

Comments
 (0)