Question: I have a 3D rotation specified by three coordinates. How do I transform this into a CannonJS quaternion to physically rotate the body?
Repl link: https://replit.com/@JasonRoman/golf (see /game_defs.js line 66)
// I saw this online:
var qX = new CANNON.Quaternion()
var qY = new CANNON.Quaternion()
qX.setFromAxisAngle(new CANNON.Vec3(1,0,0), rx);
qY.setFromAxisAngle(new CANNON.Vec3(0,1,0), ry);
var combined = qX.mult(qY)
combined.normalize();
// So I tried this:
var qX = new CANNON.Quaternion()
var qY = new CANNON.Quaternion()
var qZ = new CANNON.Quaternion()
qX.setFromAxisAngle(new CANNON.Vec3(1,0,0), rx);
qY.setFromAxisAngle(new CANNON.Vec3(0,1,0), ry);
qZ.setFromAxisAngle(new CANNON.Vec3(0,0,1), rz);
var combined = qX.mult(qY).mult(qZ);
combined.normalize();
// But apparently that doesn't work because quaternion multiplication is not communative.
How can I combine my three qX
, qY
, and qZ
quaternions to form one big quaternion?
I have also tried doing this (using a three.js object) in an attempt to get three.js to convert my rotation for me:
three_object.rotation.set(new THREE.Vector3( rx, ry, rz ));
cannon_object.quaternion.copy(three_object.quaternion)
However this results in values of NaN for all four coordinates in the quaternion.