I have the following code that works fine for me, and it have been used succesfully multiple times in my application that I’m working on:
<code>function rotate(position, angle, axis) {
if (angle !== 0) {
const x = position.x;
const y = position.y;
const z = position.z;
const sin = Math.sin(angle);
const cos = Math.cos(angle);
if (axis === 'X') {
position.x = x * cos + z * sin;
position.z = -x * sin + z * cos;
} else if (axis === 'Y') {
position.y = y * cos - z * sin;
position.z = y * sin + z * cos;
} else if (axis === 'Z') {
position.x = x * cos - y * sin;
position.y = x * sin + y * cos;
}
}
return position;
}
function applyAngle(position, angles) {
position = rotate(position, angles.x, 'X');
position = rotate(position, angles.y, 'Y');
position = rotate(position, angles.z, 'Z');
return position;
}
</code>
<code>function rotate(position, angle, axis) {
if (angle !== 0) {
const x = position.x;
const y = position.y;
const z = position.z;
const sin = Math.sin(angle);
const cos = Math.cos(angle);
if (axis === 'X') {
position.x = x * cos + z * sin;
position.z = -x * sin + z * cos;
} else if (axis === 'Y') {
position.y = y * cos - z * sin;
position.z = y * sin + z * cos;
} else if (axis === 'Z') {
position.x = x * cos - y * sin;
position.y = x * sin + y * cos;
}
}
return position;
}
function applyAngle(position, angles) {
position = rotate(position, angles.x, 'X');
position = rotate(position, angles.y, 'Y');
position = rotate(position, angles.z, 'Z');
return position;
}
</code>
function rotate(position, angle, axis) {
if (angle !== 0) {
const x = position.x;
const y = position.y;
const z = position.z;
const sin = Math.sin(angle);
const cos = Math.cos(angle);
if (axis === 'X') {
position.x = x * cos + z * sin;
position.z = -x * sin + z * cos;
} else if (axis === 'Y') {
position.y = y * cos - z * sin;
position.z = y * sin + z * cos;
} else if (axis === 'Z') {
position.x = x * cos - y * sin;
position.y = x * sin + y * cos;
}
}
return position;
}
function applyAngle(position, angles) {
position = rotate(position, angles.x, 'X');
position = rotate(position, angles.y, 'Y');
position = rotate(position, angles.z, 'Z');
return position;
}
I want to create an “addAngles(a, b)” function where I can simply add angles together. I have written a unit test for this:
<code>const testAngleA = {
x: Math.random() * Math.PI * 2,
y: Math.random() * Math.PI * 2,
z: Math.random() * Math.PI * 2,
};
const testAngleB = {
x: Math.random() * Math.PI * 2,
y: Math.random() * Math.PI * 2,
z: Math.random() * Math.PI * 2,
};
const testAdditionResult = addAngles(testAngleA, testAngleB);
let testPositionA = {x: 0, y: 1, z: 0};
let testPositionB = {x: 0, y: 1, z: 0};
testPositionA = applyAngle(testPositionA, testAdditionResult);
testPositionB = applyAngle(testPositionB, testAngleA);
testPositionB = applyAngle(testPositionB, testAngleB);
let strA = JSON.stringify(testPositionA);
let strB = JSON.stringify(testPositionB);
if(strA === strB) {
console.log('succeed', strA, strB);
} else {
console.log('failed', strA, strB);
}
</code>
<code>const testAngleA = {
x: Math.random() * Math.PI * 2,
y: Math.random() * Math.PI * 2,
z: Math.random() * Math.PI * 2,
};
const testAngleB = {
x: Math.random() * Math.PI * 2,
y: Math.random() * Math.PI * 2,
z: Math.random() * Math.PI * 2,
};
const testAdditionResult = addAngles(testAngleA, testAngleB);
let testPositionA = {x: 0, y: 1, z: 0};
let testPositionB = {x: 0, y: 1, z: 0};
testPositionA = applyAngle(testPositionA, testAdditionResult);
testPositionB = applyAngle(testPositionB, testAngleA);
testPositionB = applyAngle(testPositionB, testAngleB);
let strA = JSON.stringify(testPositionA);
let strB = JSON.stringify(testPositionB);
if(strA === strB) {
console.log('succeed', strA, strB);
} else {
console.log('failed', strA, strB);
}
</code>
const testAngleA = {
x: Math.random() * Math.PI * 2,
y: Math.random() * Math.PI * 2,
z: Math.random() * Math.PI * 2,
};
const testAngleB = {
x: Math.random() * Math.PI * 2,
y: Math.random() * Math.PI * 2,
z: Math.random() * Math.PI * 2,
};
const testAdditionResult = addAngles(testAngleA, testAngleB);
let testPositionA = {x: 0, y: 1, z: 0};
let testPositionB = {x: 0, y: 1, z: 0};
testPositionA = applyAngle(testPositionA, testAdditionResult);
testPositionB = applyAngle(testPositionB, testAngleA);
testPositionB = applyAngle(testPositionB, testAngleB);
let strA = JSON.stringify(testPositionA);
let strB = JSON.stringify(testPositionB);
if(strA === strB) {
console.log('succeed', strA, strB);
} else {
console.log('failed', strA, strB);
}
I have tried a lot of things (Quaternion multiplication, matrix multiplication) but I can’t make it. Chat gpt, Geminy and Claude is also struggeling to solve it 🙂