Skip to content

Commit b28962e

Browse files
committed
Split Curve up into includes and tidied up the namespace so it all now lives under Phaser.Curves.
1 parent 1882ee8 commit b28962e

52 files changed

Lines changed: 1074 additions & 35 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// Based on the three.js Curve classes created by [zz85](http://www.lab4games.net/zz85/blog)
2+
3+
var Class = require('../../utils/Class');
4+
var CubicBezier = require('../../math/interpolation/CubicBezierInterpolation');
5+
var Curve = require('../curve/Curve');
6+
var Vector2 = require('../../math/Vector2');
7+
8+
// Phaser.Curves.CubicBezier
9+
10+
var CubicBezierCurve = new Class({
11+
12+
Extends: Curve,
13+
14+
initialize:
15+
16+
// p0 = start point (or an array of point pairs)
17+
// p1 = control point 1
18+
// p2 = control point 2
19+
// p3 = end point
20+
function CubicBezierCurve (p0, p1, p2, p3)
21+
{
22+
Curve.call(this, 'CubicBezierCurve');
23+
24+
if (Array.isArray(p0))
25+
{
26+
p3 = new Vector2(p0[6], p0[7]);
27+
p2 = new Vector2(p0[4], p0[5]);
28+
p1 = new Vector2(p0[2], p0[3]);
29+
p0 = new Vector2(p0[0], p0[1]);
30+
}
31+
32+
this.p0 = p0;
33+
this.p1 = p1;
34+
this.p2 = p2;
35+
this.p3 = p3;
36+
},
37+
38+
getStartPoint: function (out)
39+
{
40+
if (out === undefined) { out = new Vector2(); }
41+
42+
return out.copy(this.p0);
43+
},
44+
45+
getResolution: function (divisions)
46+
{
47+
return divisions;
48+
},
49+
50+
getPoint: function (t, out)
51+
{
52+
if (out === undefined) { out = new Vector2(); }
53+
54+
var p0 = this.p0;
55+
var p1 = this.p1;
56+
var p2 = this.p2;
57+
var p3 = this.p3;
58+
59+
return out.set(CubicBezier(t, p0.x, p1.x, p2.x, p3.x), CubicBezier(t, p0.y, p1.y, p2.y, p3.y));
60+
},
61+
62+
draw: function (graphics, pointsTotal)
63+
{
64+
if (pointsTotal === undefined) { pointsTotal = 32; }
65+
66+
var points = this.getPoints(pointsTotal);
67+
68+
graphics.beginPath();
69+
graphics.moveTo(this.p0.x, this.p0.y);
70+
71+
for (var i = 1; i < points.length; i++)
72+
{
73+
graphics.lineTo(points[i].x, points[i].y);
74+
}
75+
76+
graphics.strokePath();
77+
graphics.closePath();
78+
79+
// So you can chain graphics calls
80+
return graphics;
81+
},
82+
83+
toJSON: function ()
84+
{
85+
return {
86+
type: this.type,
87+
points: [
88+
this.p0.x, this.p0.y,
89+
this.p1.x, this.p1.y,
90+
this.p2.x, this.p2.y,
91+
this.p3.x, this.p3.y
92+
]
93+
};
94+
}
95+
96+
});
97+
98+
CubicBezierCurve.fromJSON = function (data)
99+
{
100+
var points = data.points;
101+
102+
var p0 = new Vector2(points[0], points[1]);
103+
var p1 = new Vector2(points[2], points[3]);
104+
var p2 = new Vector2(points[4], points[5]);
105+
var p3 = new Vector2(points[6], points[7]);
106+
107+
return new CubicBezierCurve(p0, p1, p2, p3);
108+
};
109+
110+
module.exports = CubicBezierCurve;

