2D scene graph not transforming relative to parent
- by Dr.Denis McCracleJizz
I am currently in the process of coding my own 2D Scene graph, which is basically a port of flash's render engine.
The problem I have right now is my rendering doesn't seem to be working properly. This code creates the localTransform property for each DisplayObject.
Matrix m_transform =
Matrix.CreateRotationZ(rotation) *
Matrix.CreateScale(scaleX, scaleY, 1) *
Matrix.CreateTranslation(new Vector3(x, y, z));
This is my render code.
float dRotation;
Vector2 dPosition, dScale;
Matrix transform;
transform = this.localTransform;
if (parent != null) transform = localTransform * parent.localTransform;
DecomposeMatrix(ref transform, out dPosition, out dRotation, out dScale);
spriteBatch.Draw(this.texture, dPosition, null, Color.White, dRotation,
new Vector2(originX, originY), dScale, SpriteEffects.None, 0.0f);
Here is the result when I try to add the Stage then to the stage a First DisplayObjectContainer and then a second one.
It may look fine but the problem lies in the fact that I add a first DisplayObjectContainer at (400,400) and the second one within it (that's the smallest one)
at position (0,0). So he should be right over its parent but he gets render within the parent at the same position the parent has (400, 400) for some reason. It's just as if I double the parent's localMatrix and then render the second cat there.
This is the code i use to loop through every childs.
base.Draw(spriteBatch);
foreach (DisplayObject childs in _childs)
{
childs.Draw(spriteBatch);
}