| name | quaternions |
| description | Quaternion creation, component access, and Euler angle conversion in PyChrono |
| compatibility | pychrono >= 8.0 |
| metadata | {"domain":"mbs"} |
Skill: ChQuaterniond — Quaternion API in PyChrono
Purpose
Access quaternion components, create quaternions from angles/axes, and convert to Euler angles in PyChrono.
When to Use
When reading body orientation, converting to Euler angles, creating a rotation, or passing a quaternion to Initialize/SetRot.
Component Attributes
PyChrono quaternions use scalar-first storage: e0 is the scalar (w) part.
q = body.GetRot()
w = q.e0
i = q.e1
j = q.e2
k = q.e3
Do NOT use any of these — they do not exist in PyChrono:
q.w
q.x
q.y
q.z
q.GetW()
q.GetX()
q.GetY()
q.GetZ()
Euler Angle Conversion
rot = body.GetRot()
euler = rot.GetCardanAnglesXYZ()
angle_x = euler.x
angle_y = euler.y
angle_z = euler.z
Creating Quaternions
q = chrono.QuatFromAngleAxis(angle_rad, chrono.VECT_Z)
q = chrono.QuatFromAngleAxis(angle_rad, chrono.ChVector3d(0, 0, 1))
q = chrono.ChQuaterniond()
q.SetFromAngleAxis(angle_rad, chrono.ChVector3d(0, 1, 0))
q = chrono.QUNIT
q = chrono.QuatFromAngleX(ax) * chrono.QuatFromAngleY(ay) * chrono.QuatFromAngleZ(az)
Useful Constants
chrono.QUNIT
chrono.Q_ROTATE_Y_TO_Z
chrono.Q_ROTATE_Y_TO_X
chrono.Q_ROTATE_Z_TO_X
Rotating Vectors (Coordinate Frame Transforms)
rot = body.GetRot()
world_vec = rot.RotateBack(local_vec)
local_vec = rot.Rotate(world_vec)
Typical usage — compute world position of a body-local attachment point:
rotor_pos = rotor.GetPos()
rotor_rot = rotor.GetRot()
attach_local = chrono.ChVector3d(0.25, 0, 0)
attach_world = rotor_pos + rotor_rot.RotateBack(attach_local)
Vector Operations
v = chrono.ChVector3d(x, y, z)
v.Length()
v.Cross(other_vec)
v.Dot(other_vec)
delta = pos2 - pos1
Common Patterns
Get rotation angle of a body about the Z-axis
rot = body.GetRot()
euler = rot.GetCardanAnglesXYZ()
angle_about_z = euler.z
Set body orientation
q = chrono.QuatFromAngleAxis(chrono.CH_PI / 4, chrono.VECT_Z)
body.SetRot(q)
Check if quaternion is normalized
norm = (q.e0**2 + q.e1**2 + q.e2**2 + q.e3**2) ** 0.5
Initialize joint with a rotated frame
rot = chrono.QuatFromAngleAxis(-chrono.CH_PI / 2, chrono.ChVector3d(0, 1, 0))
frame = chrono.ChFramed(chrono.ChVector3d(x, y, z), rot)
joint.Initialize(body1, body2, frame)