I am writing a small 2d html5 game engine in javascript that relies on the concept of a hierarchy. The scene is the root node or entity, and its children make up the game objects. These children can have other children, recursively. Every entity has a relative position and an absolute position. By relative, I mean in object space, and by absolute, I mean in world space - based on its own and its parents' transformations.
To calculate the world position of an entity, all I have to do is apply its transformations (translation, rotation and scale), and that of its parent, and of its parent's parent, to its local/relative position up until the root entity of the hierarchy.
The problem I'm having is when to calculate this the most efficient way. Should I calculate the world position every time I get
its worldX
or worldY
properties? What about every time the position, scale or rotation properties of the entity or any of its parents are set
? Or is there a completely different way?
Note: along with knowing an entity's world location, I also need to know its world bounding box.