I've recently started working with this library and I've run into a persistent issue that has been quite challenging for me.
My current setup involves two cubes, one utilizing physi.js and the other using three.js. I have a function in place to rotate these cubes when certain keys (A, S, D, or W) are pressed based on the camera's rotation. Here is a snippet of my code:
var v = new THREE.Vector3(0, 0, 0);
if (keys.forward === 1)
v.x = -1;
if (keys.right === 1)
v.z = 1;
if (keys.backward === 1)
v.x = 1;
if (keys.left === 1)
v.z = -1;
//logic to obtain desired angle
var angle = camera.rotation.y + Math.atan2(v.x, v.z);
var rot = normalMesh.rotation.y;
var diff = angle - rot;
if (Math.abs(diff) > Math.PI) {
//determine the shortest path for rotation
if (diff > 0)
rot += 2 * Math.PI;
else
rot -= 2 * Math.PI;
diff = angle - rot;
}
//smooth out rotation by halving the difference
if (diff !== 0)
rot += diff / 2;
normalMesh.rotation.set(0, rot, 0);
While this works perfectly fine for the cube built with three.js, it fails to work as expected on the cube using physi.js.
I have created a demo showcasing this issue (note: unable to use jsfiddle due to web worker constraints):
The source code can be found in a downloadable zip file here --->
You can review the movement function at , specifically at line 95.
If you try rotating the camera with the mouse, you'll notice the issue occurring when attempting to align the cube with the red mesh direction.