Class EntityTransformHelper
Namespace: ValveResourceFormat.ResourceTypes
Assembly: ValveResourceFormat.dll
Helper methods for entity transformations.
public static class EntityTransformHelperInheritance
object ← EntityTransformHelper
Remarks
Source 2 is Z-up and right-handed: +X is forward, +Y is left, +Z is up. An entity's "angles" is a QAngle, held here as (pitch, yaw, roll) in degrees. The components are not in axis order: pitch turns about Y, yaw about Z, roll about X, and they compose in that order. Pitch is positive downwards, so a forward vector's Z is -sin(pitch) - cameras usually do the opposite, which is the sign most often flipped by mistake. Yaw is positive turning left. Matrices are row-vector, so a rotation's rows are forward, left and up, and forward is read by transforming Vector3.UnitX. At a pitch of +/-90 forward is vertical and yaw and roll share an axis (gimbal lock), so converting back to angles puts the whole rotation into yaw and pins roll to zero.
Methods
EulerAnglesToForwardDirection(Vector3)
Converts Euler angles (pitch, yaw, roll) to a normalized forward direction vector.
public static Vector3 EulerAnglesToForwardDirection(Vector3 pitchYawRoll)Parameters
pitchYawRoll Vector3
The Euler angles.
Returns
The normalized forward direction.
EulerAnglesToQuaternion(Vector3)
Converts Euler angles (pitch, yaw, roll) to a rotation quaternion, the same rotation EntityTransformHelper.EulerAnglesToRotationMatrix(Vector3) builds as a matrix. Inverse of EntityTransformHelper.ToEulerAngles(Quaternion).
public static Quaternion EulerAnglesToQuaternion(Vector3 pitchYawRoll)Parameters
pitchYawRoll Vector3
The Euler angles.
Returns
The rotation quaternion.
EulerAnglesToRotationMatrix(Vector3)
Converts Euler angles (pitch, yaw, roll) to a rotation matrix.
public static Matrix4x4 EulerAnglesToRotationMatrix(Vector3 pitchYawRoll)Parameters
pitchYawRoll Vector3
The Euler angles.
Returns
The rotation matrix.
EulerAnglesToRotationMatrixRadians(Vector3)
Converts Euler angles (pitch, yaw, roll) in radians to a rotation matrix, for the callers that hold their angles that way, such as a particle's rotation attributes.
public static Matrix4x4 EulerAnglesToRotationMatrixRadians(Vector3 pitchYawRoll)Parameters
pitchYawRoll Vector3
The Euler angles, in radians.
Returns
The rotation matrix.
ForwardDirectionToEulerAngles(Vector3)
Converts a forward direction vector to Euler angles (pitch, yaw, roll) in degrees, with zero roll. Inverse of EntityTransformHelper.EulerAnglesToForwardDirection(Vector3).
public static Vector3 ForwardDirectionToEulerAngles(Vector3 direction)Parameters
direction Vector3
The forward direction. Need not be normalized, but see the remarks on very short ones.
Returns
The Euler angles in degrees.
Remarks
A straight up or down direction leaves yaw undetermined, so it is pinned to zero rather than read from noise in the horizontal components. The vertical test uses the horizontal length, so a direction whose horizontal part is under a thousandth of a unit reads as vertical, whichever way it points.
ForwardDirectionToQuaternion(Vector3)
The rotation EntityTransformHelper.ForwardDirectionToRotationMatrix(Vector3) builds, as a quaternion.
public static Quaternion ForwardDirectionToQuaternion(Vector3 direction)Parameters
direction Vector3
The forward direction. Does not need to be normalized.
Returns
The rotation quaternion.
ForwardDirectionToRotationMatrix(Vector3)
Completes a rotation frame around a forward direction, with zero roll: the same frame EntityTransformHelper.EulerAnglesToRotationMatrix(Vector3) builds from EntityTransformHelper.ForwardDirectionToEulerAngles(Vector3), so forward lands on the first row.
public static Matrix4x4 ForwardDirectionToRotationMatrix(Vector3 direction)Parameters
direction Vector3
The forward direction. Does not need to be normalized.
Returns
The rotation matrix, with forward, left and up as its rows.
Remarks
A direction cannot express roll, so world up picks one rather than recovering it. Computed directly from the direction, not through angles, since this runs per particle in places.
GetTransformComponents(Entity, out Vector3, out Matrix4x4, out Vector3)
Reads an entity's scale, rotation and position from its "scales", "angles" and "origin" keyvalues.
public static void GetTransformComponents(EntityLump.Entity entity, out Vector3 scaleVector, out Matrix4x4 rotationMatrix, out Vector3 positionVector)Parameters
entity EntityLump.Entity
The entity to read from.
scaleVector Vector3
The scale vector.
rotationMatrix Matrix4x4
The rotation matrix.
positionVector Vector3
The position vector.
GetTwistAngleAroundZ(Quaternion)
Gets the angle a rotation turns about the Z axis, leaving out any swing away from that axis.
public static float GetTwistAngleAroundZ(Quaternion rotation)Parameters
rotation Quaternion
The rotation.
Returns
float
The twist angle in radians.
ToEulerAngles(Quaternion)
Converts a quaternion to Euler angles (pitch, yaw, roll) in degrees. Includes gimbal lock handling when pitch is near +/-90 degrees. Inverse of EntityTransformHelper.EulerAnglesToQuaternion(Vector3).
public static Vector3 ToEulerAngles(Quaternion rotation)Parameters
rotation Quaternion
The quaternion to convert.
Returns
The Euler angles in degrees.
ToRigidTransformationMatrix(Entity)
Like EntityTransformHelper.ToTransformationMatrix(Entity) but without scale; the transform a template passes to its children.
public static Matrix4x4 ToRigidTransformationMatrix(EntityLump.Entity entity)Parameters
entity EntityLump.Entity
The entity.
Returns
The transform without the entity's scale.
ToRigidTransformationMatrix(Vector3, Vector3)
The rigid transform for explicit angles and origin, the same composition EntityTransformHelper.ToRigidTransformationMatrix(Entity) reads from keyvalues.
public static Matrix4x4 ToRigidTransformationMatrix(Vector3 pitchYawRoll, Vector3 origin)Parameters
pitchYawRoll Vector3
The Euler angles.
origin Vector3
The translation.
Returns
The rigid transform, rotation then translation.
ToTransformationMatrix(Entity)
Calculates the full transformation matrix for an entity.
public static Matrix4x4 ToTransformationMatrix(EntityLump.Entity entity)Parameters
entity EntityLump.Entity
The entity.
Returns
The transformation matrix.
TryParseAngles(string?, out Vector3)
Parses an "angles" string: four numbers are a rotation quaternion, which is converted to angles, and three or five or more are pitch, yaw and roll in degrees, read from the first three.
public static bool TryParseAngles(string? input, out Vector3 angles)Parameters
input string?
The input string.
angles Vector3
The angles in degrees, or zero if the input holds fewer than three numbers.
Returns
bool
Whether the input held at least three numbers.
Remarks
Not part of ValueParsing because the four-number quaternion form is specific to angles.

