Libraries.Game.Collision.BoundingBox2D Documentation

This class represents a 2D box that bounds an 2D item. The box approximates the size of the 2D items. The box is defined by two Vector2 objects which represent the maximum and minimum points of the item. This class is used throughout the Collision libraries to monitor collisions. The box is not allowed to rotate, therefore, we call these boxes "axis-aligned bounding boxes".

Example Code

use Libraries.Game.Collision.BoundingBox2D
use Libraries.Compute.Vector2

BoundingBox2D boundingBox
Vector2 minimum
Vector2 maximum
minimum:Set(-1, -1)
maximum:Set(1, 1)

boundingBox:Set(minimum, maximum)

output "The center of the bounding box is (" + boundingBox:GetCenterX() 
        + ", " + boundingBox:GetCenterY() + ") "

Inherits from: Libraries.Language.Object

Actions Documentation

CalculateDimensions()

This action re-calculates the center and the dimensions of the BoundingBox2D. This action is used internally. Note that center and dimensions are calculated when minimum and maximum are set.

Clear()

This action will clear out the values of the BoundingBox2D, resetting all of its values to zero.

Example

use Libraries.Game.Collision.BoundingBox2D
use Libraries.Compute.Vector2
use Libraries.Containers.Array

BoundingBox2D boundingBox
Vector2 minimum
Vector2 maximum

minimum:Set(-1, -1)
maximum:Set(1, 1)
boundingBox:Set(minimum, maximum)

output "The box is centered at (" + boundingBox:GetCenterX() 
    + ", " + boundingBox:GetCenterY() + ") with a width of " 
    +  boundingBox:GetWidth() + " and an height of " + boundingBox:GetHeight()

boundingBox:Clear()

output "After clearing, the box is centered at (" + boundingBox:GetCenterX() 
    + ", " + boundingBox:GetCenterY() + ") with a width of " 
    +  boundingBox:GetWidth() + " and an height of " + boundingBox:GetHeight()

Combine(Libraries.Game.Collision.BoundingBox2D box1, Libraries.Game.Collision.BoundingBox2D box2)

This action combines two given boundingBox to create the bounds of this BoundingBox2D. CalculateDimensions() should be called after this action to get center, width, and height.

Parameters

Example

use Libraries.Game.Collision.BoundingBox2D
use Libraries.Compute.Vector2
use Libraries.Containers.Array

BoundingBox2D boundingBox
Vector2 minimum
Vector2 maximum
minimum:Set(-1, -1)
maximum:Set(1, 1)
boundingBox:Set(minimum, maximum)

output "The box is centered at (" + boundingBox:GetCenterX() 
    + ", " + boundingBox:GetCenterY() + ") with a width of " 
    +  boundingBox:GetWidth() + " and an height of " + boundingBox:GetHeight()

BoundingBox2D addition
Vector2 minimum2
Vector2 maximum2
minimum:Set(2, 2)
maximum:Set(0.9, 0.9)
addition:Set(minimum, maximum)

output "The second box is centered at (" + addition:GetCenterX() 
    + ", " + addition:GetCenterY() + ") with a width of " 
    +  addition:GetWidth() + " and a height of " + addition:GetHeight()

BoundingBox2D result
result:Combine(boundingBox, addition)
result:CalculateDimensions()

output "The box, resulting from the combination, is centered at (" + result:GetCenterX() 
    + ", " + result:GetCenterY() + ") with a width of " 
    +  result:GetWidth() + " and a height of " + result:GetHeight()

Compare(Libraries.Language.Object object)

This action compares two object hash codes and returns an integer. The result is larger if this hash code is larger than the object passed as a parameter, smaller, or equal. In this case, -1 means smaller, 0 means equal, and 1 means larger. This action was changed in Quorum 7 to return an integer, instead of a CompareResult object, because the previous implementation was causing efficiency issues.

Parameters

Return

integer: The Compare result, Smaller, Equal, or Larger.

Example

Object o
Object t
integer result = o:Compare(t) //1 (larger), 0 (equal), or -1 (smaller)

Contains(Libraries.Game.Collision.BoundingBox2D bounds)

