# how to manually calculate a world transform?

## Recommended Posts

Suppose I have a sprite with a child in the game world.

game world > sprite > child of sprite

The child is moving around, and Phaser will automatically calculate its actual world position with world x/y. But what If I want to calculate the world x/y of an object that would be in the same spot of the child, but is not a sprite? Suppose it is a Phaser.Point, instead of a child sprite. How do I calculate the world transform of a given point if it is acting as the child of a sprite (with its own relative position and world position)? In other words, I want this same structure:

game world > sprite > child of sprite

Except swap the child of sprite with an arbitrary point:

game world > sprite > point

So the point world x/y can be calculated in the same was as a child sprite can, even if it is offset from its immediate parent (the sprite).

##### Share on other sites

Hi, in scene graph hierarchy you need to know position in world coordinates for every node to display it at correct position in world. So, at each level of scene graph you have to calculate world transform matrix (which is combination of scale - rotate - transform). Phaser is doing this for you for every PIXI.DisplayObject. Most of the work is done in updateTransform method (https://github.com/photonstorm/phaser/blob/master/v2/src/pixi/display/DisplayObject.js#L308)

So, if you need to calculate it manually for non-DisplayObject, I would use as much of PIXI.DisplayObject class as possible. I would strip everything which is not necessary if obejct is not ment to be displayed like properties: alpha, visible, renderable, ... Most of my concern would be to keep updateTransform working. Core of such class would still be PIXI.Matrix holding final world transform. From it you can extract world position and rotation. If you are interested in position only, you can further simplyfy it by not doing whole matrix multiplication, but focusing only on wt.tx and wt.ty calculation.