Transform25D.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using Godot;
  2. using System;
  3. using System.Runtime.InteropServices;
  4. /// <summary>
  5. /// Calculates the 2D transformation from a 3D position and a Basis25D.
  6. /// </summary>
  7. [Serializable]
  8. [StructLayout(LayoutKind.Sequential)]
  9. public struct Transform25D : IEquatable<Transform25D>
  10. {
  11. // Public fields store information that is used to calculate the properties.
  12. /// <summary>
  13. /// Controls how the 3D position is transformed into 2D.
  14. /// </summary>
  15. public Basis25D basis;
  16. /// <summary>
  17. /// The 3D position of the object. Should be updated on every frame before everything else.
  18. /// </summary>
  19. public Vector3 spatialPosition;
  20. // Public properties calculate on-the-fly.
  21. /// <summary>
  22. /// The 2D transformation of this object. Slower than FlatPosition.
  23. /// </summary>
  24. public Transform2D FlatTransform
  25. {
  26. get
  27. {
  28. return new Transform2D(0, FlatPosition);
  29. }
  30. }
  31. /// <summary>
  32. /// The 2D position of this object.
  33. /// </summary>
  34. public Vector2 FlatPosition
  35. {
  36. get
  37. {
  38. Vector2 pos = spatialPosition.x * basis.x;
  39. pos += spatialPosition.y * basis.y;
  40. pos += spatialPosition.z * basis.z;
  41. return pos;
  42. }
  43. }
  44. // Constructors
  45. public Transform25D(Transform25D transform25D)
  46. {
  47. basis = transform25D.basis;
  48. spatialPosition = transform25D.spatialPosition;
  49. }
  50. public Transform25D(Basis25D basis25D)
  51. {
  52. basis = basis25D;
  53. spatialPosition = Vector3.Zero;
  54. }
  55. public Transform25D(Basis25D basis25D, Vector3 position3D)
  56. {
  57. basis = basis25D;
  58. spatialPosition = position3D;
  59. }
  60. public Transform25D(Vector2 xAxis, Vector2 yAxis, Vector2 zAxis)
  61. {
  62. basis = new Basis25D(xAxis, yAxis, zAxis);
  63. spatialPosition = Vector3.Zero;
  64. }
  65. public Transform25D(Vector2 xAxis, Vector2 yAxis, Vector2 zAxis, Vector3 position3D)
  66. {
  67. basis = new Basis25D(xAxis, yAxis, zAxis);
  68. spatialPosition = position3D;
  69. }
  70. public static bool operator ==(Transform25D left, Transform25D right)
  71. {
  72. return left.Equals(right);
  73. }
  74. public static bool operator !=(Transform25D left, Transform25D right)
  75. {
  76. return !left.Equals(right);
  77. }
  78. public override bool Equals(object obj)
  79. {
  80. if (obj is Transform25D)
  81. {
  82. return Equals((Transform25D)obj);
  83. }
  84. return false;
  85. }
  86. public bool Equals(Transform25D other)
  87. {
  88. return basis.Equals(other.basis) && spatialPosition.Equals(other.spatialPosition);
  89. }
  90. public bool IsEqualApprox(Transform25D other)
  91. {
  92. return basis.IsEqualApprox(other.basis) && spatialPosition.IsEqualApprox(other.spatialPosition);
  93. }
  94. public override int GetHashCode()
  95. {
  96. return basis.GetHashCode() ^ spatialPosition.GetHashCode();
  97. }
  98. public override string ToString()
  99. {
  100. string s = String.Format("({0}, {1})", new object[]
  101. {
  102. basis.ToString(),
  103. spatialPosition.ToString()
  104. });
  105. return s;
  106. }
  107. public string ToString(string format)
  108. {
  109. string s = String.Format("({0}, {1})", new object[]
  110. {
  111. basis.ToString(format),
  112. spatialPosition.ToString(format)
  113. });
  114. return s;
  115. }
  116. }