This action will test to see if this BoundingBox2D totally contains a given BoundingBox. If any part of the given BoundingBox2D is outside this one, it will return false.

Parameters

Return

boolean: whether the given bounding box is completely contained in this bounding box.

Example

use Libraries.Game.Collision.BoundingBox2D
use Libraries.Compute.Vector2
use Libraries.Containers.Array

BoundingBox2D boundingBox
Vector2 minimum
Vector2 maximum
minimum:Set(-1, -1)
maximum:Set(1, 1)
boundingBox:Set(minimum, maximum)

output "The box is centered at (" + boundingBox:GetCenterX() 
    + ", " + boundingBox:GetCenterY() + ") with a width of " 
    +  boundingBox:GetWidth() + " and an height of " + boundingBox:GetHeight()

BoundingBox2D addition
Vector2 minimum2
Vector2 maximum2
minimum:Set(2, 2)
maximum:Set(1.5, 1.5)
addition:Set(minimum, maximum)

if not boundingBox:Contains(addition)
    boundingBox:Extend(addition)
end
 
output "The box, with the addition, is centered at (" + boundingBox:GetCenterX() 
    + ", " + boundingBox:GetCenterY() + ") with a width of " 
    +  boundingBox:GetWidth() + " and a height of " + boundingBox:GetHeight()

Contains(Libraries.Compute.Vector2 point)

This action will test to see if the point indicated by the given Vector2 is contained inside this BoundingBox2D.

Parameters

Return

boolean: whether the point is contained in this BoundingBox2D

Example

use Libraries.Game.Collision.BoundingBox2D
use Libraries.Compute.Vector2
use Libraries.Containers.Array

BoundingBox2D boundingBox
Vector2 minimum
Vector2 maximum
minimum:Set(-1, -1)
maximum:Set(1, 1)
boundingBox:Set(minimum, maximum)

output "The box is centered at (" + boundingBox:GetCenterX() 
    + ", " + boundingBox:GetCenterY() + ") with a width of " 
    +  boundingBox:GetWidth() + " and an height of " + boundingBox:GetHeight()

Vector2 outlier
outlier:Set(5,2)

if not boundingBox:Contains(outlier)
    boundingBox:Extend(outlier)
end
 
output "The box, with the outlier, is centered at (" + boundingBox:GetCenterX() 
    + ", " + boundingBox:GetCenterY() + ") with a width of " 
    +  boundingBox:GetWidth() + " and a height of " + boundingBox:GetHeight()

Copy()

The Copy action will return a new BoundingBox2D with the same bounds as this BoundingBox2D.

Return

Libraries.Game.Collision.BoundingBox2D: an equivalent bounding box

Example

use Libraries.Game.Collision.BoundingBox2D
use Libraries.Compute.Vector2

BoundingBox2D boundingBox
Vector2 minimum
Vector2 maximum
minimum:Set(-1, -1)
maximum:Set(1, 1)

boundingBox:Set(minimum, maximum)

BoundingBox2D copy 
copy:Set(boundingBox:Copy())

Equals(Libraries.Language.Object object)

This action determines if two objects are equal based on their hash code values.

Parameters

Return

boolean: True if the hash codes are equal and false if they are not equal.

Example

use Libraries.Language.Object
use Libraries.Language.Types.Text
Object o
Text t
boolean result = o:Equals(t)

Extend(number x, number y)

This action will extend the BoundingBox2D to contain the point at the given X and Y coordinates.

Parameters

  • number x: The x value of the point to be included
  • number y: The y value of the point to be included

Example

use Libraries.Game.Collision.BoundingBox2D
use Libraries.Compute.Vector2
use Libraries.Containers.Array

BoundingBox2D boundingBox
Vector2 minimum
Vector2 maximum

minimum:Set(-1, -1)
maximum:Set(1, 1)
boundingBox:Set(minimum, maximum)

output "The box is centered at (" + boundingBox:GetCenterX() 
    + ", " + boundingBox:GetCenterY() + ") with a width of " 
    +  boundingBox:GetWidth() + " and an height of " + boundingBox:GetHeight()

number outlierX = 5
number outlierY = 2
boundingBox:Extend(outlierX, outlierY)

