Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. //struct purpose: define an axis alligned bounding box
  4. [System.Serializable]
  5. public struct AABB
  6. {
  7. public Vector3 origin; // lowest point on all 3 axis of the AABB
  8. public float width;
  9. public float height;
  10. public float length;
  11.  
  12. //Summary:
  13. // Shorthand for writing AABB(new Vector3.zero, 0, 0, 0).
  14. public static AABB zero { get; } = new AABB(Vector3.zero, 0, 0, 0);
  15.  
  16. /// <summary> Forms an AABB out of the provided data.If allow_negative_size is true, adjusts the AABB to make sure the size values are not negative </summary>
  17. public AABB(Vector3 origin, float width, float height, float length)
  18. {
  19. float origin_x = origin.x;
  20. float origin_y = origin.y;
  21. float origin_z = origin.z;
  22.  
  23. // If we get provided negative size for the axis, offset the origin by the size and absolute the size
  24. if (width < 0)
  25. {
  26. this.width = -width;
  27. origin_x += width;
  28. }
  29. else
  30. this.width = width;
  31.  
  32. if (height < 0)
  33. {
  34. this.height = -height;
  35. origin_y += height;
  36. }
  37. else
  38. this.height = height;
  39.  
  40. if (length < 0)
  41. {
  42. this.length = -length;
  43. origin_z += length;
  44. }
  45. else
  46. this.length = length;
  47.  
  48. this.origin = new Vector3(origin_x, origin_y, origin_z);
  49. }
  50.  
  51. /// <summary> Forms an AABB out of the sphere origin and sphere radius </summary>
  52. public AABB(Vector3 sphere_origin, float sphere_radius)
  53. {
  54. origin = sphere_origin - new Vector3(sphere_radius, sphere_radius, sphere_radius);
  55. width = sphere_radius * 2;
  56. height = width;
  57. length = width;
  58. }
  59.  
  60. /// <summary> Returns the lowest point on all axis </summary>
  61. public Vector3 AABB_Min()
  62. {
  63. return origin;
  64. }
  65.  
  66. /// <summary> Returns the highest point on all axis </summary>
  67. public Vector3 AABB_Max()
  68. {
  69. return origin + new Vector3(width, height, length);
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement