2017-09-20 22:10:37 +00:00
|
|
|
// Based on the three.js Curve classes created by [zz85](http://www.lab4games.net/zz85/blog)
|
|
|
|
|
|
|
|
var Curve = require('../Curve');
|
|
|
|
var Class = require('../../../utils/Class');
|
2017-09-21 02:07:42 +00:00
|
|
|
var DegToRad = require('../../../math/DegToRad');
|
|
|
|
var Vector2 = require('../../../math/Vector2');
|
2017-09-20 22:10:37 +00:00
|
|
|
|
2017-09-21 01:30:54 +00:00
|
|
|
// Phaser.Curves.Ellipse
|
2017-09-20 22:10:37 +00:00
|
|
|
|
|
|
|
var EllipseCurve = new Class({
|
|
|
|
|
|
|
|
Extends: Curve,
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
2017-09-22 00:34:39 +00:00
|
|
|
function EllipseCurve (x, y, xRadius, yRadius, startAngle, endAngle, clockwise, rotation)
|
2017-09-20 22:10:37 +00:00
|
|
|
{
|
2017-09-21 02:07:42 +00:00
|
|
|
if (yRadius === undefined) { yRadius = xRadius; }
|
2017-09-22 00:34:39 +00:00
|
|
|
if (startAngle === undefined) { startAngle = 0; }
|
|
|
|
if (endAngle === undefined) { endAngle = 360; }
|
|
|
|
if (clockwise === undefined) { clockwise = false; }
|
|
|
|
if (rotation === undefined) { rotation = 0; }
|
2017-09-21 02:07:42 +00:00
|
|
|
|
2017-09-21 00:19:27 +00:00
|
|
|
Curve.call(this);
|
|
|
|
|
2017-09-22 15:41:11 +00:00
|
|
|
this.p0 = new Vector2(x, y);
|
2017-09-20 22:10:37 +00:00
|
|
|
|
2017-09-22 15:41:11 +00:00
|
|
|
this._xRadius = xRadius;
|
|
|
|
this._yRadius = yRadius;
|
2017-09-20 22:10:37 +00:00
|
|
|
|
2017-09-21 02:07:42 +00:00
|
|
|
// Radians
|
2017-09-22 15:41:11 +00:00
|
|
|
this._startAngle = DegToRad(startAngle);
|
|
|
|
this._endAngle = DegToRad(endAngle);
|
2017-09-20 22:10:37 +00:00
|
|
|
|
2017-09-21 02:07:42 +00:00
|
|
|
// Boolean (anti-clockwise direction)
|
2017-09-22 15:41:11 +00:00
|
|
|
this._clockwise = clockwise;
|
2017-09-20 22:10:37 +00:00
|
|
|
|
2017-09-21 02:07:42 +00:00
|
|
|
// The rotation of the arc
|
2017-09-22 15:41:11 +00:00
|
|
|
this._rotation = DegToRad(rotation);
|
|
|
|
|
|
|
|
this._startPoint = this.getPoint(0);
|
|
|
|
},
|
|
|
|
|
|
|
|
getStartPoint: function ()
|
|
|
|
{
|
|
|
|
return this._startPoint;
|
2017-09-20 22:10:37 +00:00
|
|
|
},
|
|
|
|
|
2017-09-21 16:12:16 +00:00
|
|
|
getResolution: function (divisions)
|
|
|
|
{
|
|
|
|
return divisions * 2;
|
|
|
|
},
|
|
|
|
|
2017-09-21 02:07:42 +00:00
|
|
|
getPoint: function (t, out)
|
2017-09-20 22:10:37 +00:00
|
|
|
{
|
2017-09-21 02:07:42 +00:00
|
|
|
if (out === undefined) { out = new Vector2(); }
|
|
|
|
|
2017-09-20 22:10:37 +00:00
|
|
|
var twoPi = Math.PI * 2;
|
2017-09-22 15:41:11 +00:00
|
|
|
var deltaAngle = this._endAngle - this._startAngle;
|
|
|
|
var samePoints = Math.abs(deltaAngle) < Number.EPSILON;
|
2017-09-20 22:10:37 +00:00
|
|
|
|
|
|
|
// ensures that deltaAngle is 0 .. 2 PI
|
|
|
|
while (deltaAngle < 0)
|
|
|
|
{
|
|
|
|
deltaAngle += twoPi;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (deltaAngle > twoPi)
|
|
|
|
{
|
|
|
|
deltaAngle -= twoPi;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (deltaAngle < Number.EPSILON)
|
|
|
|
{
|
|
|
|
if (samePoints)
|
|
|
|
{
|
|
|
|
deltaAngle = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
deltaAngle = twoPi;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-22 15:41:11 +00:00
|
|
|
if (this._clockwise && !samePoints)
|
2017-09-20 22:10:37 +00:00
|
|
|
{
|
|
|
|
if (deltaAngle === twoPi)
|
|
|
|
{
|
|
|
|
deltaAngle = - twoPi;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
deltaAngle = deltaAngle - twoPi;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-22 15:41:11 +00:00
|
|
|
var angle = this._startAngle + t * deltaAngle;
|
|
|
|
var x = this.p0.x + this._xRadius * Math.cos(angle);
|
|
|
|
var y = this.p0.y + this._yRadius * Math.sin(angle);
|
2017-09-20 22:10:37 +00:00
|
|
|
|
2017-09-22 15:41:11 +00:00
|
|
|
if (this._rotation !== 0)
|
2017-09-20 22:10:37 +00:00
|
|
|
{
|
2017-09-22 15:41:11 +00:00
|
|
|
var cos = Math.cos(this._rotation);
|
|
|
|
var sin = Math.sin(this._rotation);
|
2017-09-20 22:10:37 +00:00
|
|
|
|
2017-09-22 15:41:11 +00:00
|
|
|
var tx = x - this.p0.x;
|
|
|
|
var ty = y - this.p0.y;
|
2017-09-20 22:10:37 +00:00
|
|
|
|
|
|
|
// Rotate the point about the center of the ellipse.
|
2017-09-22 15:41:11 +00:00
|
|
|
x = tx * cos - ty * sin + this.p0.x;
|
|
|
|
y = tx * sin + ty * cos + this.p0.y;
|
2017-09-20 22:10:37 +00:00
|
|
|
}
|
|
|
|
|
2017-09-21 02:07:42 +00:00
|
|
|
return out.set(x, y);
|
2017-09-22 15:41:11 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
x: {
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this.p0.x;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
|
|
|
this.p0.x = value;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
y: {
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this.p0.y;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
|
|
|
this.p0.y = value;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
xRadius: {
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this._xRadius;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
|
|
|
this._xRadius = value;
|
|
|
|
this.getPoint(0, this._startPoint);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
yRadius: {
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this._yRadius;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
|
|
|
this._yRadius = value;
|
|
|
|
this.getPoint(0, this._startPoint);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
startAngle: {
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this._startAngle;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
|
|
|
this._startAngle = DegToRad(value);
|
|
|
|
this.getPoint(0, this._startPoint);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
endAngle: {
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this._endAngle;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
|
|
|
this._endAngle = DegToRad(value);
|
|
|
|
this.getPoint(0, this._startPoint);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
clockwise: {
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this._clockwise;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
|
|
|
this._clockwise = value;
|
|
|
|
this.getPoint(0, this._startPoint);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
rotation: {
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this._rotation;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
|
|
|
this._rotation = DegToRad(value);
|
|
|
|
this.getPoint(0, this._startPoint);
|
|
|
|
}
|
2017-09-20 22:10:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = EllipseCurve;
|