Class: Quat

pc.Quat

A quaternion.

Constructor

new Quat(xopt, yopt, zopt, wopt)

Create a new Quat object.
Parameters:
Name Type Attributes Description
x Number <optional>
The quaternion's x component. Default value 0. If x is an array of length 4, the array will be used to populate all components.
y Number <optional>
The quaternion's y component. Default value 0.
z Number <optional>
The quaternion's z component. Default value 0.
w Number <optional>
The quaternion's w component. Default value 1.
Source:

Members

(static, readonly) IDENTITY :pc.Quat

A constant quaternion set to [0, 0, 0, 1] (the identity).
Type:
Source:

(static, readonly) ZERO :pc.Quat

A constant quaternion set to [0, 0, 0, 0].
Type:
Source:

w :Number

The w component of the quaternion.
Type:
  • Number
Source:
Example
var quat = new pc.Quat();

// Get w
var w = quat.w;

// Set w
quat.w = 0;

x :Number

The x component of the quaternion.
Type:
  • Number
Source:
Example
var quat = new pc.Quat();

// Get x
var x = quat.x;

// Set x
quat.x = 0;

y :Number

The y component of the quaternion.
Type:
  • Number
Source:
Example
var quat = new pc.Quat();

// Get y
var y = quat.y;

// Set y
quat.y = 0;

z :Number

The z component of the quaternion.
Type:
  • Number
Source:
Example
var quat = new pc.Quat();

// Get z
var z = quat.z;

// Set z
quat.z = 0;

Methods

clone() → {pc.Quat}

Returns an identical copy of the specified quaternion.
Source:
Returns:
A quaternion containing the result of the cloning.
Type
pc.Quat
Example
var q = new pc.Quat(-0.11, -0.15, -0.46, 0.87);
var qclone = q.clone();

console.log("The result of the cloning is: " + q.toString());

copy(rhs) → {pc.Quat}

Copies the contents of a source quaternion to a destination quaternion.
Parameters:
Name Type Description
rhs pc.Quat The quaternion to be copied.
Source:
Returns:
Self for chaining.
Type
pc.Quat
Example
var src = new pc.Quat();
var dst = new pc.Quat();
dst.copy(src, src);
console.log("The two quaternions are " + (src.equals(dst) ? "equal" : "different"));

equals(rhs) → {Boolean}

Reports whether two quaternions are equal.
Parameters:
Name Type Description
rhs pc.Quat The quaternion to be compared against.
Source:
Returns:
true if the quaternions are equal and false otherwise.
Type
Boolean
Example
var a = new pc.Quat();
var b = new pc.Quat();
console.log("The two quaternions are " + (a.equals(b) ? "equal" : "different"));

getAxisAngle(axis) → {Number}

Gets the rotation axis and angle for a given quaternion. If a quaternion is created with setFromAxisAngle, this method will return the same values as provided in the original parameter list OR functionally equivalent values.
Parameters:
Name Type Description
axis pc.Vec3 The 3-dimensional vector to receive the axis of rotation.
Source:
Returns:
Angle, in degrees, of the rotation
Type
Number
Example
var q = new pc.Quat();
q.setFromAxisAngle(new pc.Vec3(0, 1, 0), 90);
var v = new pc.Vec3();
var angle = q.getAxisAngle(v);
// Should output 90
console.log(angle)
// Should output [0, 1, 0]
console.log(v.toString());

getEulerAngles(eulersopt) → {pc.Vec3}

Converts the supplied quaternion to Euler angles.
Parameters:
Name Type Attributes Description
eulers pc.Vec3 <optional>
The 3-dimensional vector to receive the Euler angles.
Source:
Returns:
The 3-dimensional vector holding the Euler angles that correspond to the supplied quaternion.
Type
pc.Vec3

invert() → {pc.Quat}

Generates the inverse of the specified quaternion.
Source:
Returns:
Self for chaining.
Type
pc.Quat
Example
// Create a quaternion rotated 180 degrees around the y-axis
var rot = new pc.Quat().setFromEulerAngles(0, 180, 0);

// Invert in place
rot.invert();

length() → {Number}

Returns the magnitude of the specified quaternion.
Source:
Returns:
The magnitude of the specified quaternion.
Type
Number
Example
var q = new pc.Quat(0, 0, 0, 5);
var len = q.length();
// Should output 5
console.log("The length of the quaternion is: " + len);

lengthSq() → {Number}

Returns the magnitude squared of the specified quaternion.
Source:
Returns:
The magnitude of the specified quaternion.
Type
Number
Example
var q = new pc.Quat(3, 4, 0);
var lenSq = q.lengthSq();
// Should output 25
console.log("The length squared of the quaternion is: " + lenSq);

mul(rhs) → {pc.Quat}

Returns the result of multiplying the specified quaternions together.
Parameters:
Name Type Description
rhs pc.Quat The quaternion used as the second multiplicand of the operation.
Source:
Returns:
Self for chaining.
Type
pc.Quat
Example
var a = new pc.Quat().setFromEulerAngles(0, 30, 0);
var b = new pc.Quat().setFromEulerAngles(0, 60, 0);

