Skip to content

Commit bec8345

Browse files
committed
Added Hermite class and functions.
1 parent 7338e54 commit bec8345

12 files changed

Lines changed: 658 additions & 1 deletion

File tree

v3/src/checksum.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
var CHECKSUM = {
2-
build: '528a0210-d341-11e6-a2a2-8fbbe27dda30'
2+
build: '20b3f530-d35e-11e6-b3f2-61f4b6b8c9a3'
33
};
44
module.exports = CHECKSUM;

v3/src/geom/hermite/FindT.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Convert a distance along this curve into a `time` value which will be between 0 and 1.
3+
*
4+
* For example if this curve has a length of 100 pixels then `findT(50)` would return `0.5`.
5+
*
6+
* @method Phaser.Hermite#findT
7+
* @param {integer} distance - The distance into the curve in pixels. Should be a positive integer.
8+
* @return {number} The time (`t`) value, a float between 0 and 1.
9+
*/
10+
var FindT = function (curve, distance)
11+
{
12+
if (distance <= 0)
13+
{
14+
return 0;
15+
}
16+
17+
// Find the _points which bracket the distance value
18+
var ti = Math.floor(distance / curve.length * curve._accuracy);
19+
20+
while (ti > 0 && curve._points[ti] > distance)
21+
{
22+
ti--;
23+
}
24+
25+
while (ti < curve._accuracy && curve._points[ti] < distance)
26+
{
27+
ti++;
28+
}
29+
30+
// Linear interpolation to get a more accurate fix
31+
var dt = curve._points[ti] - curve._points[ti - 1];
32+
var d = distance - curve._points[ti - 1];
33+
34+
return ((ti - 1) / curve._accuracy) + d / (dt * curve._accuracy);
35+
};
36+
37+
module.exports = FindT;

v3/src/geom/hermite/GetAngle.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var GetPoint = require('./GetPoint');
2+
3+
/**
4+
* Calculate and return the angle, in radians, of the curves tangent based on time.
5+
*
6+
* @method Phaser.Hermite#getAngle
7+
* @param {number} [t=0] - The `t` (time) value at which to find the angle. Must be between 0 and 1.
8+
* @return {number} The angle of the line at the specified `t` time value along the curve. The value is in radians.
9+
*/
10+
var GetAngle = function (curve, t)
11+
{
12+
if (t === undefined) { t = 0; }
13+
14+
GetPoint(curve, t - 0.01, curve._temp1);
15+
GetPoint(curve, t + 0.01, curve._temp2);
16+
17+
return Math.atan2(curve._temp2.y - curve._temp1.y, curve._temp2.x - curve._temp1.x);
18+
};
19+
20+
module.exports = GetAngle;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var GetAngle = require('./GetAngle');
2+
var FindT = require('./FindT');
3+
4+
/**
5+
* Calculate and return the angle, in radians, of the curves tangent at the given pixel distance along the curves length.
6+
*
7+
* @method Phaser.Hermite#getAngleWithDistance
8+
* @param {number} [distance=0] - The distance along the curve to get the angle from, in pixels.
9+
* @return {number} The angle of the line at the specified distance along the curve. The value is in radians.
10+
*/
11+
var GetAngleWithDistance = function (curve, distance)
12+
{
13+
if (distance === undefined) { distance = 0; }
14+
15+
if (distance <= 0)
16+
{
17+
return Math.atan2(this._v1y, this._v1x);
18+
}
19+
else
20+
{
21+
return GetAngle(curve, FindT(curve, distance));
22+
}
23+
};
24+
25+
module.exports = GetAngleWithDistance;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Get the angle of the curves entry point.
3+
*
4+
* @method Phaser.Hermite#getEntryTangent
5+
* @param {Phaser.Point|Object} point - The Phaser.Point object, or an Object with public `x` and `y` properties, in which the tangent vector values will be stored.
6+
* @return {Phaser.Point} A Point object containing the tangent vector of this Hermite curve.
7+
*/
8+
var GetEntryTangent = function (curve, point)
9+
{
10+
point.x = curve._v1x;
11+
point.y = curve._v1y;
12+
13+
return point;
14+
};
15+
16+
module.exports = GetEntryTangent;

