i want to make quaternion or euler that beforeVector rotate to AfterVector
so, i make a function that create euler with two Vectors.
but it doesn’t work properly
What can I fix here? or Any other solution?
function getEulerYZX(prevVector, nextVector) {
prevVector = prevVector.clone().normalize();
nextVector = nextVector.clone().normalize();
const yRad = Math.atan2(
nextVector.x - prevVector.x,
nextVector.z - prevVector.z
); //zx 평면 회전
const yRotatedVector = prevVector.clone();
yRotatedVector.z =
prevVector.z * Math.cos(yRad) - prevVector.x * Math.sin(yRad);
yRotatedVector.x =
prevVector.z * Math.sin(yRad) + prevVector.x * Math.cos(yRad);
const zRad = Math.atan2(
nextVector.y - yRotatedVector.y,
nextVector.x - yRotatedVector.x
);
const yzRotatedVector = yRotatedVector.clone();
yzRotatedVector.x =
yRotatedVector.x * Math.cos(zRad) - yRotatedVector.y * Math.sin(zRad);
yzRotatedVector.y =
yRotatedVector.x * Math.sin(zRad) + yRotatedVector.y * Math.cos(zRad);
const xRad = Math.atan2(
nextVector.z - yzRotatedVector.z,
nextVector.y - yzRotatedVector.y
);
const yzxRotatedVector = yzRotatedVector.clone();
yzxRotatedVector.y =
yzRotatedVector.z * Math.cos(xRad) - yzRotatedVector.y * Math.sin(xRad);
yzxRotatedVector.z =
yzRotatedVector.z * Math.sin(xRad) + yzRotatedVector.y * Math.cos(xRad);
return new THREE.Euler(xRad, yRad, zRad, "YZX");
}