output "The box, including the outlier, is centered at (" + boundingBox:GetCenterX() 
    + ", " + boundingBox:GetCenterY() + ") with a width of " 
    +  boundingBox:GetWidth() + " and a height of " + boundingBox:GetHeight()

Extend(Libraries.Compute.Vector2 center, number radius)

This action will extend the BoundingBox2D to contain a circle denoted by a center point and a radius.

Parameters

  • Libraries.Compute.Vector2: The center point of the circle to be included in the extended BoundingBox2D
  • number radius: the radius of the circle to be included in the extended BoundingBox2D

Example

use Libraries.Game.Collision.BoundingBox2D
use Libraries.Compute.Vector2
use Libraries.Containers.Array

BoundingBox2D boundingBox
Vector2 minimum
Vector2 maximum
minimum:Set(-1, -1)
maximum:Set(1, 1)
boundingBox:Set(minimum, maximum)

output "The box is centered at (" + boundingBox:GetCenterX() 
    + ", " + boundingBox:GetCenterY() + ") with a width of " 
    +  boundingBox:GetWidth() + " and an height of " + boundingBox:GetHeight()

Vector2 circleCenter
circleCenter:Set(1, 1)
number radius = 1.5

boundingBox:Extend(circleCenter, radius)
 
output "The box, now also containing the circle, is centered at (" + boundingBox:GetCenterX() 
    + ", " + boundingBox:GetCenterY() + ") with a width of " 
    +  boundingBox:GetWidth() + " and a height of " + boundingBox:GetHeight()

Extend(Libraries.Game.Collision.BoundingBox2D bounds)

This action will extend the BoundingBox2D to contain the provided bounds.

Parameters

Example


use Libraries.Game.Collision.BoundingBox2D
use Libraries.Compute.Vector2
use Libraries.Containers.Array

BoundingBox2D boundingBox
Vector2 minimum
Vector2 maximum
minimum:Set(-1, -1)
maximum:Set(1, 1)
boundingBox:Set(minimum, maximum)

output "The box is centered at (" + boundingBox:GetCenterX() 
    + ", " + boundingBox:GetCenterY() + ") with a width of " 
    +  boundingBox:GetWidth() + " and an height of " + boundingBox:GetHeight()

Vector2 maximum2
Vector2 minimum2
maximum2:Set(2, 2)
minimum2:Set(1.5, 1.5)
BoundingBox2D addition
addition:Set(minimum2, maximum2)

boundingBox:Extend(addition)
 
output "The box, with the additional bounds, is centered at (" + boundingBox:GetCenterX() 
    + ", " + boundingBox:GetCenterY() + ") with a width of " 
    +  boundingBox:GetWidth() + " and a height of " + boundingBox:GetHeight()

Extend(Libraries.Compute.Vector2 point)

This action will extend the BoundingBox2D to include the given point.

Parameters

Example

use Libraries.Game.Collision.BoundingBox2D
use Libraries.Compute.Vector2
use Libraries.Containers.Array

BoundingBox2D boundingBox
Vector2 minimum
Vector2 maximum
Vector2 outlier
minimum:Set(-1, -1)
maximum:Set(1, 1)
boundingBox:Set(minimum, maximum)

output "The box is centered at (" + boundingBox:GetCenterX() 
    + ", " + boundingBox:GetCenterY() + ") with a width of " 
    +  boundingBox:GetWidth() + " and an height of " + boundingBox:GetHeight()

outlier:Set(5, 2)
boundingBox:Extend(outlier)

output "The box, including the outlier, is centered at (" + boundingBox:GetCenterX() 
    + ", " + boundingBox:GetCenterY() + ") with a width of " 
    +  boundingBox:GetWidth() + " and a height of " + boundingBox:GetHeight()

GetCenter()

GetCenter will return a Vector2 object containing the point at the center of the BoundingBox2D.

Return

Libraries.Compute.Vector2: the center point of the BoundingBox2D as a Vector2D

Example

use Libraries.Game.Collision.BoundingBox2D
use Libraries.Compute.Vector2

BoundingBox2D boundingBox
Vector2 minimum
Vector2 maximum
minimum:Set(-1, -1)
maximum:Set(1, 1)

