In my three.js demo, collision detection is achieved using a Raycaster that extends from the front of the camera. The movement of the camera is restricted to follow its facing direction, although the mouse controls allow for steering in different directions.
var ray = new THREE.Raycaster(camera.position, cameraDirectionVector);
var collisionResults = ray.intersectObjects(scene.children);
if (collisionResults.length > 0) {
if (collisionResults[0].distance < 0.15) {
var crossAxis = new THREE.Vector3(0, 0, 1);
crossAxis.crossVectors(cameraDirectionVector, collisionResults[0].face.normal);
camera.rotateOnAxis(crossAxis, 0.2); // this second parameter needs correct calculation
}
}
When a collision occurs, I am utilizing the cross product of the collision face normal and the camera's travel direction as the rotation axis. This method aims to rotate the camera away from the point of impact effectively.
To determine the rotation direction around this axis accurately, I need to calculate it based on the orientation of the crossAxis Vector3 relative to the face normal and camera travel direction. The resulting value may have to be positive or negative depending on these factors.
Note that this collision detection system is designed to be basic and has limitations due to the player's forward-only movement capability.
Is there a way to establish whether the camera should rotate clockwise or counterclockwise around the cross product axis? Once this calculation is correctly determined, I can enhance the collision effects, such as reducing the rotation gradually over a specified number of frames.
Any assistance would be highly valued!