public void BakeTransformFromParent() { GameObject parent = MyObject.GetParent(); if (parent != null && parent.MyTransform != null) { Matrix parents = parent.MyTransform.GetWorldMatrix(); Vector3 trans, scl; Quaternion quat; parents.Decompose(out scl, out quat, out trans); this.position = Vector3.Transform(this.position, Matrix.CreateFromQuaternion(quat) * Matrix.CreateTranslation(trans)); this.rotation += parent.MyTransform.rotation; } }
protected void CalculateWorldMatrix() { Matrix translation, rotation, scale, fromSocket, parents, secondPreRot; translation = Matrix.CreateTranslation(new Vector3(this.Position.X, this.Position.Y, -this.Position.Z)); rotation = Matrix.CreateFromYawPitchRoll(Rotation.Y, Rotation.X, Rotation.Z); scale = Matrix.CreateScale(this.Scale); fromSocket = Matrix.Identity; parents = Matrix.Identity; secondPreRot = Matrix.Identity; if (MyObject.MyCarrierSocket != null) { fromSocket = MyObject.MyCarrierSocket.BoneTransform; Vector3 trans, scl; Quaternion quat; MyObject.MyCarrierSocket.Carrier.MyTransform.GetWorldMatrix().Decompose(out scl, out quat, out trans); fromSocket = fromSocket * Matrix.CreateFromQuaternion(quat) * Matrix.CreateTranslation(trans); } if (MyObject.GetParent() != null && MyObject.GetParent().MyTransform != null) { parents = MyObject.GetParent().MyTransform.GetWorldMatrix(); Vector3 trans, scl; Quaternion quat; parents.Decompose(out scl, out quat, out trans); if (MyObject.GetType() == typeof(LightPoint)) { trans.Z = -trans.Z; } parents = Matrix.CreateFromQuaternion(quat) * Matrix.CreateTranslation(trans); secondPreRot = preRotationMatrix; } this.worldMatrix = preRotationMatrix * scale * rotation * translation * fromSocket * secondPreRot * parents; }