boundingBox:Set(minimum, maximum)
Vector2 center = boundingBox:GetCenter()

output "The center of the bounding box is (" + center:GetX() 
        + ", " + center:GetY() + ") "

GetCenterX()

GetCenterX will return the X coordinate of the center of the BoundingBox2D.

Return

number: the X coordinate of the center point of the BoundingBox2D.

Example

use Libraries.Game.Collision.BoundingBox2D
use Libraries.Compute.Vector2

BoundingBox2D boundingBox
Vector2 minimum
Vector2 maximum
minimum:Set(-1, -1)
maximum:Set(1, 1)

boundingBox:Set(minimum, maximum)

output "The center of the bounding box is (" + boundingBox:GetCenterX() 
        + ", " + boundingBox:GetCenterY() + ") "

GetCenterY()

GetCenterY will return the Y coordinate of the center of the BoundingBox2D.

Return

number: the Y coordinate of the center point of the BoundingBox2D.

Example

use Libraries.Game.Collision.BoundingBox2D
use Libraries.Compute.Vector2

BoundingBox2D boundingBox
Vector2 minimum
Vector2 maximum
minimum:Set(-1, -1)
maximum:Set(1, 1)

boundingBox:Set(minimum, maximum)

output "The center of the bounding box is (" + boundingBox:GetCenterX() 
        + ", " + boundingBox:GetCenterY() + ") "

GetDimensions()

GetDimensions will return the width and height of the BoundingBox2D stored within a Vector2 object.

Return

Libraries.Compute.Vector2: the width and height of the BoundingBox2D stored as the X and Y coordinates of the Vector2 object, respectively.

Example

use Libraries.Game.Collision.BoundingBox2D
use Libraries.Compute.Vector2

BoundingBox2D boundingBox
Vector2 minimum
Vector2 maximum
minimum:Set(-1, -1)
maximum:Set(1, 1)

boundingBox:Set(minimum, maximum)
Vector2 dimensions = boundingBox:GetDimensions()

output "The bounding box is " + dimensions:GetX() 
        + " wide by " + dimensions:GetY() + " tall. "

GetHashCode()

This action gets the hash code for an object.

Return

integer: The integer hash code of the object.

Example

Object o
integer hash = o:GetHashCode()

GetHeight()

GetHeight will return the height of the BoundingBox2D.

Return

number: the height of the BoundingBox2D

Example

use Libraries.Game.Collision.BoundingBox2D
use Libraries.Compute.Vector2

BoundingBox2D boundingBox
Vector2 minimum
Vector2 maximum
minimum:Set(-1, -1)
maximum:Set(1, 1)

boundingBox:Set(minimum, maximum)

output "The bounding box is " + boundingBox:GetWidth() 
        + " wide by " + boundingBox:GetHeight() + " tall. "

GetMaximum()

GetMaximum will return the "maximum" point of the BoundingBox2D. This is specifically the point of the BoundingBox2D with the highest X and Y values. The point will be returned as a Vector2.

Return

Libraries.Compute.Vector2: the maximum point of the BoundingBox2D

Example

use Libraries.Game.Collision.BoundingBox2D
use Libraries.Compute.Vector2

BoundingBox2D boundingBox
Vector2 minimum
Vector2 maximum
minimum:Set(-1, -1)
maximum:Set(1, 1)

boundingBox:Set(minimum, maximum)

output "The maximum of the bounding box is (" + boundingBox:GetMaximum():GetX() 
        + ", " + boundingBox:GetMaximum():GetY() + ")"

GetMinimum()

GetMinimum will return the "minimum" point of the BoundingBox2D. This is specifically the point of the BoundingBox2D with the lowest X and Y values. The point will be returned as a Vector2.

Return

Libraries.Compute.Vector2: the minimum point of the BoundingBox2D

Example

use Libraries.Game.Collision.BoundingBox2D
use Libraries.Compute.Vector2

BoundingBox2D boundingBox
Vector2 minimum
Vector2 maximum
minimum:Set(-1, -1)
maximum:Set(1, 1)

boundingBox:Set(minimum, maximum)

