Skip to content

Commit af8f873

Browse files
authored
Merge pull request phaserjs#3307 from RiCoTeRoX/curves-quadratic
Add quadraticBezier to API
2 parents 7d777f3 + af3e47c commit af8f873

4 files changed

Lines changed: 243 additions & 6 deletions

File tree

src/curves/QuadraticBezier.js

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2018 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
8+
var Class = require('../utils/Class');
9+
var QuadraticBezierInter = require('../math/interpolation/QuadraticBezierInterpolation');
10+
var Curve = require('./Curve');
11+
var Vector2 = require('../math/Vector2');
12+
13+
/**
14+
* @classdesc
15+
* [description]
16+
*
17+
* @class QuadraticBezier
18+
* @extends Phaser.Curves.Curve
19+
* @memberOf Phaser.Curves
20+
* @constructor
21+
* @since 3.0.0
22+
*
23+
* @param {Phaser.Math.Vector2|Phaser.Math.Vector2[]} p0 - Start point, or an array of point pairs.
24+
* @param {Phaser.Math.Vector2} p1 - Control Point 1.
25+
* @param {Phaser.Math.Vector2} p2 - Control Point 2.
26+
* @param {Phaser.Math.Vector2} p3 - End Point.
27+
*/
28+
var QuadraticBezier = new Class({
29+
30+
Extends: Curve,
31+
32+
initialize:
33+
34+
function QuadraticBezier (p0, p1, p2)
35+
{
36+
Curve.call(this, 'QuadraticBezier');
37+
38+
if (Array.isArray(p0))
39+
{
40+
p2 = new Vector2(p0[4], p0[5]);
41+
p1 = new Vector2(p0[2], p0[3]);
42+
p0 = new Vector2(p0[0], p0[1]);
43+
}
44+
45+
/**
46+
* [description]
47+
*
48+
* @name Phaser.Curves.QuadraticBezier#p0
49+
* @type {Phaser.Math.Vector2}
50+
* @since 3.0.0
51+
*/
52+
this.p0 = p0;
53+
54+
/**
55+
* [description]
56+
*
57+
* @name Phaser.Curves.QuadraticBezier#p1
58+
* @type {Phaser.Math.Vector2}
59+
* @since 3.0.0
60+
*/
61+
this.p1 = p1;
62+
63+
/**
64+
* [description]
65+
*
66+
* @name Phaser.Curves.QuadraticBezier#p2
67+
* @type {Phaser.Math.Vector2}
68+
* @since 3.0.0
69+
*/
70+
this.p2 = p2;
71+
},
72+
73+
/**
74+
* [description]
75+
*
76+
* @method Phaser.Curves.QuadraticBezier#getStartPoint
77+
* @since 3.0.0
78+
*
79+
* @param {Phaser.Math.Vector2} out - [description]
80+
*
81+
* @return {Phaser.Math.Vector2} [description]
82+
*/
83+
getStartPoint: function (out)
84+
{
85+
if (out === undefined) { out = new Vector2(); }
86+
87+
return out.copy(this.p0);
88+
},
89+
90+
/**
91+
* [description]
92+
*
93+
* @method Phaser.Curves.QuadraticBezier#getResolution
94+
* @since 3.0.0
95+
*
96+
* @param {[type]} divisions - [description]
97+
*
98+
* @return {[type]} [description]
99+
*/
100+
getResolution: function (divisions)
101+
{
102+
return divisions;
103+
},
104+
105+
/**
106+
* [description]
107+
*
108+
* @method Phaser.Curves.QuadraticBezier#getPoint
109+
* @since 3.0.0
110+
*
111+
* @param {[type]} t - [description]
112+
* @param {[type]} out - [description]
113+
*
114+
* @return {[type]} [description]
115+
*/
116+
getPoint: function (t, out)
117+
{
118+
if (out === undefined) { out = new Vector2(); }
119+
120+
var p0 = this.p0;
121+
var p1 = this.p1;
122+
var p2 = this.p2;
123+
124+
return out.set(QuadraticBezierInter(t, p0.x, p1.x, p2.x), QuadraticBezierInter(t, p0.y, p1.y, p2.y));
125+
},
126+
127+
/**
128+
* [description]
129+
*
130+
* @method Phaser.Curves.QuadraticBezier#draw
131+
* @since 3.0.0
132+
*
133+
* @param {Phaser.GameObjects.Graphics} graphics - [description]
134+
* @param {integer} [pointsTotal=32] - [description]
135+
*
136+
* @return {Phaser.GameObjects.Graphics} [description]
137+
*/
138+
draw: function (graphics, pointsTotal)
139+
{
140+
if (pointsTotal === undefined) { pointsTotal = 32; }
141+
142+
var points = this.getPoints(pointsTotal);
143+
144+
graphics.beginPath();
145+
graphics.moveTo(this.p0.x, this.p0.y);
146+
147+
for (var i = 1; i < points.length; i++)
148+
{
149+
graphics.lineTo(points[i].x, points[i].y);
150+
}
151+
152+
graphics.strokePath();
153+
154+
// So you can chain graphics calls
155+
return graphics;
156+
},
157+
158+
/**
159+
* [description]
160+
*
161+
* @method Phaser.Curves.QuadraticBezier#toJSON
162+
* @since 3.0.0
163+
*
164+
* @return {object} [description]
165+
*/
166+
toJSON: function ()
167+
{
168+
return {
169+
type: this.type,
170+
points: [
171+
this.p0.x, this.p0.y,
172+
this.p1.x, this.p1.y,
173+
this.p2.x, this.p2.y
174+
]
175+
};
176+
}
177+
178+
});
179+
180+
QuadraticBezier.fromJSON = function (data)
181+
{
182+
var points = data.points;
183+
184+
var p0 = new Vector2(points[0], points[1]);
185+
var p1 = new Vector2(points[2], points[3]);
186+
var p2 = new Vector2(points[4], points[5]);
187+
188+
return new QuadraticBezier(p0, p1, p2);
189+
};
190+
191+
module.exports = QuadraticBezier;

