I'm just starting to learn about 3D graphics, so a lot of the terminology is unfamiliar to me...
In my ThreeJs project, I have a 3D model flying around a room and avoiding walls. When it gets near a wall, it needs to turn slightly to avoid hitting it.
However, I'm having trouble getting the model to face the new direction of motion after turning. No matter what I try, it always ends up facing a random direction.
Below is the code I've been using in an attempt to make it face the correct direction. I've included comments to explain my thought process behind each step.
(the specific model I'm working with is stored as an attribute of the object named "bird")
// convert the current rotation angle to a directional vector
const currentMotionDirection = new THREE.Vector3()
.setFromEuler(bird.model.rotation)
.normalize();
// calculate the angle between the current direction and the desired direction
const angle = currentMotionDirection.angleTo(directionToGo);
// rotate the model to face the desired direction
const quaternion = new THREE.Quaternion().setFromAxisAngle(axis, angle);
bird.model.quaternion.copy(quaternion);
// update velocity to move in the new direction
bird.setVelocity(directionToGo);
.
This is what happens based on the above code:
https://i.sstatic.net/Lzafe.gif
Any assistance would be greatly appreciated, thank you!