Skip to content

Commit cf4b97c

Browse files
committed
Added SplineCurve
1 parent b63c8ad commit cf4b97c

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

v3/src/paths/curves/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ module.exports = {
44

55
CubicBezier: require('./cubicbezier/CubicBezierCurve'),
66
Ellipse: require('./ellipse/EllipseCurve'),
7-
Line: require('./line/LineCurve')
7+
Line: require('./line/LineCurve'),
8+
Spline: require('./spline/SplineCurve')
89

910
};
1011

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Based on the three.js Curve classes created by [zz85](http://www.lab4games.net/zz85/blog)
2+
3+
var CatmullRom = require('../../../math/CatmullRom');
4+
var Class = require('../../../utils/Class');
5+
var Curve = require('../Curve');
6+
var Vector2 = require('../../../math/Vector2');
7+
8+
// Phaser.Curves.Spline
9+
10+
var SplineCurve = new Class({
11+
12+
Extends: Curve,
13+
14+
initialize:
15+
16+
// Array of vec2s
17+
function SplineCurve (points)
18+
{
19+
if (points === undefined) { points = []; }
20+
21+
Curve.call(this);
22+
23+
this.points = points;
24+
},
25+
26+
getPoint: function (t, out)
27+
{
28+
if (out === undefined) { out = new Vector2(); }
29+
30+
var points = this.points;
31+
32+
var point = (points.length - 1) * t;
33+
34+
var intPoint = Math.floor(point);
35+
36+
var weight = point - intPoint;
37+
38+
var p0 = points[ (intPoint === 0) ? intPoint : intPoint - 1 ];
39+
var p1 = points[ intPoint ];
40+
var p2 = points[ (intPoint > points.length - 2) ? points.length - 1 : intPoint + 1 ];
41+
var p3 = points[ (intPoint > points.length - 3) ? points.length - 1 : intPoint + 2 ];
42+
43+
return out.set(CatmullRom(weight, p0.x, p1.x, p2.x, p3.x), CatmullRom(weight, p0.y, p1.y, p2.y, p3.y));
44+
}
45+
46+
});
47+
48+
module.exports = SplineCurve;

0 commit comments

Comments
 (0)