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 . 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
builds as a matrix. Inverse of.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 .
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 direction that is straight up or down leaves yaw undetermined, and reading it out of the residual horizontal components would be numerical noise, so it is pinned to zero the way the engine's VectorAngles does. That test is on absolute length, matching the engine, so a direction shorter than a thousandth of a unit reads as vertical whichever way it points.
ForwardDirectionToQuaternion(Vector3)
The rotation 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
builds from ,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 one is chosen rather than recovered, and world up is the reference that chooses it. Computed straight from the direction rather than by going 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.
ParseVector2(string)
Parses a string representation of a Vector2.
public static Vector2 ParseVector2(string input)Parameters
input string
The input string.
Returns
The parsed vector, or zero if the input is not two numbers.
ParseVector3(string)
Parses a string representation of a Vector3.
public static Vector3 ParseVector3(string input)Parameters
input string
The input string.
Returns
The parsed vector, or zero if the input is not three numbers.
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 .
public static Vector3 ToEulerAngles(Quaternion rotation)Parameters
rotation Quaternion
The quaternion to convert.
Returns
The Euler angles in degrees.
ToRigidTransformationMatrix(Entity)
Like 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.
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.
TryParseVector2(string, out Vector2)
Parses a string representation of a Vector2, reporting whether it was one.
public static bool TryParseVector2(string input, out Vector2 value)Parameters
input string
The input string.
value Vector2
The parsed vector, or zero if the input is not two numbers.
Returns
Whether the input parsed.
TryParseVector3(string, out Vector3)
Parses a string representation of a Vector3, reporting whether it was one.
public static bool TryParseVector3(string input, out Vector3 value)Parameters
input string
The input string.
value Vector3
The parsed vector, or zero if the input is not three numbers.
Returns
Whether the input parsed.
Remarks
Callers with a meaningful default need this rather than , whose zero is indistinguishable from a genuine "0 0 0" - a malformed "scales" falling back to zero rather than to one collapses the thing being scaled.

