I created a small tank game a year ago, as proof of concept using three.js and WebSockets. However in the later versions of Three.js that way to do quaternions has changed. I cannot find any example code of how to do it in the latest version. This is my old code:
function calculateQuaternions(tankmesh) {
var n = getLandscapeNormal(tankmesh);
var q1 = new THREE.Quaternion();
q1.setFromAxisAngle(new THREE.Vector3(0, 1, 0), tankmesh.rotation.y);
var q2 = new THREE.Quaternion();
var axis = new THREE.Vector3(0, 1, 0);
var theta = Math.acos(axis.dot(n));
axis.cross(n).normalize();
q2.setFromAxisAngle(axis, theta);
q2.multiply(q1);
tankmesh.quaternion = q2;
}
The getLandscapeNormal function returns a normalized vector indicating the direction the tank schould be tilted (calculated from the slope of the landscape at the tanks position). When the code worked, the tank moved just like in this video: https://www.youtube.com/watch?v=CMNllyqq_O0
Can anyone help me how to do this calculation now?
I found the answer. If anyone else have problems porting quarternions from older Three.JS to later, this is how it's done (version 81):
replace:
tankmesh.quaternion = q2;
with:
tankmesh.setRotationFromQuaternion(q2);