src/curves/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@
99
*/
1010

1111
module.exports = {
12-
1312
Path: require('./path/Path'),
1413

1514
CubicBezier: require('./CubicBezierCurve'),
1615
Curve: require('./Curve'),
1716
Ellipse: require('./EllipseCurve'),
1817
Line: require('./LineCurve'),
19-
Spline: require('./SplineCurve')
20-
18+
Spline: require('./SplineCurve'),
19+
QuadraticBezier: require('./QuadraticBezier')
2120
};
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2018 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
function P0 (t, p)
8+
{
9+
var k = 1 - t;
10+
11+
return k * k * p;
12+
}
13+
14+
function P1 (t, p)
15+
{
16+
return 2 * ( 1 - t ) * t * p;
17+
}
18+
19+
function P2 (t, p)
20+
{
21+
return t * t * p;
22+
}
23+
24+
// p0 = start point
25+
// p1 = control point 1
26+
// p2 = end point
27+
28+
// https://github.com/mrdoob/three.js/blob/master/src/extras/core/Interpolations.js
29+
30+
/**
31+
* [description]
32+
*
33+
* @function Phaser.Math.Interpolation.QuadraticBezier
34+
* @since 3.0.0
35+
*
36+
* @param {float} t - [description]
37+
* @param {number} p0 - [description]
38+
* @param {number} p1 - [description]
39+
* @param {number} p2 - [description]
40+
*
41+
* @return {number} [description]
42+
*/
43+
var QuadraticBezierInterpolation = function (t, p0, p1, p2)
44+
{
45+
return P0(t, p0) + P1(t, p1) + P2(t, p2);
46+
};
47+
48+
module.exports = QuadraticBezierInterpolation;

src/math/interpolation/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@
99
*/
1010

1111
module.exports = {
12-
1312
Bezier: require('./BezierInterpolation'),
1413
CatmullRom: require('./CatmullRomInterpolation'),
1514
CubicBezier: require('./CubicBezierInterpolation'),
16-
Linear: require('./LinearInterpolation')
17-
15+
Linear: require('./LinearInterpolation'),
16+
QuadraticBezier: require('./QuadraticBezierInterpolation')
1817
};

0 commit comments

Comments
 (0)