62 public void Set(FP new_x, FP new_y, FP new_z, FP new_w) {
69 public void SetFromToRotation(
TSVector fromDirection,
TSVector toDirection) {
71 this.Set(targetRotation.
x, targetRotation.
y, targetRotation.
z, targetRotation.
w);
79 FP t0 = -2.0f * (ysqr + z *
z) + 1.0f;
80 FP t1 = +2.0f * (x * y - w *
z);
81 FP t2 = -2.0f * (x * z + w *
y);
82 FP t3 = +2.0f * (y * z - w *
x);
83 FP t4 = -2.0f * (x * x + ysqr) + 1.0f;
85 t2 = t2 > 1.0f ? 1.0f : t2;
86 t2 = t2 < -1.0f ? -1.0f : t2;
88 result.
x = FP.Atan2(t3, t4) * FP.Rad2Deg;
89 result.
y = FP.Asin(t2) * FP.Rad2Deg;
90 result.
z = FP.Atan2(t1, t0) * FP.Rad2Deg;
100 FP angle = FP.Acos(f.
w) * 2 * FP.Rad2Deg;
115 #region public static JQuaternion Add(JQuaternion quaternion1, JQuaternion quaternion2) 129 TSQuaternion.CreateFromYawPitchRoll(y, x, z, out rotation);
135 return Euler(eulerAngles.
x, eulerAngles.
y, eulerAngles.
z);
139 axis = axis * FP.Deg2Rad;
142 FP halfAngle = angle * FP.Deg2Rad * FP.Half;
145 FP sin = FP.Sin(halfAngle);
147 rotation.
x = axis.
x * sin;
148 rotation.
y = axis.
y * sin;
149 rotation.
z = axis.
z * sin;
150 rotation.
w = FP.Cos(halfAngle);
155 public static void CreateFromYawPitchRoll(FP yaw, FP pitch, FP roll, out
TSQuaternion result)
157 FP num9 = roll * FP.Half;
158 FP num6 = FP.Sin(num9);
159 FP num5 = FP.Cos(num9);
160 FP num8 = pitch * FP.Half;
161 FP num4 = FP.Sin(num8);
162 FP num3 = FP.Cos(num8);
163 FP num7 = yaw * FP.Half;
164 FP num2 = FP.Sin(num7);
165 FP num = FP.Cos(num7);
166 result.x = ((num * num4) * num5) + ((num2 * num3) * num6);
167 result.y = ((num2 * num3) * num5) - ((num * num4) * num6);
168 result.z = ((num * num3) * num6) - ((num2 * num4) * num5);
169 result.w = ((num * num3) * num5) + ((num2 * num4) * num6);
180 result.x = quaternion1.x + quaternion2.x;
181 result.y = quaternion1.y + quaternion2.y;
182 result.z = quaternion1.z + quaternion2.z;
183 result.w = quaternion1.w + quaternion2.w;
190 quaternion.
x = -value.
x;
191 quaternion.
y = -value.
y;
192 quaternion.
z = -value.
z;
193 quaternion.
w = value.
w;
198 return a.
w * b.
w + a.
x * b.
x + a.
y * b.
y + a.
z * b.
z;
202 FP invNorm = FP.One / ((rotation.
x * rotation.
x) + (rotation.
y * rotation.
y) + (rotation.
z * rotation.
z) + (rotation.
w * rotation.
w));
218 return LerpUnclamped(a, b, t);
234 #region public static JQuaternion Subtract(JQuaternion quaternion1, JQuaternion quaternion2) 250 result.x = quaternion1.x - quaternion2.x;
251 result.y = quaternion1.y - quaternion2.y;
252 result.z = quaternion1.z - quaternion2.z;
253 result.w = quaternion1.w - quaternion2.w;
263 #region public static JQuaternion Multiply(JQuaternion quaternion1, JQuaternion quaternion2) 279 FP x = quaternion1.x;
280 FP y = quaternion1.y;
281 FP z = quaternion1.z;
282 FP w = quaternion1.w;
283 FP num4 = quaternion2.x;
284 FP num3 = quaternion2.y;
285 FP num2 = quaternion2.z;
286 FP num = quaternion2.w;
287 FP num12 = (y * num2) - (z * num3);
288 FP num11 = (z * num4) - (x * num2);
289 FP num10 = (x * num3) - (y * num4);
290 FP num9 = ((x * num4) + (y * num3)) + (z * num2);
291 result.x = ((x * num) + (num4 * w)) + num12;
292 result.y = ((y * num) + (num3 * w)) + num11;
293 result.z = ((z * num) + (num2 * w)) + num10;
294 result.w = (w * num) - num9;
304 #region public static JQuaternion Multiply(JQuaternion quaternion1, FP scaleFactor) 320 result.x = quaternion1.x * scaleFactor;
321 result.y = quaternion1.y * scaleFactor;
322 result.z = quaternion1.z * scaleFactor;
323 result.w = quaternion1.w * scaleFactor;
330 #region public void Normalize() 333 FP num2 = (((this.x * this.
x) + (this.y * this.y)) + (this.z * this.z)) + (this.w * this.w);
334 FP num = 1 / (FP.Sqrt(num2));
347 #region public static JQuaternion CreateFromMatrix(JMatrix matrix) 362 FP num8 = (matrix.M11 + matrix.M22) + matrix.M33;
365 FP num = FP.Sqrt((num8 + FP.One));
366 result.w = num * FP.Half;
368 result.x = (matrix.M23 - matrix.M32) * num;
369 result.y = (matrix.M31 - matrix.M13) * num;
370 result.z = (matrix.M12 - matrix.M21) * num;
372 else if ((matrix.M11 >= matrix.M22) && (matrix.M11 >= matrix.M33))
374 FP num7 = FP.Sqrt((((FP.One + matrix.M11) - matrix.M22) - matrix.M33));
375 FP num4 = FP.Half / num7;
376 result.x = FP.Half * num7;
377 result.y = (matrix.M12 + matrix.M21) * num4;
378 result.z = (matrix.M13 + matrix.M31) * num4;
379 result.w = (matrix.M23 - matrix.M32) * num4;
381 else if (matrix.M22 > matrix.M33)
383 FP num6 = FP.Sqrt((((FP.One + matrix.M22) - matrix.M11) - matrix.M33));
384 FP num3 = FP.Half / num6;
385 result.x = (matrix.M21 + matrix.M12) * num3;
386 result.y = FP.Half * num6;
387 result.z = (matrix.M32 + matrix.M23) * num3;
388 result.w = (matrix.M31 - matrix.M13) * num3;
392 FP num5 = FP.Sqrt((((FP.One + matrix.M33) - matrix.M11) - matrix.M22));
393 FP num2 = FP.Half / num5;
394 result.x = (matrix.M31 + matrix.M13) * num2;
395 result.y = (matrix.M32 + matrix.M23) * num2;
396 result.z = FP.Half * num5;
397 result.w = (matrix.M12 - matrix.M21) * num2;
408 #region public static FP operator *(JQuaternion value1, JQuaternion value2) 423 #region public static FP operator +(JQuaternion value1, JQuaternion value2) 438 #region public static FP operator -(JQuaternion value1, JQuaternion value2) 451 FP num = quat.
x * 2f;
452 FP num2 = quat.
y * 2f;
453 FP num3 = quat.
z * 2f;
454 FP num4 = quat.
x * num;
455 FP num5 = quat.
y * num2;
456 FP num6 = quat.
z * num3;
457 FP num7 = quat.
x * num2;
458 FP num8 = quat.
x * num3;
459 FP num9 = quat.
y * num3;
460 FP num10 = quat.
w * num;
461 FP num11 = quat.
w * num2;
462 FP num12 = quat.
w * num3;
465 result.
x = (1f - (num5 + num6)) * vec.
x + (num7 - num12) * vec.
y + (num8 + num11) * vec.
z;
466 result.
y = (num7 + num12) * vec.
x + (1f - (num4 + num6)) * vec.
y + (num9 - num10) * vec.
z;
467 result.
z = (num8 - num11) * vec.
x + (num9 + num10) * vec.
y + (1f - (num4 + num5)) * vec.
z;
472 public override string ToString() {
473 return string.Format(
"({0:f1}, {1:f1}, {2:f1}, {3:f1})", x.AsFloat(), y.AsFloat(), z.AsFloat(), w.AsFloat());
static TSQuaternion operator*(TSQuaternion value1, TSQuaternion value2)
Multiply two quaternions.
void Normalize()
Sets the length of the quaternion to one.
FP y
The Y component of the vector.
FP y
The Y component of the quaternion.
static void CreateFromMatrix(ref TSMatrix matrix, out TSQuaternion result)
Creates a quaternion from a matrix.
static void Subtract(ref TSQuaternion quaternion1, ref TSQuaternion quaternion2, out TSQuaternion result)
Quaternions are subtracted.
static void Multiply(ref TSQuaternion quaternion1, FP scaleFactor, out TSQuaternion result)
Scale a quaternion
static FP Clamp(FP value, FP min, FP max)
Returns a number which is within [min,max]
static FP Dot(TSVector vector1, TSVector vector2)
Calculates the dot product of two vectors.
static TSQuaternion operator-(TSQuaternion value1, TSQuaternion value2)
Subtract two quaternions.
Contains common math operations.
FP z
The Z component of the vector.
static TSQuaternion CreateFromMatrix(TSMatrix matrix)
Creates a quaternion from a matrix.
static TSQuaternion Subtract(TSQuaternion quaternion1, TSQuaternion quaternion2)
Quaternions are subtracted.
static TSQuaternion Add(TSQuaternion quaternion1, TSQuaternion quaternion2)
Quaternions are added.
TSQuaternion(FP x, FP y, FP z, FP w)
Initializes a new instance of the JQuaternion structure.
FP z
The Z component of the quaternion.
static TSQuaternion operator+(TSQuaternion value1, TSQuaternion value2)
Add two quaternions.
static TSVector Cross(TSVector vector1, TSVector vector2)
The cross product of two vectors.
FP sqrMagnitude
Gets the squared length of the vector.
static TSVector Normalize(TSVector value)
Normalizes the given vector.
static void Multiply(ref TSQuaternion quaternion1, ref TSQuaternion quaternion2, out TSQuaternion result)
Multiply two quaternions.
static TSQuaternion Multiply(TSQuaternion quaternion1, FP scaleFactor)
Scale a quaternion
FP x
The X component of the quaternion.
FP x
The X component of the vector.
FP w
The W component of the quaternion.
A Quaternion representing an orientation.
static void Add(ref TSQuaternion quaternion1, ref TSQuaternion quaternion2, out TSQuaternion result)
Quaternions are added.
static TSQuaternion Multiply(TSQuaternion quaternion1, TSQuaternion quaternion2)
Multiply two quaternions.