GetAbsOrigin()

From Valve Developer Community
Jump to: navigation, search

GetAbsOrigin() returns the absolute (i.e. world-relative) origin of an entity as a vector. Its sister function SetAbsOrigin() changes it.

The origin is technically stored in local space (i.e parent-relative). If an entity has a parent, the Abs functions perform extra calculations to get the position relative to the world. This value is cached and only recalculated if the origin has changed since the last time it was used. On the other hand, GetLocalOrigin() and SetLocalOrigin() work directly with the stored origin (m_vecOrigin) with no extra calculations, so if one entity is parented to another entity, the returned origin will be relative to the origin of its parent. Get/SetLocalOrigin should only be used when world space isn't important (e.g. an entity that can't be parented) or is undesirable while an entity is parented.

Alternatives

A different approach for finding the position of an entity can be useful, particularly when dealing with a brush entity which might not have a valid origin. Bear in mind that these alternatives are all significantly more expensive than the 'vanilla' functions listed above.

Vector WorldSpaceCenter()
Returns the centre of an entity's bounding box.
void CollisionProp()->CalcNearestPoint(&vecWorldPt,*pVecNearestWorldPt)
Finds the nearest point on the entity's bounding box to vecWorldPt. Modifies pVecNearestWorldPt with the result instead of returning anything.

Cells

Starting with Left 4 Dead, CBaseEntity transmits its origin in "cells". This splits the entity's local coordinate space up into cells of 32 units X/Y/Z, then uses that reduced range to compress the networked value to a mere 5 bits per ordinate. The index of the current cell also has to be networked, but it changes relatively infrequently.

This system leads to a huge reduction in network traffic at the price of imprecision. The sensitivity of prediction requires the local player to be sent an uncompressed origin.

Note:<Alien Swarm> In Alien Swarm, CBasePlayer assumes that an uncompressed origin will be sent...but does not actually send one. This leads to the client-side entity being stuck at world origin. Either network the origin yourself, or for a quick fix override ShouldRegenerateOriginFromCellBits() and return true.