phaser/v3/src/paths/curves/ellipse/EllipseCurve.js

240 lines
5.2 KiB
JavaScript
Raw Normal View History

// Based on the three.js Curve classes created by [zz85](http://www.lab4games.net/zz85/blog)
var Class = require('../../../utils/Class');
var Curve = require('../Curve');
2017-09-21 02:07:42 +00:00
var DegToRad = require('../../../math/DegToRad');
var RadToDeg = require('../../../math/RadToDeg');
2017-09-21 02:07:42 +00:00
var Vector2 = require('../../../math/Vector2');
2017-09-21 01:30:54 +00:00
// Phaser.Curves.Ellipse
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-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
Curve.call(this);
this.p0 = new Vector2(x, y);
this._xRadius = xRadius;
this._yRadius = yRadius;
2017-09-21 02:07:42 +00:00
// Radians
this._startAngle = DegToRad(startAngle);
this._endAngle = DegToRad(endAngle);
2017-09-21 02:07:42 +00:00
// Boolean (anti-clockwise direction)
this._clockwise = clockwise;
2017-09-21 02:07:42 +00:00
// The rotation of the arc
this._rotation = DegToRad(rotation);
this._startPoint = this.getPoint(0);
},
getStartPoint: function (out)
{
if (out === undefined) { out = new Vector2(); }
return out.copy(this._startPoint);
},
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-21 02:07:42 +00:00
if (out === undefined) { out = new Vector2(); }
var twoPi = Math.PI * 2;
var deltaAngle = this._endAngle - this._startAngle;
var samePoints = Math.abs(deltaAngle) < Number.EPSILON;
// 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;
}
}
if (this._clockwise && !samePoints)
{
if (deltaAngle === twoPi)
{
deltaAngle = - twoPi;
}
else
{
deltaAngle = deltaAngle - twoPi;
}
}
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);
if (this._rotation !== 0)
{
var cos = Math.cos(this._rotation);
var sin = Math.sin(this._rotation);
var tx = x - this.p0.x;
var ty = y - this.p0.y;
// Rotate the point about the center of the ellipse.
x = tx * cos - ty * sin + this.p0.x;
y = tx * sin + ty * cos + this.p0.y;
}
2017-09-21 02:07:42 +00:00
return out.set(x, y);
},
toJSON: function ()
{
return {
type: 'EllipseCurve',
x: this.p0.x,
y: this.p0.y,
xRadius: this._xRadius,
yRadius: this._yRadius,
startAngle: RadToDeg(this._startAngle),
endAngle: RadToDeg(this._endAngle),
clockwise: this._clockwise,
rotation: RadToDeg(this._rotation)
};
},
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);
}
}
});
module.exports = EllipseCurve;