Geom.Mesh.RotateFace is a new function that will rotate a Face by a given amount, based on an optional center of rotation.

This commit is contained in:
Richard Davey 2020-10-09 11:53:57 +01:00
parent bf8cd60aab
commit f77a5de32c
2 changed files with 60 additions and 0 deletions

View file

@ -0,0 +1,59 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
/**
* Rotates the vertices of a Face to the given angle.
*
* The actual vertex positions are adjusted, not their transformed positions.
*
* Therefore, this updates the vertex data directly.
*
* @function Phaser.Geom.Mesh.RotateFace
* @since 3.50.0
*
* @param {Phaser.Geom.Mesh.Face} face - The Face to rotate.
* @param {number} angle - The angle to rotate to, in radians.
* @param {number} [cx] - An optional center of rotation. If not given, the Face in-center is used.
* @param {number} [cy] - An optional center of rotation. If not given, the Face in-center is used.
*/
var RotateFace = function (face, angle, cx, cy)
{
var x;
var y;
// No point of rotation? Use the inCenter instead, then.
if (cx === undefined && cy === undefined)
{
var inCenter = face.getInCenter();
x = inCenter.x;
y = inCenter.y;
}
var c = Math.cos(angle);
var s = Math.sin(angle);
var v1 = face.vertex1;
var v2 = face.vertex2;
var v3 = face.vertex3;
var tx = v1.x - x;
var ty = v1.y - y;
v1.set(tx * c - ty * s + x, tx * s + ty * c + y);
tx = v2.x - x;
ty = v2.y - y;
v2.set(tx * c - ty * s + x, tx * s + ty * c + y);
tx = v3.x - x;
ty = v3.y - y;
v3.set(tx * c - ty * s + x, tx * s + ty * c + y);
};
module.exports = RotateFace;

View file

@ -16,6 +16,7 @@ var Mesh = {
GenerateVerts: require('./GenerateVerts'),
ParseObj: require('./ParseObj'),
ParseObjMaterial: require('./ParseObjMaterial'),
RotateFace: require('./RotateFace'),
Vertex: require('./Vertex')
};