The Quaternion.multiplyQuaternions()
method in Three.js multiplies two input quaternions and stores the resulting quaternion in the first input quaternion.
mulitplyQuaternions( q1, q2 )
q1
- The first input quaternion.q2
- The second input quaternion.Both q1
and q2
should be instances of the Quaternion class in Three.js.
This method does not return anything. Instead, the resulting quaternion is stored in the first input quaternion q1
.
This method multiplies two quaternions in the specified order and stores the resulting quaternion in the first input quaternion.
Quaternions are a 4-dimensional representation of rotations in Three.js. They are used to describe rotations in 3D space and can be used to smoothly interpolate between different orientations. Quaternions are defined with four components (x, y, z, w), but are often normalized to have a length of 1. The formula for quaternion multiplication is:
result = q1 * q2
Where q1
and q2
are quaternions and result
is the resulting quaternion.
import { Quaternion } from 'three.js';
const q1 = new Quaternion(0, 1, 0, 0); // represents a rotation of 180 degrees around the x-axis
const q2 = new Quaternion().setFromAxisAngle(new Vector3(1, 1, 0), Math.PI / 4); // represents a rotation of 45 degrees around the axis (1, 1, 0)
q1.multiplyQuaternions(q1, q2); // multiply q1 by q2 and store the result in q1
console.log(q1); // prints Quaternion { _x: 0.382, _y: 0.924, _z: 0, _w: 0 }
In this example, we create two quaternions q1
and q2
. q1
represents a rotation of 180 degrees around the x-axis, and q2
represents a rotation of 45 degrees around the axis (1, 1, 0). We then call the multiplyQuaternions()
method on q1
and pass in q1
as the first parameter and q2
as the second parameter. This multiplies q1
by q2
and stores the result in q1
. Finally, we log the resulting quaternion, which represents a rotation of 225 degrees around the axis (0.382, 0.924, 0).
q1
) and does not create a new quaternion.q1 * q2
produces a different result than q2 * q1
.