phaser/src/curves/LineCurve.js

301 lines
8.4 KiB
JavaScript
Raw Normal View History

2018-02-12 16:01:20 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
2023-01-02 17:36:27 +00:00
* @copyright 2013-2023 Photon Storm Ltd.
2019-05-10 15:15:04 +00:00
* @license {@link https://opensource.org/licenses/MIT|MIT License}
2018-02-12 16:01:20 +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 Curve = require('./Curve');
var FromPoints = require('../geom/rectangle/FromPoints');
var Rectangle = require('../geom/rectangle/Rectangle');
var Vector2 = require('../math/Vector2');
2018-02-07 15:27:21 +00:00
/**
* @classdesc
2018-10-19 11:32:43 +00:00
* A LineCurve is a "curve" comprising exactly two points (a line segment).
2018-02-07 15:27:21 +00:00
*
2018-05-23 14:04:54 +00:00
* @class Line
2018-02-07 15:27:21 +00:00
* @extends Phaser.Curves.Curve
2018-10-10 09:49:13 +00:00
* @memberof Phaser.Curves
2018-02-07 15:27:21 +00:00
* @constructor
* @since 3.0.0
*
2018-10-19 11:32:43 +00:00
* @param {(Phaser.Math.Vector2|number[])} p0 - The first endpoint.
* @param {Phaser.Math.Vector2} [p1] - The second endpoint.
2018-02-07 15:27:21 +00:00
*/
var LineCurve = new Class({
Extends: Curve,
initialize:
// vec2s or array
function LineCurve (p0, p1)
{
Curve.call(this, 'LineCurve');
if (Array.isArray(p0))
{
p1 = new Vector2(p0[2], p0[3]);
p0 = new Vector2(p0[0], p0[1]);
}
2018-01-25 05:26:13 +00:00
/**
2018-10-19 11:32:43 +00:00
* The first endpoint.
2018-01-25 05:26:13 +00:00
*
2018-05-23 22:09:31 +00:00
* @name Phaser.Curves.Line#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
*/
this.p0 = p0;
2018-01-25 05:26:13 +00:00
/**
2018-10-19 11:32:43 +00:00
* The second endpoint.
2018-01-25 05:26:13 +00:00
*
2018-05-23 22:09:31 +00:00
* @name Phaser.Curves.Line#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
*/
this.p1 = p1;
// Override default Curve.arcLengthDivisions
/**
* The quantity of arc length divisions within the curve.
*
2019-11-18 03:11:25 +00:00
* @name Phaser.Curves.Line#arcLengthDivisions
2020-11-23 10:22:13 +00:00
* @type {number}
* @default 1
* @since 3.0.0
*/
2019-11-18 16:31:20 +00:00
this.arcLengthDivisions = 1;
},
2018-01-25 05:26:13 +00:00
/**
2018-03-18 13:43:37 +00:00
* Returns a Rectangle where the position and dimensions match the bounds of this Curve.
2018-01-25 05:26:13 +00:00
*
2018-05-23 22:09:31 +00:00
* @method Phaser.Curves.Line#getBounds
2018-01-25 05:26:13 +00:00
* @since 3.0.0
*
2018-03-27 12:06:24 +00:00
* @generic {Phaser.Geom.Rectangle} O - [out,$return]
*
2018-03-18 13:43:37 +00:00
* @param {Phaser.Geom.Rectangle} [out] - A Rectangle object to store the bounds in. If not given a new Rectangle will be created.
2018-01-25 05:26:13 +00:00
*
2018-03-18 13:43:37 +00:00
* @return {Phaser.Geom.Rectangle} A Rectangle object holding the bounds of this curve. If `out` was given it will be this object.
2018-01-25 05:26:13 +00:00
*/
getBounds: function (out)
{
if (out === undefined) { out = new Rectangle(); }
return FromPoints([ this.p0, this.p1 ], out);
},
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.Line#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
*/
getStartPoint: function (out)
{
if (out === undefined) { out = new Vector2(); }
return out.copy(this.p0);
},
2018-01-25 05:26:13 +00:00
/**
2018-10-19 11:32:43 +00:00
* Gets the resolution of the line.
2018-01-25 05:26:13 +00:00
*
2018-05-23 22:09:31 +00:00
* @method Phaser.Curves.Line#getResolution
2018-01-25 05:26:13 +00:00
* @since 3.0.0
*
2018-10-19 11:32:43 +00:00
* @param {number} [divisions=1] - The number of divisions to consider.
*
2018-10-19 11:32:43 +00:00
* @return {number} The resolution. Equal to the number of divisions.
2018-01-25 05:26:13 +00:00
*/
getResolution: function (divisions)
2017-09-21 16:12:16 +00:00
{
if (divisions === undefined) { divisions = 1; }
return divisions;
2017-09-21 16:12:16 +00:00
},
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.Line#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]
*
* @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
*/
getPoint: function (t, out)
{
if (out === undefined) { out = new Vector2(); }
if (t === 1)
{
return out.copy(this.p1);
}
out.copy(this.p1).subtract(this.p0).scale(t).add(this.p0);
return out;
},
// Line curve is linear, so we can overwrite default getPointAt
2018-03-18 13:43:37 +00:00
2018-01-25 05:26:13 +00:00
/**
2018-10-19 11:32:43 +00:00
* Gets a point at a given position on the line.
2018-01-25 05:26:13 +00:00
*
2018-05-23 22:09:31 +00:00
* @method Phaser.Curves.Line#getPointAt
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]
*
* @param {number} u - 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
*/
getPointAt: function (u, out)
{
return this.getPoint(u, out);
},
2018-01-25 05:26:13 +00:00
/**
2018-10-19 11:32:43 +00:00
* Gets the slope of the line as a unit vector.
2018-01-25 05:26:13 +00:00
*
2018-05-23 22:09:31 +00:00
* @method Phaser.Curves.Line#getTangent
2018-01-25 05:26:13 +00:00
* @since 3.0.0
*
2018-04-18 12:39:55 +00:00
* @generic {Phaser.Math.Vector2} O - [out,$return]
2018-01-25 05:26:13 +00:00
*
* @param {number} [t] - The relative position on the line, [0..1].
* @param {Phaser.Math.Vector2} [out] - A vector to store the result in.
*
2018-10-19 11:32:43 +00:00
* @return {Phaser.Math.Vector2} The tangent vector.
2018-01-25 05:26:13 +00:00
*/
getTangent: function (t, out)
{
if (out === undefined) { out = new Vector2(); }
out.copy(this.p1).subtract(this.p0).normalize();
return out;
},
/**
2020-04-27 14:41:44 +00:00
* Given u ( 0 .. 1 ), get a t to find p. This gives you points which are equidistant.
*
* @method Phaser.Curves.Line#getUtoTmapping
* @since 3.0.0
*
2020-04-27 14:41:44 +00:00
* @param {number} u - A float between 0 and 1.
2020-11-23 10:22:13 +00:00
* @param {number} distance - The distance, in pixels.
* @param {number} [divisions] - Optional amount of divisions.
*
2020-04-27 14:41:44 +00:00
* @return {number} The equidistant value.
*/
getUtoTmapping: function (u, distance, divisions)
{
var t;
if (distance)
{
var arcLengths = this.getLengths(divisions);
2019-11-26 02:15:23 +00:00
var lineLength = arcLengths[arcLengths.length - 1];
2019-12-28 17:28:50 +00:00
// Cannot overshoot the curve
var targetLineLength = Math.min(distance, lineLength);
t = targetLineLength / lineLength;
}
else
{
t = u;
2019-12-28 17:28:50 +00:00
}
return t;
},
// Override default Curve.draw because this is better than calling getPoints on a line!
2018-03-18 13:43:37 +00:00
2018-01-25 05:26:13 +00:00
/**
2018-03-18 13:43:37 +00:00
* Draws this curve on the given Graphics object.
2018-03-19 15:53:55 +00:00
*
2018-03-18 13:43:37 +00:00
* The curve is drawn using `Graphics.lineBetween` so will be drawn at whatever the present Graphics line color is.
* The Graphics object is not cleared before the draw, so the curve will appear on-top of anything else already rendered to it.
2018-01-25 05:26:13 +00:00
*
2018-05-23 22:09:31 +00:00
* @method Phaser.Curves.Line#draw
2018-01-25 05:26:13 +00:00
* @since 3.0.0
*
2018-03-27 12:06:24 +00:00
* @generic {Phaser.GameObjects.Graphics} G - [graphics,$return]
*
2018-03-18 13:43:37 +00:00
* @param {Phaser.GameObjects.Graphics} graphics - The Graphics instance onto which this curve will be drawn.
2018-01-25 05:26:13 +00:00
*
2018-03-18 13:43:37 +00:00
* @return {Phaser.GameObjects.Graphics} The Graphics object to which the curve was drawn.
2018-01-25 05:26:13 +00:00
*/
draw: function (graphics)
{
graphics.lineBetween(this.p0.x, this.p0.y, this.p1.x, this.p1.y);
// So you can chain graphics calls
return graphics;
},
2018-01-25 05:26:13 +00:00
/**
2018-10-19 11:32:43 +00:00
* Gets a JSON representation of the line.
2018-01-25 05:26:13 +00:00
*
2018-05-23 22:09:31 +00:00
* @method Phaser.Curves.Line#toJSON
2018-01-25 05:26:13 +00:00
* @since 3.0.0
*
2019-05-09 10:52:07 +00:00
* @return {Phaser.Types.Curves.JSONCurve} The JSON object containing this curve data.
2018-01-25 05:26:13 +00:00
*/
toJSON: function ()
{
return {
type: this.type,
points: [
this.p0.x, this.p0.y,
this.p1.x, this.p1.y
]
};
}
});
2018-03-18 13:43:37 +00:00
/**
2018-10-19 11:32:43 +00:00
* Configures this line from a JSON representation.
2018-03-18 13:43:37 +00:00
*
2018-05-23 22:09:31 +00:00
* @function Phaser.Curves.Line.fromJSON
2018-03-18 13:43:37 +00:00
* @since 3.0.0
*
2019-05-09 10:52:07 +00:00
* @param {Phaser.Types.Curves.JSONCurve} data - The JSON object containing this curve data.
2018-03-18 13:43:37 +00:00
*
2018-10-19 11:32:43 +00:00
* @return {Phaser.Curves.Line} A new LineCurve object.
2018-03-18 13:43:37 +00:00
*/
LineCurve.fromJSON = function (data)
{
var points = data.points;
var p0 = new Vector2(points[0], points[1]);
var p1 = new Vector2(points[2], points[3]);
return new LineCurve(p0, p1);
};
module.exports = LineCurve;