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

114 lines
2.8 KiB
JavaScript
Raw Normal View History

// 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-21 01:30:54 +00:00
// Phaser.Curves.Ellipse
var EllipseCurve = new Class({
Extends: Curve,
initialize:
function EllipseCurve (aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation)
{
2017-09-21 02:07:42 +00:00
if (yRadius === undefined) { yRadius = xRadius; }
if (aStartAngle === undefined) { aStartAngle = 0; }
if (aEndAngle === undefined) { aEndAngle = 360; }
if (aClockwise === undefined) { aClockwise = false; }
if (aRotation === undefined) { aRotation = 0; }
Curve.call(this);
this.aX = aX;
this.aY = aY;
this.xRadius = xRadius;
this.yRadius = yRadius;
2017-09-21 02:07:42 +00:00
// Radians
this.aStartAngle = DegToRad(aStartAngle);
this.aEndAngle = DegToRad(aEndAngle);
2017-09-21 02:07:42 +00:00
// Boolean (anti-clockwise direction)
this.aClockwise = aClockwise;
2017-09-21 02:07:42 +00:00
// The rotation of the arc
this.aRotation = DegToRad(aRotation);
},
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.aEndAngle - this.aStartAngle;
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;
}
}
2017-09-21 02:07:42 +00:00
if (this.aClockwise && ! samePoints)
{
if (deltaAngle === twoPi)
{
deltaAngle = - twoPi;
}
else
{
deltaAngle = deltaAngle - twoPi;
}
}
var angle = this.aStartAngle + t * deltaAngle;
var x = this.aX + this.xRadius * Math.cos(angle);
var y = this.aY + this.yRadius * Math.sin(angle);
if (this.aRotation !== 0)
{
var cos = Math.cos(this.aRotation);
var sin = Math.sin(this.aRotation);
var tx = x - this.aX;
var ty = y - this.aY;
// Rotate the point about the center of the ellipse.
x = tx * cos - ty * sin + this.aX;
y = tx * sin + ty * cos + this.aY;
}
2017-09-21 02:07:42 +00:00
return out.set(x, y);
}
});
module.exports = EllipseCurve;