Entity position and rotation

From Age of Empires 4 Modding


Position

Position is encoded as a table with x, y, z entries. Such a table can be created with the World_Pos(x, y, z) function. The X and Z axes point along the map plane and the Y axis is the up direction.

  • Use Entity_GetPosition(entity) to get the position of an entity. The returned object has x, y, z fields.
  • Use Entity_SetPosition(entity, position) to set the position of an entity. The entity will not snap to the position immediately but will interpolate to it smoothly during the frame.

Example

local entity_position = Entity_GetPosition(entity)

-- Move entity forward by 5 along the x axis
local new_position = World_Pos(
    entity_position.x + 5,
    entity_position.y,
    entity_position.z
)

Entity_SetPosition(entity, new_position)

const entityPosition = Entity_GetPosition(entity)

// Move entity forward by 5 along the x axis
const newPosition = World_Pos(
    entityPosition.x + 5,
    entityPosition.y,
    entityPosition.z
)

Entity_SetPosition(entity, newPosition)

Rotation

Rotation is encoded as a heading vector (ie. the forward direction vector). The vector uses the same type as positions and can also be created with World_Pos(x, y, z).

  • Use Entity_GetHeading(entity) to get the heading of an entity. The returned object has x, y, z fields.
  • Use Entity_SetHeading(entity, heading, bInterpolate) to set the heading of an entity. The entity will rotate immediately or interpolate smoothly depending on whether bInterpolate is true or false.

Example

local entity_heading = Entity_GetPosition(entity)
print(entity_heading.x, entity_heading.y, entity_heading.z)

-- Make the entity point along the X axis
local new_heading = World_Pos(1, 0, 0)

Entity_SetHeading(entity, new_heading, true)

const entityHeading = Entity_GetHeading(entity)
print(entityHeading.x, entityHeading.y, entityHeading.z)

// Make the entity point along the X axis
const newHeading = World_Pos(1, 0, 0)

Entity_SetHeading(entity, newHeading, true)

Misc

  • Functions for manipulating position and heading also exist for other types of objects (eg. Squad_GetHeading for Squad)
  • As rotation is encoded as a heading vector we can not have roll angle (ie. a rotation around the forward direction)