2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2018 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
2017-09-20 22:10:37 +00:00
|
|
|
// Based on the three.js Curve classes created by [zz85](http://www.lab4games.net/zz85/blog)
|
|
|
|
|
2018-02-13 00:40:51 +00:00
|
|
|
var Class = require('../utils/Class');
|
|
|
|
var CubicBezier = require('../math/interpolation/CubicBezierInterpolation');
|
|
|
|
var Curve = require('./Curve');
|
|
|
|
var Vector2 = require('../math/Vector2');
|
2017-09-20 22:10:37 +00:00
|
|
|
|
2018-02-07 15:27:21 +00:00
|
|
|
/**
|
|
|
|
* @classdesc
|
|
|
|
* [description]
|
|
|
|
*
|
2018-05-23 14:04:54 +00:00
|
|
|
* @class CubicBezier
|
2018-02-07 15:27:21 +00:00
|
|
|
* @extends Phaser.Curves.Curve
|
|
|
|
* @memberOf Phaser.Curves
|
|
|
|
* @constructor
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-03-20 14:58:02 +00:00
|
|
|
* @param {(Phaser.Math.Vector2|Phaser.Math.Vector2[])} p0 - Start point, or an array of point pairs.
|
2018-02-13 00:40:51 +00:00
|
|
|
* @param {Phaser.Math.Vector2} p1 - Control Point 1.
|
|
|
|
* @param {Phaser.Math.Vector2} p2 - Control Point 2.
|
|
|
|
* @param {Phaser.Math.Vector2} p3 - End Point.
|
2018-02-07 15:27:21 +00:00
|
|
|
*/
|
2017-09-20 22:10:37 +00:00
|
|
|
var CubicBezierCurve = new Class({
|
|
|
|
|
|
|
|
Extends: Curve,
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
2017-09-22 15:41:11 +00:00
|
|
|
function CubicBezierCurve (p0, p1, p2, p3)
|
2017-09-20 22:10:37 +00:00
|
|
|
{
|
2017-10-02 21:42:47 +00:00
|
|
|
Curve.call(this, 'CubicBezierCurve');
|
2017-09-21 00:19:27 +00:00
|
|
|
|
2017-09-22 15:41:11 +00:00
|
|
|
if (Array.isArray(p0))
|
|
|
|
{
|
|
|
|
p3 = new Vector2(p0[6], p0[7]);
|
|
|
|
p2 = new Vector2(p0[4], p0[5]);
|
|
|
|
p1 = new Vector2(p0[2], p0[3]);
|
|
|
|
p0 = new Vector2(p0[0], p0[1]);
|
|
|
|
}
|
|
|
|
|
2018-01-25 05:26:13 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
2018-05-23 22:09:31 +00:00
|
|
|
* @name Phaser.Curves.CubicBezier#p0
|
2018-02-13 00:40:51 +00:00
|
|
|
* @type {Phaser.Math.Vector2}
|
2018-01-25 05:26:13 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-09-22 15:41:11 +00:00
|
|
|
this.p0 = p0;
|
2018-01-25 05:26:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
2018-05-23 22:09:31 +00:00
|
|
|
* @name Phaser.Curves.CubicBezier#p1
|
2018-02-13 00:40:51 +00:00
|
|
|
* @type {Phaser.Math.Vector2}
|
2018-01-25 05:26:13 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-09-22 15:41:11 +00:00
|
|
|
this.p1 = p1;
|
2018-01-25 05:26:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
2018-05-23 22:09:31 +00:00
|
|
|
* @name Phaser.Curves.CubicBezier#p2
|
2018-02-13 00:40:51 +00:00
|
|
|
* @type {Phaser.Math.Vector2}
|
2018-01-25 05:26:13 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-09-22 15:41:11 +00:00
|
|
|
this.p2 = p2;
|
2018-01-25 05:26:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
2018-05-23 22:09:31 +00:00
|
|
|
* @name Phaser.Curves.CubicBezier#p3
|
2018-02-13 00:40:51 +00:00
|
|
|
* @type {Phaser.Math.Vector2}
|
2018-01-25 05:26:13 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-09-22 15:41:11 +00:00
|
|
|
this.p3 = p3;
|
|
|
|
},
|
|
|
|
|
2018-01-25 05:26:13 +00:00
|
|
|
/**
|
2018-03-18 13:43:37 +00:00
|
|
|
* Gets the starting point on the curve.
|
2018-01-25 05:26:13 +00:00
|
|
|
*
|
2018-05-23 22:09:31 +00:00
|
|
|
* @method Phaser.Curves.CubicBezier#getStartPoint
|
2018-01-25 05:26:13 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-03-27 12:06:24 +00:00
|
|
|
* @generic {Phaser.Math.Vector2} O - [out,$return]
|
|
|
|
*
|
2018-03-18 13:43:37 +00:00
|
|
|
* @param {Phaser.Math.Vector2} [out] - A Vector2 object to store the result in. If not given will be created.
|
2018-01-25 05:26:13 +00:00
|
|
|
*
|
2018-03-18 13:43:37 +00:00
|
|
|
* @return {Phaser.Math.Vector2} The coordinates of the point on the curve. If an `out` object was given this will be returned.
|
2018-01-25 05:26:13 +00:00
|
|
|
*/
|
2017-09-27 01:32:26 +00:00
|
|
|
getStartPoint: function (out)
|
2017-09-22 15:41:11 +00:00
|
|
|
{
|
2017-09-27 01:32:26 +00:00
|
|
|
if (out === undefined) { out = new Vector2(); }
|
|
|
|
|
|
|
|
return out.copy(this.p0);
|
2017-09-20 22:10:37 +00:00
|
|
|
},
|
|
|
|
|
2018-01-25 05:26:13 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
2018-05-23 22:09:31 +00:00
|
|
|
* @method Phaser.Curves.CubicBezier#getResolution
|
2018-01-25 05:26:13 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-03-18 13:43:37 +00:00
|
|
|
* @param {number} divisions - The amount of divisions used by this curve.
|
2018-01-25 05:26:13 +00:00
|
|
|
*
|
2018-03-18 13:43:37 +00:00
|
|
|
* @return {number} The resolution of the curve.
|
2018-01-25 05:26:13 +00:00
|
|
|
*/
|
2017-09-21 16:12:16 +00:00
|
|
|
getResolution: function (divisions)
|
|
|
|
{
|
|
|
|
return divisions;
|
|
|
|
},
|
|
|
|
|
2018-01-25 05:26:13 +00:00
|
|
|
/**
|
2018-03-18 13:43:37 +00:00
|
|
|
* Get point at relative position in curve according to length.
|
2018-01-25 05:26:13 +00:00
|
|
|
*
|
2018-05-23 22:09:31 +00:00
|
|
|
* @method Phaser.Curves.CubicBezier#getPoint
|
2018-01-25 05:26:13 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-03-27 12:06:24 +00:00
|
|
|
* @generic {Phaser.Math.Vector2} O - [out,$return]
|
|
|
|
*
|
2018-06-26 22:19:14 +00:00
|
|
|
* @param {number} t - The position along the curve to return. Where 0 is the start and 1 is the end.
|
2018-03-18 13:43:37 +00:00
|
|
|
* @param {Phaser.Math.Vector2} [out] - A Vector2 object to store the result in. If not given will be created.
|
2018-01-25 05:26:13 +00:00
|
|
|
*
|
2018-03-18 13:43:37 +00:00
|
|
|
* @return {Phaser.Math.Vector2} The coordinates of the point on the curve. If an `out` object was given this will be returned.
|
2018-01-25 05:26:13 +00:00
|
|
|
*/
|
2017-09-20 22:10:37 +00:00
|
|
|
getPoint: function (t, out)
|
|
|
|
{
|
|
|
|
if (out === undefined) { out = new Vector2(); }
|
|
|
|
|
2017-09-22 15:41:11 +00:00
|
|
|
var p0 = this.p0;
|
|
|
|
var p1 = this.p1;
|
|
|
|
var p2 = this.p2;
|
|
|
|
var p3 = this.p3;
|
|
|
|
|
|
|
|
return out.set(CubicBezier(t, p0.x, p1.x, p2.x, p3.x), CubicBezier(t, p0.y, p1.y, p2.y, p3.y));
|
|
|
|
},
|
|
|
|
|
2018-01-25 05:26:13 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
2018-05-23 22:09:31 +00:00
|
|
|
* @method Phaser.Curves.CubicBezier#draw
|
2018-01-25 05:26:13 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-04-18 12:39:55 +00:00
|
|
|
* @generic {Phaser.GameObjects.Graphics} G - [graphics,$return]
|
2018-03-27 12:06:24 +00:00
|
|
|
*
|
2018-02-13 00:40:51 +00:00
|
|
|
* @param {Phaser.GameObjects.Graphics} graphics - [description]
|
|
|
|
* @param {integer} [pointsTotal=32] - [description]
|
2018-01-25 05:26:13 +00:00
|
|
|
*
|
2018-02-13 00:40:51 +00:00
|
|
|
* @return {Phaser.GameObjects.Graphics} [description]
|
2018-01-25 05:26:13 +00:00
|
|
|
*/
|
2017-09-22 15:41:11 +00:00
|
|
|
draw: function (graphics, pointsTotal)
|
|
|
|
{
|
|
|
|
if (pointsTotal === undefined) { pointsTotal = 32; }
|
|
|
|
|
|
|
|
var points = this.getPoints(pointsTotal);
|
|
|
|
|
|
|
|
graphics.beginPath();
|
|
|
|
graphics.moveTo(this.p0.x, this.p0.y);
|
|
|
|
|
|
|
|
for (var i = 1; i < points.length; i++)
|
|
|
|
{
|
|
|
|
graphics.lineTo(points[i].x, points[i].y);
|
|
|
|
}
|
|
|
|
|
|
|
|
graphics.strokePath();
|
2017-09-20 22:10:37 +00:00
|
|
|
|
2017-09-22 15:41:11 +00:00
|
|
|
// So you can chain graphics calls
|
|
|
|
return graphics;
|
2017-09-22 18:36:00 +00:00
|
|
|
},
|
|
|
|
|
2018-01-25 05:26:13 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
2018-05-23 22:09:31 +00:00
|
|
|
* @method Phaser.Curves.CubicBezier#toJSON
|
2018-01-25 05:26:13 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-03-19 15:53:55 +00:00
|
|
|
* @return {JSONCurve} The JSON object containing this curve data.
|
2018-01-25 05:26:13 +00:00
|
|
|
*/
|
2017-09-22 18:36:00 +00:00
|
|
|
toJSON: function ()
|
|
|
|
{
|
|
|
|
return {
|
2017-10-02 21:42:47 +00:00
|
|
|
type: this.type,
|
2017-09-22 18:36:00 +00:00
|
|
|
points: [
|
|
|
|
this.p0.x, this.p0.y,
|
|
|
|
this.p1.x, this.p1.y,
|
|
|
|
this.p2.x, this.p2.y,
|
|
|
|
this.p3.x, this.p3.y
|
|
|
|
]
|
|
|
|
};
|
2017-09-20 22:10:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2018-03-18 13:43:37 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
2018-05-23 22:09:31 +00:00
|
|
|
* @function Phaser.Curves.CubicBezier.fromJSON
|
2018-03-18 13:43:37 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-03-19 15:53:55 +00:00
|
|
|
* @param {JSONCurve} data - The JSON object containing this curve data.
|
2018-03-18 13:43:37 +00:00
|
|
|
*
|
2018-05-23 22:09:31 +00:00
|
|
|
* @return {Phaser.Curves.CubicBezier} [description]
|
2018-03-18 13:43:37 +00:00
|
|
|
*/
|
2017-10-02 21:42:47 +00:00
|
|
|
CubicBezierCurve.fromJSON = function (data)
|
|
|
|
{
|
|
|
|
var points = data.points;
|
|
|
|
|
|
|
|
var p0 = new Vector2(points[0], points[1]);
|
|
|
|
var p1 = new Vector2(points[2], points[3]);
|
|
|
|
var p2 = new Vector2(points[4], points[5]);
|
|
|
|
var p3 = new Vector2(points[6], points[7]);
|
|
|
|
|
|
|
|
return new CubicBezierCurve(p0, p1, p2, p3);
|
|
|
|
};
|
|
|
|
|
2017-09-20 22:10:37 +00:00
|
|
|
module.exports = CubicBezierCurve;
|