|
| 1 | +// Created for Phaser 3 |
| 2 | +// Curve class based work done in three.js by [zz85](http://www.lab4games.net/zz85/blog) |
| 3 | + |
| 4 | +var Class = require('../../../utils/Class'); |
| 5 | +var Curve = require('../Curve'); |
| 6 | +var Vector2 = require('../../../math/Vector2'); |
| 7 | + |
| 8 | +// Phaser.Curves.Hermite |
| 9 | + |
| 10 | +/** |
| 11 | +* A data representation of a Hermite Curve (see http://en.wikipedia.org/wiki/Cubic_Hermite_spline) |
| 12 | +* |
| 13 | +* A Hermite curve has a start and end point and tangent vectors for both of them. |
| 14 | +* The curve will always pass through the two control points and the shape of it is controlled |
| 15 | +* by the length and direction of the tangent vectors. At the control points the curve will |
| 16 | +* be facing exactly in the vector direction. |
| 17 | +* |
| 18 | +* As these curves change speed (speed = distance between points separated by an equal change in |
| 19 | +* 't' value - see Hermite.getPoint) this class attempts to reduce the variation by pre-calculating |
| 20 | +* the `accuracy` number of points on the curve. The straight-line distances to these points are stored |
| 21 | +* in the private 'points' array, and this information is used by Hermite.findT() to convert a pixel |
| 22 | +* distance along the curve into a 'time' value. |
| 23 | +* |
| 24 | +* Higher `accuracy` values will result in more even movement, but require more memory for the points |
| 25 | +* list. 5 works, but 10 seems to be an ideal value for the length of curves found in most games on |
| 26 | +* a desktop screen. If you use very long curves (more than 400 pixels) you may need to increase |
| 27 | +* this value further. |
| 28 | +* |
| 29 | +* @param {number} p1x - The x coordinate of the start of the curve. |
| 30 | +* @param {number} p1y - The y coordinate of the start of the curve. |
| 31 | +* @param {number} p2x - The x coordinate of the end of the curve. |
| 32 | +* @param {number} p2y - The y coordinate of the end of the curve. |
| 33 | +* @param {number} v1x - The x component of the tangent vector for the start of the curve. |
| 34 | +* @param {number} v1y - The y component of the tangent vector for the start of the curve. |
| 35 | +* @param {number} v2x - The x component of the tangent vector for the end of the curve. |
| 36 | +* @param {number} v2y - The y component of the tangent vector for the end of the curve. |
| 37 | +* @param {number} [accuracy=10] The amount of points to pre-calculate on the curve. |
| 38 | +*/ |
| 39 | +var HermiteCurve = new Class({ |
| 40 | + |
| 41 | + Extends: Curve, |
| 42 | + |
| 43 | + initialize: |
| 44 | + |
| 45 | + // p0 = start point |
| 46 | + // p1 = end point |
| 47 | + // v0 = start tangent point |
| 48 | + // v1 = end tangent point |
| 49 | + function HermiteCurve (p0, p1, v0, v1) |
| 50 | + { |
| 51 | + Curve.call(this); |
| 52 | + |
| 53 | + if (Array.isArray(p0)) |
| 54 | + { |
| 55 | + v1 = new Vector2(p0[6], p0[7]); |
| 56 | + v0 = new Vector2(p0[4], p0[5]); |
| 57 | + p1 = new Vector2(p0[2], p0[3]); |
| 58 | + p0 = new Vector2(p0[0], p0[1]); |
| 59 | + } |
| 60 | + |
| 61 | + this.p0 = p0; |
| 62 | + this.p1 = p1; |
| 63 | + this.v0 = v0; |
| 64 | + this.v1 = v1; |
| 65 | + }, |
| 66 | + |
| 67 | + getStartPoint: function (out) |
| 68 | + { |
| 69 | + if (out === undefined) { out = new Vector2(); } |
| 70 | + |
| 71 | + return out.copy(this.p0); |
| 72 | + }, |
| 73 | + |
| 74 | + getResolution: function (divisions) |
| 75 | + { |
| 76 | + return divisions; |
| 77 | + }, |
| 78 | + |
| 79 | + /** |
| 80 | + * Performs the curve calculations. |
| 81 | + * |
| 82 | + * This is called automatically if you change any of the curves public properties, such as `Hermite.p1x` or `Hermite.v2y`. |
| 83 | + * |
| 84 | + * If you adjust any of the internal private values, then call this to update the points. |
| 85 | + * |
| 86 | + * @method Phaser.Hermite#recalculate |
| 87 | + * @return {Phaser.Hermite} This object. |
| 88 | + */ |
| 89 | + recalculate: function () |
| 90 | + { |
| 91 | + this._ax = (2 * this._p1x - 2 * this._p2x + this._v1x + this._v2x); |
| 92 | + this._ay = (2 * this._p1y - 2 * this._p2y + this._v1y + this._v2y); |
| 93 | + this._bx = (-3 * this._p1x + 3 * this._p2x - 2 * this._v1x - this._v2x); |
| 94 | + this._by = (-3 * this._p1y + 3 * this._p2y - 2 * this._v1y - this._v2y); |
| 95 | + |
| 96 | + this.length = this.calculateEvenPoints(); |
| 97 | + |
| 98 | + return this; |
| 99 | + }, |
| 100 | + |
| 101 | + getPoint: function (t, out) |
| 102 | + { |
| 103 | + if (out === undefined) { out = new Vector2(); } |
| 104 | + |
| 105 | + var t2 = t * t; |
| 106 | + var t3 = t * t2; |
| 107 | + |
| 108 | + var ax = (2 * this.p0.x - 2 * this.p1.x + this.v0.x + this.v1.x); |
| 109 | + var ay = (2 * this.p0.y - 2 * this.p1.y + this.v0.y + this.v1.y); |
| 110 | + var bx = (-3 * this.p0.x + 3 * this.p1.x - 2 * this.v0.x - this.v1.x); |
| 111 | + var by = (-3 * this.p0.y + 3 * this.p1.y - 2 * this.v0.y - this.v1.y); |
| 112 | + |
| 113 | + out.x = t3 * ax + t2 * bx + t * this.v0.x + this.p0.x; |
| 114 | + out.y = t3 * ay + t2 * by + t * this.v0.y + this.p0.y; |
| 115 | + |
| 116 | + return out; |
| 117 | + }, |
| 118 | + |
| 119 | + // Given u ( 0 .. 1 ), get a t to find p. This gives you points which are equidistant |
| 120 | + |
| 121 | + getUtoTmapping: function (u, distance, divisions) |
| 122 | + { |
| 123 | + // Find the _points which bracket the distance value |
| 124 | + var ti = Math.floor(distance / this.length * divisions); |
| 125 | + |
| 126 | + while (ti > 0 && this._points[ti] > distance) |
| 127 | + { |
| 128 | + ti--; |
| 129 | + } |
| 130 | + |
| 131 | + while (ti < divisions && this._points[ti] < distance) |
| 132 | + { |
| 133 | + ti++; |
| 134 | + } |
| 135 | + |
| 136 | + // Linear interpolation to get a more accurate fix |
| 137 | + var dt = this._points[ti] - this._points[ti - 1]; |
| 138 | + var d = distance - this._points[ti - 1]; |
| 139 | + |
| 140 | + return ((ti - 1) / divisions) + d / (dt * divisions); |
| 141 | + }, |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | +}); |
| 146 | + |
| 147 | +module.exports = HermiteCurve; |
0 commit comments