// a becomes a 90 degree rotation around the Y axis
// In other words, a = a * b
a.mul(b);

console.log("The result of the multiplication is: " a.toString());

mul2(lhs, rhs) → {pc.Quat}

Returns the result of multiplying the specified quaternions together.
Parameters:
Name Type Description
lhs pc.Quat The quaternion used as the first multiplicand of the operation.
rhs pc.Quat The quaternion used as the second multiplicand of the operation.
Source:
Returns:
Self for chaining.
Type
pc.Quat
Example
var a = new pc.Quat().setFromEulerAngles(0, 30, 0);
var b = new pc.Quat().setFromEulerAngles(0, 60, 0);
var r = new pc.Quat();

// r is set to a 90 degree rotation around the Y axis
// In other words, r = a * b
r.mul2(a, b);

console.log("The result of the multiplication is: " r.toString());

normalize() → {pc.Quat}

Returns the specified quaternion converted in place to a unit quaternion.
Source:
Returns:
The result of the normalization.
Type
pc.Quat
Example
var v = new pc.Quat(0, 0, 0, 5);

v.normalize();

// Should output 0, 0, 0, 1
console.log("The result of the vector normalization is: " + v.toString());

set(x, y, z, w) → {pc.Quat}

Sets the specified quaternion to the supplied numerical values.
Parameters:
Name Type Description
x Number The x component of the quaternion.
y Number The y component of the quaternion.
z Number The z component of the quaternion.
w Number The w component of the quaternion.
Source:
Returns:
Self for chaining.
Type
pc.Quat
Example
var q = new pc.Quat();
q.set(1, 0, 0, 0);

// Should output 1, 0, 0, 0
console.log("The result of the vector set is: " + q.toString());

setFromAxisAngle(axis, angle) → {pc.Quat}

Sets a quaternion from an angular rotation around an axis.
Parameters:
Name Type Description
axis pc.Vec3 World space axis around which to rotate.
angle Number Angle to rotate around the given axis in degrees.
Source:
Returns:
Self for chaining.
Type
pc.Quat
Example
var q = new pc.Quat();
q.setFromAxisAngle(pc.Vec3.UP, 90);

setFromEulerAngles(ex, ey, ez) → {pc.Quat}

Sets a quaternion from Euler angles specified in XYZ order.
Parameters:
Name Type Description
ex Number Angle to rotate around X axis in degrees.
ey Number Angle to rotate around Y axis in degrees.
ez Number Angle to rotate around Z axis in degrees.
Source:
Returns:
Self for chaining.
Type
pc.Quat
Example
var q = new pc.Quat();
q.setFromEulerAngles(45, 90, 180);

setFromMat4(m) → {pc.Quat}

Converts the specified 4x4 matrix to a quaternion. Note that since a quaternion is purely a representation for orientation, only the translational part of the matrix is lost.
Parameters:
Name Type Description
m pc.Mat4 The 4x4 matrix to convert.
Source:
Returns:
Self for chaining.
Type
pc.Quat
Example
// Create a 4x4 rotation matrix of 180 degrees around the y-axis
var rot = new pc.Mat4().setFromAxisAngle(pc.Vec3.UP, 180);

// Convert to a quaternion
var q = new pc.Quat().setFromMat4(rot);

slerp(lhs, rhs, alpha) → {pc.Quat}

Performs a spherical interpolation between two quaternions. The result of the interpolation is written to the quaternion calling the function.
Parameters:
Name Type Description
lhs pc.Quat The quaternion to interpolate from.
rhs pc.Quat The quaternion to interpolate to.
alpha Number The value controlling the interpolation in relation to the two input quaternions. The value is in the range 0 to 1, 0 generating q1, 1 generating q2 and anything in between generating a spherical interpolation between the two.
Source:
Returns:
Self for chaining.
Type
pc.Quat
Example
var q1 = new pc.Quat(-0.11,-0.15,-0.46,0.87);
var q2 = new pc.Quat(-0.21,-0.21,-0.67,0.68);

var result;
result = new pc.Quat().slerp(q1, q2, 0);   // Return q1
result = new pc.Quat().slerp(q1, q2, 0.5); // Return the midpoint interpolant
result = new pc.Quat().slerp(q1, q2, 1);   // Return q2

toString() → {String}

Converts the quaternion to string form.
Source:
Returns:
The quaternion in string form.
Type
String
Example
var v = new pc.Quat(0, 0, 0, 1);
// Should output '[0, 0, 0, 1]'
console.log(v.toString());

transformVector(vec, resopt) → {pc.Vec3}

Transforms a 3-dimensional vector by the specified quaternion.
Parameters:
Name Type Attributes Description
vec pc.Vec3 The 3-dimensional vector to be transformed.
res pc.Vec3 <optional>
An optional 3-dimensional vector to receive the result of the transformation.
Source:
Returns:
The input vector v transformed by the current instance.
Type
pc.Vec3
Example
// Create a 3-dimensional vector
var v = new pc.Vec3(1, 2, 3);

// Create a 4x4 rotation matrix
var q = new pc.Quat().setFromEulerAngles(10, 20, 30);

var tv = q.transformVector(v);