output "The minimum of the bounding box is (" + boundingBox:GetMinimum():GetX() 
        + ", " + boundingBox:GetMinimum():GetY() + ")"

GetPerimeter()

This action returns the perimeter of the BoundingBox2D.

Return

number: the perimeter of this BoundingBox2D

Example

use Libraries.Game.Collision.BoundingBox2D
use Libraries.Compute.Vector2
use Libraries.Containers.Array

BoundingBox2D boundingBox
Vector2 minimum
Vector2 maximum
minimum:Set(-1, -1)
maximum:Set(1, 1)
boundingBox:Set(minimum, maximum)

output "The box has perimeter " + boundingBox:GetPerimeter()

GetWidth()

GetWidth will return the width of the BoundingBox2D.

Return

number: the width of the BoundingBox2D

Example

use Libraries.Game.Collision.BoundingBox2D
use Libraries.Compute.Vector2

BoundingBox2D boundingBox
Vector2 minimum
Vector2 maximum
minimum:Set(-1, -1)
maximum:Set(1, 1)

boundingBox:Set(minimum, maximum)

output "The bounding box is " + boundingBox:GetWidth() 
        + " wide by " + boundingBox:GetHeight() + " tall. "

Intersects(Libraries.Game.Collision.BoundingBox2D bounds)

This action will test to see if this BoundingBox2D intersects with the given BoundingBox2D.

Parameters

Return

boolean: whether the given bounding box intersects this bounding box.

Example

use Libraries.Game.Collision.BoundingBox2D
use Libraries.Compute.Vector2
use Libraries.Containers.Array

BoundingBox2D boundingBox
Vector2 minimum
Vector2 maximum
minimum:Set(-1, -1)
maximum:Set(1, 1)
boundingBox:Set(minimum, maximum)

output "The box is centered at (" + boundingBox:GetCenterX() 
    + ", " + boundingBox:GetCenterY() + ") with a width of " 
    +  boundingBox:GetWidth() + " and an height of " + boundingBox:GetHeight()

BoundingBox2D addition
Vector2 minimum2
Vector2 maximum2
minimum:Set(2, 2)
maximum:Set(0.9, 0.9)
addition:Set(minimum, maximum)

if boundingBox:Intersects(addition)
    boundingBox:Extend(addition)
end
 
output "The box, with the addition, is centered at (" + boundingBox:GetCenterX() 
    + ", " + boundingBox:GetCenterY() + ") with a width of " 
    +  boundingBox:GetWidth() + " and a height of " + boundingBox:GetHeight()

Invalidate()

This action will invalidate the BoundingBox2D, setting its minimum values to positive infinity and setting its maximum values to negative infinity.

Example

use Libraries.Game.Collision.BoundingBox2D
use Libraries.Compute.Vector2
use Libraries.Containers.Array

BoundingBox2D boundingBox
Vector2 minimum
Vector2 maximum
Vector2 newMaximum
minimum:Set(-1, -1)
maximum:Set(1, 1)
boundingBox:Set(minimum, maximum)

output "The box is centered at (" + boundingBox:GetCenterX() 
    + ", " + boundingBox:GetCenterY() + ") with a width of " 
    +  boundingBox:GetWidth() + " and an height of " + boundingBox:GetHeight()

newMaximum:Set(5, 2)

boundingBox:Invalidate()
boundingBox:Extend(minimum)
boundingBox:Extend(newMaximum)


output "The box, with the new maximum, is centered at (" + boundingBox:GetCenterX() 
    + ", " + boundingBox:GetCenterY() + ") with a width of " 
    +  boundingBox:GetWidth() + " and a height of " + boundingBox:GetHeight()

IsValid()

This action will return whether or not the BoundingBox2D defines a real, 2-dimensional area. This requires all of the values of the "minimum" point to be less than the values of the "maximum" point.

Return

boolean: whether the bounding box is valid.

Example

use Libraries.Game.Collision.BoundingBox2D
use Libraries.Compute.Vector2
use Libraries.Containers.Array

BoundingBox2D boundingBox
Vector2 minimum
Vector2 maximum

minimum:Set(-1, -1)
maximum:Set(1, 1)
boundingBox:Set(minimum, maximum)

