Moving things around (Transforms)

There can’t be many games that don’t require moving objects about. In Chilli Source objects are described by entities and entities can be moved, scaled and rotated via their transform.

entity->GetTransform().SetPosition(0.0f, 10.0f, 0.0f);

In standard Chilli Source coordinates this would put the object 10 units off the ground. Rather than setting the position of an object (i.e. move to) we can also translate the object (i.e. move by)

entity->GetTransform().MoveBy(0.0f, 5.0f, 0.0f);

Getting the local position of the entity will return 0.0f, 15.0f, 0.0f.

CSCore::Vector3 localPos = entity->GetTransform().GetLocalPosition();

The transform of an entity is built hierarchically from its own local transform and the world transform of its parent. If we add our entity as a child of another we can see this hierarchical relationship.

parentEntity->AddEntity(entity);
parentEntity->GetTransform().SetPosition(5.0f, 0.0f, 0.0f);

Getting the local position of the entity will still return 0.0f, 15.0f, 0.0f; but getting the world position will return 5.0f, 15.0f, 0.0f.

CSCore::Vector3 worldPos = entity->GetTransform().GetWorldPosition();

Scaling and rotation work in much the same way with local components and hierarchical world components. There are many different convenience methods for rotating and scaling but here are a few examples:

entity->GetTransform().ScaleTo(5.0f); //Scale uniformly to 5 units
entity->GetTransform().ScaleBy(2.0f, 1.0f, 2.0f); //Scale the width and depth by 2 units

entity->GetTransform().RotateTo(CSCore::Vector3::k_one, k_pi/2.0f); //Rotate by 90 degrees (pi/2 radians) around each axis
entity->GetTransform().RotateXBy(k_pi/2.0f); //Rotate by 90 degrees (pi/2 radians) around the x axis

Transforms also support event driven programming by allowing listeners to subscribe to change events:

m_connection = entity->GetTransform().GetTransformChangedEvent().OpenConnection([]()
{
//Called whenever the entity transform changes
});

Remember events are scoped and therefore the connection must be held open as a member.