Recently delving into the world of ThreeJS, I've been exploring its potential for fun and creativity. My current project involves using ThreeJS in conjunction with aframe. The task at hand requires computing with ThreeJS to pass position and rotation data to the aframe component. Despite scouring through various resources, most of them are based on C++ and don't seem to utilize quaternions or ThreeJS.
At the moment, I have a camera positioned at 0,0,0 that I want to rotate towards a specific point in space. Eventually, the system will handle multiple points that can be saved, edited, and selected by users. It seems like storing this information in a matrix array would be ideal. When a point is selected, I need the camera to rotate and face in that direction. The user should be able to switch between points, altering the camera rotation accordingly.
// Seeking to determine the rotation from the camera to a raycast result
const camera = new Vector3()
const cast = new Vector3(10, 0, -20)
// Generating a quaternion based on the vectors to establish the rotation from camera to cast
const quaternion = new Quaternion().setFromUnitVectors(camera, cast)
console.info('quaternion', quaternion.toArray())
// The output is consistently [-0, 0, 0, 1], regardless of the cast's x, y, z values
// Using the quaternion to construct a new Matrix4
const matrix = new Matrix4().makeRotationFromQuaternion(quaternion)
// The output remains constant
// Understandable since the quaternion remains unchanged
console.log(matrix.toArray())
const position = matrix.getPosition() // Vector3(0, 0, 0)
// This generates another matrix
// Unsure how to retrieve the rotation
const rotation = new Matrix4().extractRotation(matrix)
Am I approaching this the right way? The quaternion properties for x, y, z, and w don't seem to vary, indicating a potential mistake on my part.
To summarize my queries:
- Why does the quaternion exhibit uniformity regardless of the x, y, z values provided?
- How can I ensure the quaternion represents the necessary rotation for the camera?
- How do I extract the rotation from a Matrix4?
This journey has been quite challenging, and I fear I might be overlooking something basic.
Thank you,
Jordan