Game Development Reference
In-Depth Information
math::matrix m_localTransform;
math::matrix m_worldTransform;
std::vector< std::shared_ptr<item>> m_children;
};
An item will have two transforms, the world transform defines its position, rotation and
scalerelativetotheworld'sorigin{0,0,0}andthelocaltransformthatwillberelativetoits
parent's world transform.
void item::ApplyWorldTransform(const math::matrix& world)
{
m_worldTransform = world;
for ( auto& child : m_children )
{
child->SetWorld( m_worldTransform * child->Local() );
}
}
When we apply a world transform on an item, we will recalculate each of the children's
world transform by multiplying their local transform with the parent's new world trans-
form.
During an item's update we can iterate through all the children and apply the world trans-
formation on them.
void item::Update(float deltaTime)
{
if ( m_applyTransform )
{
for ( auto& child : m_children )
{
child->ApplyWorldTransform(m_worldTransform);
}
m_applyTransform = false;
}
}
To avoid unnecessary matrix multiplications, we keep a flag, m_applyTransform which is
set anytime we modify the local or world transforms. This ensures that we only propagate
a matrix down the hierarchy if it has been modified, it would be a potentially costly waste
of CPU cycles to be transforming the hierarchy every frame.
To draw a menu item, we simply draw the item itself, then draw each of its children.
Search WWH ::




Custom Search