v3/src/geom/hermite/GetPoint.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
var Point = require('../point/Point');
2+
3+
/**
4+
* Get a point on the curve using the `t` (time) value, which must be between 0 and 1.
5+
*
6+
* @method Phaser.Hermite#getPoint
7+
* @param {number} [t=0] - The time value along the curve from which to extract a point. This is a value between 0 and 1, where 0 represents the start of the curve and 1 the end.
8+
* @param {Phaser.Point|Object} [point] - An optional Phaser.Point, or Object containing public `x` and `y` properties. If given the resulting values will be stored in the Objects `x` and `y` properties. If omitted a new Phaser.Point object is created.
9+
* @return {Phaser.Point} An Object with the x, y coordinate of the curve at the specified `t` value set in its `x` and `y` properties.
10+
*/
11+
var GetPoint = function (curve, t, out)
12+
{
13+
if (t === undefined) { t = 0; }
14+
if (out === undefined) { out = new Point(); }
15+
16+
if (t < 0)
17+
{
18+
t = 0;
19+
}
20+
21+
if (t > 1)
22+
{
23+
t = 1;
24+
}
25+
26+
var t2 = t * t;
27+
var t3 = t * t2;
28+
29+
out.x = t3 * curve._ax + t2 * curve._bx + t * curve._v1x + curve._p1x;
30+
out.y = t3 * curve._ay + t2 * curve._by + t * curve._v1y + curve._p1y;
31+
32+
return out;
33+
};
34+
35+
module.exports = GetPoint;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
var Point = require('../point/Point');
2+
var GetPoint = require('./GetPoint');
3+
var FindT = require('./FindT');
4+
5+
/**
6+
* Get a point on the curve using the distance, in pixels, along the curve.
7+
*
8+
* @method Phaser.Hermite#getPointWithDistance
9+
* @param {integer} [distance=0] - The distance along the curve to get the point from, given in pixels.
10+
* @param {Phaser.Point|Object} [point] - An optional Phaser.Point, or Object containing public `x` and `y` properties. If given the resulting values will be stored in the Objects `x` and `y` properties. If omitted a new Phaser.Point object is created.
11+
* @return {Phaser.Point} The point on the line at the specified 'distance' along the curve.
12+
*/
13+
var GetPointWithDistance = function (curve, distance, out)
14+
{
15+
if (distance === undefined) { distance = 0; }
16+
if (out === undefined) { out = new Point(); }
17+
18+
if (distance <= 0)
19+
{
20+
out.x = this._p1x;
21+
out.y = this._p1y;
22+
}
23+
else
24+
{
25+
GetPoint(curve, FindT(curve, distance), out);
26+
}
27+
28+
return out;
29+
};
30+
31+
module.exports = GetPointWithDistance;

v3/src/geom/hermite/GetX.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Get the X component of a point on the curve based on the `t` (time) value, which must be between 0 and 1.
3+
*
4+
* @method Phaser.Hermite#getX
5+
* @param {number} [t=0] - The time value along the curve from which to extract a point. This is a value between 0 and 1, where 0 represents the start of the curve and 1 the end.
6+
* @return {number} The X component of a point on the curve based on the `t` (time) value.
7+
*/
8+
var GetX = function (curve, t)
9+
{
10+
if (t === undefined)
11+
{
12+
t = 0;
13+
}
14+
else
15+
{
16+
if (t < 0)
17+
{
18+
t = 0;
19+
}
20+
21+
if (t > 1)
22+
{
23+
t = 1;
24+
}
25+
}
26+
27+
var t2 = t * t;
28+
var t3 = t * t2;
29+
30+
return (t3 * curve._ax + t2 * curve._bx + t * curve._v1x + curve._p1x);
31+
};
32+
33+
module.exports = GetX;

v3/src/geom/hermite/GetY.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Get the Y component of a point on the curve based on the `t` (time) value, which must be between 0 and 1.
3+
*
4+
* @method Phaser.Hermite#getY
5+
* @param {number} [t=0] - The time value along the curve from which to extract a point. This is a value between 0 and 1, where 0 represents the start of the curve and 1 the end.
6+
* @return {number} The Y component of a point on the curve based on the `t` (time) value.
7+
*/
8+
var GetY = function (curve, t)
9+
{
10+
if (t === undefined)
11+
{
12+
t = 0;
13+
}
14+
else
15+
{
16+
if (t < 0)
17+
{
18+
t = 0;
19+
}
20+
21+
if (t > 1)
22+
{
23+
t = 1;
24+
}
25+
}
26+
27+
var t2 = t * t;
28+
var t3 = t * t2;
29+
30+
return (t3 * curve._ay + t2 * curve._by + t * curve._v1y + curve._p1y);
31+
};
32+
33+
module.exports = GetY;

0 commit comments

Comments
 (0)