if boundingBox:IsValid()
    output "The box is valid"
else 
    output "The box is invalid"
end

output "Invalidating..."
boundingBox:Invalidate()

if boundingBox:IsValid()
    output "The box is valid"
else 
    output "The box is invalid"
end

Set(Libraries.Compute.Vector2 min, Libraries.Compute.Vector2 max)

The Set action can be provided with a pair of vectors to set the bounds of this BoundingBox2D. The first vector should represent the "minimum" point of the bounds, or the point with the lowest X and Y values. The second vector should represent the "maximum" point of the bounds, or the point with the highest X and Y values. This action will also calculate the center and dimensions of the BoundingBox2D given the maximum and minimum points.

Parameters

Example

use Libraries.Game.Collision.BoundingBox2D
use Libraries.Compute.Vector2

BoundingBox2D boundingBox
Vector2 minimum
Vector2 maximum
minimum:Set(-1, -1)
maximum:Set(1, 1)

boundingBox:Set(minimum, maximum)

Set(Libraries.Game.Collision.BoundingBox2D bounds)

When provided with a BoundingBox2D as a parameter, the Set action will set the BoundingBox2D to match the bounds of the parameter.

Parameters

Example

use Libraries.Game.Collision.BoundingBox2D
use Libraries.Compute.Vector2

BoundingBox2D boundingBox
Vector2 minimum
Vector2 maximum
minimum:Set(-1, -1)
maximum:Set(1, 1)

boundingBox:Set(minimum, maximum)

BoundingBox2D copy 
copy:Set(boundingBox:Copy())

Set(Libraries.Containers.Array<Libraries.Compute.Vector2> points)

This action will set the BoundingBox2D to the minimum possible size that contains all of the points in the given array.

Parameters

Example

use Libraries.Game.Collision.BoundingBox2D
use Libraries.Compute.Vector2
use Libraries.Containers.Array

BoundingBox2D boundingBox
Vector2 point1   //the minimum point of the boundingBox
Vector2 point2   //the maximum point of the boundingBox
Vector2 outlier  //the outlier in addition to the original boundingBox
point1:Set(-1, -1)
point2:Set(1, 1)
outlier:Set(5, 2)

Array<Vector2> points
//you would want to include the minimum and the maximum points of the 
//original bounding box if you want to add an extra outlier 
//(another way to do this is to use the Extend(Vector2 point) action)

//but if you just want to bound a series of points, you just have to insert 
//the points that you want the box to bound 

points:Add(point1)
points:Add(point2)
points:Add(outlier)

boundingBox:Set(points)

output "The box, including the outlier, is centered at (" + boundingBox:GetCenterX() 
        + ", " + boundingBox:GetCenterY() + ") with a width of " 
        +  boundingBox:GetWidth() + " and a height of " + boundingBox:GetHeight()

TestOverlap(Libraries.Game.Collision.BoundingBox2D boundingBox)

This action tests for overlap between the given BoundingBox2D and this BoundingBox2D

Parameters

Return

boolean: whether the given bounding box overlaps with this bounding box.

Example

use Libraries.Game.Collision.BoundingBox2D
use Libraries.Compute.Vector2
use Libraries.Containers.Array

BoundingBox2D boundingBox
Vector2 minimum
Vector2 maximum
minimum:Set(-1, -1)
maximum:Set(1, 1)
boundingBox:Set(minimum, maximum)

output "The box is centered at (" + boundingBox:GetCenterX() 
    + ", " + boundingBox:GetCenterY() + ") with a width of " 
    +  boundingBox:GetWidth() + " and an height of " + boundingBox:GetHeight()

BoundingBox2D addition
Vector2 minimum2
Vector2 maximum2
minimum:Set(2, 2)
maximum:Set(0.9, 0.9)
addition:Set(minimum, maximum)

if boundingBox:TestOverlap(addition)
    boundingBox:Extend(addition)
end
 
output "The box, with the addition, is centered at (" + boundingBox:GetCenterX() 
    + ", " + boundingBox:GetCenterY() + ") with a width of " 
    +  boundingBox:GetWidth() + " and a height of " + boundingBox:GetHeight()