Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- //struct purpose: define an axis alligned bounding box
- [System.Serializable]
- public struct AABB
- {
- public Vector3 origin; // lowest point on all 3 axis of the AABB
- public float width;
- public float height;
- public float length;
- //Summary:
- // Shorthand for writing AABB(new Vector3.zero, 0, 0, 0).
- public static AABB zero { get; } = new AABB(Vector3.zero, 0, 0, 0);
- /// <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>
- public AABB(Vector3 origin, float width, float height, float length)
- {
- float origin_x = origin.x;
- float origin_y = origin.y;
- float origin_z = origin.z;
- // If we get provided negative size for the axis, offset the origin by the size and absolute the size
- if (width < 0)
- {
- this.width = -width;
- origin_x += width;
- }
- else
- this.width = width;
- if (height < 0)
- {
- this.height = -height;
- origin_y += height;
- }
- else
- this.height = height;
- if (length < 0)
- {
- this.length = -length;
- origin_z += length;
- }
- else
- this.length = length;
- this.origin = new Vector3(origin_x, origin_y, origin_z);
- }
- /// <summary> Forms an AABB out of the sphere origin and sphere radius </summary>
- public AABB(Vector3 sphere_origin, float sphere_radius)
- {
- origin = sphere_origin - new Vector3(sphere_radius, sphere_radius, sphere_radius);
- width = sphere_radius * 2;
- height = width;
- length = width;
- }
- /// <summary> Returns the lowest point on all axis </summary>
- public Vector3 AABB_Min()
- {
- return origin;
- }
- /// <summary> Returns the highest point on all axis </summary>
- public Vector3 AABB_Max()
- {
- return origin + new Vector3(width, height, length);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement