Orienting an Object to a Particular Direction
From C4 Engine Wiki
The following function orients a node so that its local x-axis is aligned with the direction given by the direction parameter. The up parameter represents the general up direction, which does not need to be perpendicular to direction, and determines how the node is rotated in the plane perpendicular to direction. The z-axis of the node will be aligned as closely as possible with the up vector.
void OrientToDirection(Node *node, const Point3D& position, const Vector3D& direction, const Vector3D& up) { Vector3D front = direction * InverseMag(direction); Vector3D left = Cross(up, direction).Normalize(); Vector3D top = Cross(front, left); node->SetNodeTransform(Transform4D(front, left, top, position)); }
