Skip to content

mat3: Add fromEuler Function to Support Euler Angles to Matrix Conversion #462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions spec/gl-matrix/mat3-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@ describe("mat3", function() {
});
});

describe("fromEuler", function() {
beforeEach(function() {
let row = 0;
let pitch = 0;
let yaw = 0;
result = mat3.fromEuler(out, row, pitch, yaw);
});

it("should return out", function() { expect(result).toBe(out); });

it("should place a 3x3 identity matrix into out", function() {
expect(out).toBeEqualish(identity);
});
});

describe("fromMat4", function() {
beforeEach(function() {
result = mat3.fromMat4(out, [ 1, 2, 3, 4,
Expand Down
33 changes: 33 additions & 0 deletions src/mat3.js
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,39 @@ export function fromQuat(out, q) {
return out;
}

/**
* Calculates a 3x3 matrix from the given euler angles
*
* @param {mat3} out mat3 receiving operation result
* @param {Number} roll Roll angle to create matrix from
* @param {Number} pitch Pitch angle to create matrix from
* @param {Number} yaw Yaw angle to create matrix from
*
* @returns {mat3} out
*/
export function fromEuler(out, roll, pitch, yaw) {
const cp = Math.cos(pitch);
const sp = Math.sin(pitch);
const sr = Math.sin(roll);
const cr = Math.cos(roll);
const sy = Math.sin(yaw);
const cy = Math.cos(yaw);

out[0] = cp * cy;
out[1] = (sr * sp * cy) - (cr * sy);
out[2] = (cr * sp * cy) + (sr * sy);

out[3] = cp * sy;
out[4] = (sr * sp * sy) + (cr * cy);
out[5] = (cr * sp * sy) - (sr * cy);

out[6] = -sp;
out[7] = sr * cp;
out[8] = cr * cp;

return out;
}

/**
* Calculates a 3x3 normal matrix (transpose inverse) from the 4x4 matrix
*
Expand Down