4 using System.Collections.Generic;
5 using System.Collections.Specialized;
6 using SiliconStudio.Core.Serialization.Converters;
7 using SiliconStudio.Core.Serialization.Serializers;
8 using SiliconStudio.Paradox.Effects;
9 using SiliconStudio.Paradox.Engine;
10 using SiliconStudio.Paradox.EntityModel;
11 using SiliconStudio.Paradox.Games;
12 using SiliconStudio.Core;
13 using SiliconStudio.Core.Collections;
14 using SiliconStudio.Core.Mathematics;
16 namespace SiliconStudio.
Paradox.Engine
22 [DataContract(
"TransformationComponent")]
23 [DataSerializerGlobal(null, typeof(TrackingCollection<TransformationComponent>))]
33 internal bool isSpecialRoot =
false;
34 private bool useTRS =
true;
41 public Matrix WorldMatrix = Matrix.Identity;
47 public Matrix LocalMatrix = Matrix.Identity;
72 var children =
new TrackingCollection<TransformationComponent>();
73 children.CollectionChanged += ChildrenCollectionChanged;
78 Scaling = Vector3.One;
84 get {
return useTRS; }
85 set { useTRS = value; }
95 public FastCollection<TransformationComponent> Children {
get;
private set; }
104 public Vector3 RotationEulerXYZ
108 var rotation = Rotation;
116 float xx = rotation.X * rotation.X;
117 float yy = rotation.Y * rotation.Y;
118 float zz = rotation.Z * rotation.Z;
119 float xy = rotation.X * rotation.Y;
120 float zw = rotation.Z * rotation.W;
121 float zx = rotation.Z * rotation.X;
122 float yw = rotation.Y * rotation.W;
123 float yz = rotation.Y * rotation.Z;
124 float xw = rotation.X * rotation.W;
126 rotationEuler.Y = (float)Math.Asin(2.0f * (yw - zx));
127 double test = Math.Cos(rotationEuler.Y);
130 rotationEuler.Z = (float)Math.Atan2(2.0f * (xy + zw), 1.0f - (2.0f * (yy + zz)));
131 rotationEuler.X = (float)Math.Atan2(2.0f * (yz + xw), 1.0f - (2.0f * (yy + xx)));
135 rotationEuler.Z = (float)Math.Atan2(2.0f * (zw - xy), 2.0f * (zx + yw));
136 rotationEuler.X = 0.0f;
138 return rotationEuler;
151 var halfAngles = value * 0.5f;
153 var fSinX = (float)Math.Sin(halfAngles.X);
154 var fCosX = (float)Math.Cos(halfAngles.X);
155 var fSinY = (float)Math.Sin(halfAngles.Y);
156 var fCosY = (float)Math.Cos(halfAngles.Y);
157 var fSinZ = (float)Math.Sin(halfAngles.Z);
158 var fCosZ = (float)Math.Cos(halfAngles.Z);
160 var fCosXY = fCosX * fCosY;
161 var fSinXY = fSinX * fSinY;
163 Rotation.X = fSinX * fCosY * fCosZ - fSinZ * fSinY * fCosX;
164 Rotation.Y = fSinY * fCosX * fCosZ + fSinZ * fSinX * fCosY;
165 Rotation.Z = fSinZ * fCosXY - fSinXY * fCosZ;
166 Rotation.W = fCosZ * fCosXY + fSinXY * fSinZ;
178 get {
return parent; }
181 var oldParent = Parent;
182 if (oldParent == value)
185 if (oldParent != null)
186 oldParent.Children.Remove(
this);
188 value.Children.Add(
this);
200 CreateMatrixTRS(ref Translation, ref Rotation, ref Scaling, out LocalMatrix);
213 if (Parent != null && !isSpecialRoot)
215 Parent.UpdateWorldMatrix();
216 Matrix.Multiply(ref LocalMatrix, ref Parent.WorldMatrix, out WorldMatrix);
220 WorldMatrix = LocalMatrix;
224 internal void UpdateWorldMatrixNonRecursive()
226 if (Parent != null && !isSpecialRoot)
228 Matrix.Multiply(ref LocalMatrix, ref Parent.WorldMatrix, out WorldMatrix);
232 WorldMatrix = LocalMatrix;
239 throw new InvalidOperationException(
"This TransformationComponent already has a Parent, detach it first.");
247 throw new InvalidOperationException(
"This TransformationComponent's parent is not the expected value.");
256 case NotifyCollectionChangedAction.Add:
259 case NotifyCollectionChangedAction.Remove:
263 throw new NotSupportedException();
285 float xx = rotation.X * rotation.X;
286 float yy = rotation.Y * rotation.Y;
287 float zz = rotation.Z * rotation.Z;
288 float xy = rotation.X * rotation.Y;
289 float zw = rotation.Z * rotation.W;
290 float zx = rotation.Z * rotation.X;
291 float yw = rotation.Y * rotation.W;
292 float yz = rotation.Y * rotation.Z;
293 float xw = rotation.X * rotation.W;
295 result.M11 = 1.0f - (2.0f * (yy + zz));
296 result.M12 = 2.0f * (xy + zw);
297 result.M13 = 2.0f * (zx - yw);
298 result.M21 = 2.0f * (xy - zw);
299 result.M22 = 1.0f - (2.0f * (zz + xx));
300 result.M23 = 2.0f * (yz + xw);
301 result.M31 = 2.0f * (zx + yw);
302 result.M32 = 2.0f * (yz - xw);
303 result.M33 = 1.0f - (2.0f * (yy + xx));
306 result.M41 = translation.X;
307 result.M42 = translation.Y;
308 result.M43 = translation.Z;
311 if (scaling.X != 1.0f)
313 result.M11 *= scaling.X;
314 result.M12 *= scaling.X;
315 result.M13 *= scaling.X;
317 if (scaling.Y != 1.0f)
319 result.M21 *= scaling.Y;
320 result.M22 *= scaling.Y;
321 result.M23 *= scaling.Y;
323 if (scaling.Z != 1.0f)
325 result.M31 *= scaling.Z;
326 result.M32 *= scaling.Z;
327 result.M33 *= scaling.Z;
Game entity. It usually aggregates multiple EntityComponent
Represents a container that can hold properties, lightweight to embed (lazy initialized).
Base class for converters to/from a data type.
Represents a three dimensional mathematical vector.
Represents a four dimensional mathematical quaternion.
object Item
Gets the added or removed item (if dictionary, value only).
NotifyCollectionChangedAction Action
Gets the type of action performed. Allowed values are NotifyCollectionChangedAction.Add and NotifyCollectionChangedAction.Remove.
A class that represents a tag propety.
Represents a 4x4 mathematical matrix.