BabylonQuaternion.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System;
  2. namespace BabylonExport.Entities
  3. {
  4. public class BabylonQuaternion
  5. {
  6. public float X { get; set; }
  7. public float Y { get; set; }
  8. public float Z { get; set; }
  9. public float W { get; set; }
  10. public float[] ToArray()
  11. {
  12. return new [] {X, Y, Z, W};
  13. }
  14. /**
  15. * Copy / pasted from babylon
  16. */
  17. public BabylonVector3 toEulerAngles()
  18. {
  19. var result = new BabylonVector3();
  20. var qz = this.Z;
  21. var qx = this.X;
  22. var qy = this.Y;
  23. var qw = this.W;
  24. var sqw = qw * qw;
  25. var sqz = qz * qz;
  26. var sqx = qx * qx;
  27. var sqy = qy * qy;
  28. var zAxisY = qy * qz - qx * qw;
  29. var limit = .4999999;
  30. if (zAxisY< -limit) {
  31. result.Y = (float) (2 * Math.Atan2(qy, qw));
  32. result.X = (float) Math.PI / 2;
  33. result.Z = 0;
  34. } else if (zAxisY > limit) {
  35. result.Y = (float) (2 * Math.Atan2(qy, qw));
  36. result.X = (float) -Math.PI / 2;
  37. result.Z = 0;
  38. } else {
  39. result.Z = (float)Math.Atan2(2.0 * (qx* qy + qz* qw), (-sqz - sqx + sqy + sqw));
  40. result.X = (float)Math.Asin(-2.0 * (qz* qy - qx* qw));
  41. result.Y = (float)Math.Atan2(2.0 * (qz* qx + qy* qw), (sqz - sqx - sqy + sqw));
  42. }
  43. return result;
  44. }
  45. public override string ToString()
  46. {
  47. return "{ X=" + X + ", Y=" + Y + ", Z=" + Z + ", W=" + W + " }";
  48. }
  49. }
  50. }