v3/src/curves/curve/Curve.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Based on the three.js Curve classes created by [zz85](http://www.lab4games.net/zz85/blog)
2+
3+
var Class = require('../../utils/Class');
4+
var Vector2 = require('../../math/Vector2');
5+
6+
// Our Base Curve which all other curves extend
7+
8+
var Curve = new Class({
9+
10+
initialize:
11+
12+
function Curve (type)
13+
{
14+
// String based identifier
15+
this.type = type;
16+
17+
this.defaultDivisions = 5;
18+
19+
this.arcLengthDivisions = 100;
20+
21+
this.cacheArcLengths = [];
22+
23+
this.needsUpdate = true;
24+
25+
this.active = true;
26+
27+
this._tmpVec2A = new Vector2();
28+
this._tmpVec2B = new Vector2();
29+
},
30+
31+
draw: require('./inc/Draw'),
32+
getBounds: require('./inc/GetBounds'),
33+
getDistancePoints: require('./inc/GetDistancePoints'),
34+
getEndPoint: require('./inc/GetEndPoint'),
35+
getLength: require('./inc/GetLength'),
36+
getLengths: require('./inc/GetLengths'),
37+
getPointAt: require('./inc/GetPointAt'),
38+
getPoints: require('./inc/GetPoints'),
39+
getSpacedPoints: require('./inc/GetSpacedPoints'),
40+
getStartPoint: require('./inc/GetStartPoint'),
41+
getTangent: require('./inc/GetTangent'),
42+
getTangentAt: require('./inc/GetTangentAt'),
43+
getTFromDistance: require('./inc/GetTFromDistance'),
44+
getUtoTmapping: require('./inc/GetUToTMapping'),
45+
updateArcLengths: require('./inc/UpdateArcLengths')
46+
47+
});
48+
49+
module.exports = Curve;

v3/src/curves/curve/inc/Draw.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var Draw = function (graphics, pointsTotal)
2+
{
3+
if (pointsTotal === undefined) { pointsTotal = 32; }
4+
5+
var start = this.getStartPoint();
6+
var points = this.getPoints(pointsTotal);
7+
8+
graphics.beginPath();
9+
10+
graphics.moveTo(start.x, start.y);
11+
12+
for (var i = 1; i < points.length; i++)
13+
{
14+
graphics.lineTo(points[i].x, points[i].y);
15+
}
16+
17+
graphics.strokePath();
18+
graphics.closePath();
19+
20+
// So you can chain graphics calls
21+
return graphics;
22+
};
23+
24+
module.exports = Draw;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var FromPoints = require('../../../geom/rectangle/FromPoints');
2+
var Rectangle = require('../../../geom/rectangle/Rectangle');
3+
4+
var GetBounds = function (out, accuracy)
5+
{
6+
if (out === undefined) { out = new Rectangle(); }
7+
if (accuracy === undefined) { accuracy = 16; }
8+
9+
var len = this.getLength();
10+
11+
if (accuracy > len)
12+
{
13+
accuracy = len / 2;
14+
}
15+
16+
// The length of the curve in pixels
17+
// So we'll have 1 spaced point per 'accuracy' pixels
18+
19+
var spaced = Math.max(1, Math.round(len / accuracy));
20+
21+
return FromPoints(this.getSpacedPoints(spaced), out);
22+
};
23+
24+
module.exports = GetBounds;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Return an array of points, spaced out X distance pixels apart
2+
var GetDistancePoints = function (distance)
3+
{
4+
var len = this.getLength();
5+
6+
var spaced = Math.max(1, len / distance);
7+
8+
return this.getSpacedPoints(spaced);
9+
10+
// Get the t value for 200 pixels along the curve
11+
// var t = curve.getTFromDistance(200);
12+
// = this.getUtoTmapping(0, distance, divisions)
13+
14+
// Get the point at t
15+
// var p = curve.getPoint(t);
16+
};
17+
18+
module.exports = GetDistancePoints;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var Vector2 = require('../../../math/Vector2');
2+
3+
var GetEndPoint = function (out)
4+
{
5+
if (out === undefined) { out = new Vector2(); }
6+
7+
return this.getPointAt(1, out);
8+
};
9+
10+
module.exports = GetEndPoint;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Get total curve arc length
2+
3+
var GetLength = function ()
4+
{
5+
var lengths = this.getLengths();
6+
7+
return lengths[lengths.length - 1];
8+
};
9+
10+
module.exports = GetLength;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Get list of cumulative segment lengths
2+
3+
var GetLengths = function (divisions)
4+
{
5+
if (divisions === undefined) { divisions = this.arcLengthDivisions; }
6+
7+
if ((this.cacheArcLengths.length === divisions + 1) && !this.needsUpdate)
8+
{
9+
return this.cacheArcLengths;
10+
}
11+
12+
this.needsUpdate = false;
13+
14+
var cache = [];
15+
var current;
16+
var last = this.getPoint(0, this._tmpVec2A);
17+
var sum = 0;
18+
19+
cache.push(0);
20+
21+
for (var p = 1; p <= divisions; p++)
22+
{
23+
current = this.getPoint(p / divisions, this._tmpVec2B);
24+
25+
sum += current.distance(last);
26+
27+
cache.push(sum);
28+
29+
last.copy(current);
30+
}
31+
32+
this.cacheArcLengths = cache;
33+
34+
return cache; // { sums: cache, sum:sum }; Sum is in the last element.
35+
};
36+
37+
module.exports = GetLengths;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Get point at relative position in curve according to arc length
2+
3+
// - u [0 .. 1]
4+
5+
var GetPointAt = function (u, out)
6+
{
7+
var t = this.getUtoTmapping(u);
8+
9+
return this.getPoint(t, out);
10+
};
11+
12+
module.exports = GetPointAt;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Get sequence of points using getPoint( t )
2+
3+
var GetPoints = function (divisions)
4+
{
5+
if (divisions === undefined) { divisions = this.defaultDivisions; }
6+
7+
var points = [];
8+
9+
for (var d = 0; d <= divisions; d++)
10+
{
11+
points.push(this.getPoint(d / divisions));
12+
}
13+
14+
return points;
15+
};
16+
17+
module.exports = GetPoints;

0 commit comments

Comments
 (0)