Libraries.Game.Collision.DynamicBoundingVolumeTree2D Documentation

DynamicBoundingVolumeTree2D is the tree structure for storing the bounding boxes for collidable Item2D objects. The tree is a complete binary tree and stores the bounding boxes in a hierarchal structure with the parent node containing a bounding box that bounds the bounding boxes of both of its children. This class is used internally by BroadphaseCollision2D.

Example Code

use Libraries.Game.Collision.DynamicBoundingVolumeTree2D
use Libraries.Game.Collision.BoundingBox2D
use Libraries.Game.Collision.Item2DNode

DynamicBoundingVolumeTree2D tree
BoundingBox2D boundingBox1
BoundingBox2D boundingBox2

Item2DNode item1
Item2DNode item2

integer id1 = tree:CreateNode(boundingBox1, item1)
integer id2 = tree:CreateNode(boundingBox2, item2)
output "Tree has height " + tree:GetHeight()

tree:DestroyNode(id2)
output "After removing node 2 , tree has height " + tree:GetHeight()

Inherits from: Libraries.Language.Object

Variables Table

VariablesDescription
integer NULL_NODE

Actions Documentation

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)

ComputeHeight()

This action computes the height of the tree

Return

integer: the height of the tree

Example

use Libraries.Game.Collision.DynamicBoundingVolumeTree2D
use Libraries.Game.Collision.BoundingBox2D
use Libraries.Game.Collision.Item2DNode

    DynamicBoundingVolumeTree2D tree
    BoundingBox2D boundingBox1
    BoundingBox2D boundingBox2

    Item2DNode item1
    Item2DNode item2

    integer id1 = tree:CreateNode(boundingBox1, item1)
    integer id2 = tree:CreateNode(boundingBox2, item2)
    output "Tree has height " + tree:ComputeHeight()

    tree:DestroyNode(id2)
    output "After removing node 2 , tree has height " + tree:ComputeHeight()

CreateNode(Libraries.Game.Collision.BoundingBox2D boundingBox, Libraries.Game.Collision.Item2DNode node)

This action creates a new node for the Dynamic Bounding Volume Tree with the given BoundingBox and given Item and inserts the new node as a leaf in the tree. This action is used internally.

Parameters

Return

integer: The integer id of the new tree node

Example

use Libraries.Game.Collision.DynamicBoundingVolumeTree2D
use Libraries.Game.Collision.BoundingBox2D
use Libraries.Game.Collision.Item2DNode

    DynamicBoundingVolumeTree2D tree
    BoundingBox2D boundingBox

    Item2DNode item

    output "This item was added to node number " + tree:CreateNode(boundingBox, item)

DestroyNode(integer nodeID)

This action removes from the Dynamic Bounding Volume Tree the node with the given node ID. This action is used internally.

Parameters

  • integer nodeID: The integer id number for the node which is to be removed

Example

use Libraries.Game.Collision.DynamicBoundingVolumeTree2D
use Libraries.Game.Collision.BoundingBox2D
use Libraries.Game.Collision.Item2DNode

    DynamicBoundingVolumeTree2D tree
    BoundingBox2D boundingBox

    Item2DNode item
    integer id = tree:CreateNode(boundingBox, item)
    output "This item was added to node number " + id

    tree:DestroyNode(id)
    output "Node number " + id + " was removed from the tree."

DestroyNode(Libraries.Game.Collision.DynamicBoundingVolumeTreeNode2D node)

This action removes the given node from the tree. Only leaf nodes will be removed. This action is used internally.

Parameters

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)

GetBoundingBox(integer nodeID)

This action returns the bounding box contained in the tree node with the given ID. Note that this bounding box is slightly larger than the bounding box of the item contained in this node. This action is used internally.

Parameters

  • integer nodeID: The integer id of the tree node containing the BoundingBox2D to be returned

Return

Libraries.Game.Collision.BoundingBox2D: the specified BoundingBox2D

Example

use Libraries.Game.Collision.DynamicBoundingVolumeTree2D
use Libraries.Game.Collision.BoundingBox2D
use Libraries.Game.Collision.Item2DNode

    DynamicBoundingVolumeTree2D tree
    BoundingBox2D boundingBox1
    BoundingBox2D boundingBox2

    Item2DNode item1
    Item2DNode item2

    integer id1 = tree:CreateNode(boundingBox1, item1)
    integer id2 = tree:CreateNode(boundingBox2, item2)

    BoundingBox2D result
    result = tree:GetBoundingBox(id2)

GetBoundingBox(Libraries.Game.Collision.DynamicBoundingVolumeTreeNode2D node)

This action returns the bounding box contained in the given tree node. Note that this bounding box is slightly larger than the bounding box of the item contained in this node. This action is used internally.

Parameters

Return

Libraries.Game.Collision.BoundingBox2D: the specified BoundingBox2D

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()

This action returns the height of the tree

Return

integer: the height of the tree

Example

use Libraries.Game.Collision.DynamicBoundingVolumeTree2D
use Libraries.Game.Collision.BoundingBox2D
use Libraries.Game.Collision.Item2DNode

    DynamicBoundingVolumeTree2D tree
    BoundingBox2D boundingBox1
    BoundingBox2D boundingBox2

    Item2DNode item1
    Item2DNode item2

    integer id1 = tree:CreateNode(boundingBox1, item1)
    integer id2 = tree:CreateNode(boundingBox2, item2)
    output "Tree has height " + tree:GetHeight()

    tree:DestroyNode(id2)
    output "After removing node 2 , tree has height " + tree:GetHeight()

GetItemNode(integer nodeID)

This action returns the Item2DNode contained in the tree node with the given ID This is an internal action.

Parameters

  • integer nodeID: The integer id of the tree node containing the Item2DNode to be returned

Return

Libraries.Game.Collision.Item2DNode: The specified Item2DNode

Example


use Libraries.Game.Collision.DynamicBoundingVolumeTree2D
use Libraries.Game.Collision.BoundingBox2D
use Libraries.Game.Collision.Item2DNode

    DynamicBoundingVolumeTree2D tree
    BoundingBox2D boundingBox1
    BoundingBox2D boundingBox2

    Item2DNode item1
    Item2DNode item2

    integer id1 = tree:CreateNode(boundingBox1, item1)
    integer id2 = tree:CreateNode(boundingBox2, item2)

    Item2DNode result
    result = tree:GetItemNode(id2)

GetItemNode(Libraries.Game.Collision.DynamicBoundingVolumeTreeNode2D node)

This action returns the Item2Dcontained in the given tree node This action is used internally

Parameters

Return

Libraries.Game.Collision.Item2DNode: The specified Item2DNode

GetMaximumBalance()

This action computes the maximum balance of the tree. The maximum balance is the maximum difference of the heights of the two children of any node. Attributes: Returns the maximum balance of the tree Atributes: Example use Libraries.Game.Collision.DynamicBoundingVolumeTree2D use Libraries.Game.Collision.BoundingBox2D use Libraries.Game.Collision.Item2DNode DynamicBoundingVolumeTree2D tree BoundingBox2D boundingBox1 BoundingBox2D boundingBox2 BoundingBox2D boundingBox3 Item2DNode item1 Item2DNode item2 Item2DNode item3 integer id1 = tree:CreateNode(boundingBox1, item1) integer id2 = tree:CreateNode(boundingBox2, item2) integer id3 = tree:CreateNode(boundingBox3, item3) output "Tree has maximum balance " + tree:GetMaximumBalance()

Return

integer:

GetNodeCount()

This action returns the number of nodes in the DynamicBoundingVolumeTree2D. This action is used internally.

Return

integer: the number of nodes in the DynamicBoundingVolumeTree2D

Example

use Libraries.Game.Collision.DynamicBoundingVolumeTree2D
use Libraries.Game.Collision.BoundingBox2D
use Libraries.Game.Collision.Item2DNode

    DynamicBoundingVolumeTree2D tree
    BoundingBox2D boundingBox1
    BoundingBox2D boundingBox2

    Item2DNode item1
    Item2DNode item2

    integer id1 = tree:CreateNode(boundingBox1, item1)
    integer id2 = tree:CreateNode(boundingBox2, item2)

    output "The tree has " + tree:GetNodeCount() + " nodes."

GetPerimeterRatio()

This action computes the ratio of the sum of the node perimeters to the root perimeter.

Return

number: the perimeterRatio of the tree

Example


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

    DynamicBoundingVolumeTree2D tree
    BoundingBox2D boundingBox1
    BoundingBox2D boundingBox2

    Vector2 minimum1
    Vector2 minimum2
    Vector2 maximum1
    Vector2 maximum2

    minimum1:Set(0, 0)
    minimum2:Set(0.5, 0.5)
    maximum1:Set(1, 1)
    maximum2:Set(1.5, 1.5)
    boundingBox1:Set(minimum1, maximum1)
    boundingBox2:Set(minimum2, maximum2)

    Item2DNode item1
    Item2DNode item2

    integer id1 = tree:CreateNode(boundingBox1, item1)
    integer id2 = tree:CreateNode(boundingBox2, item2)

    output "The perimeter ratio is " + tree:GetPerimeterRatio()

MoveNode(integer nodeID, Libraries.Game.Collision.BoundingBox2D boundingBox, Libraries.Compute.Vector2 displacement)

This action moves the Item2DNode contained in the node with the given node ID by the given displacement vector. If the node moves outside of its existing bounding box, then the object is removed from the tree, a new bounding box is calculated, and it is reinserted into the tree. Otherwise, the node is left where it is in the tree. This action is used internally.

Parameters

Return

boolean: whether the item was removed and reinserted into the tree

Example

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

    DynamicBoundingVolumeTree2D tree
    BoundingBox2D boundingBox
    Vector2 displacement

    Item2DNode item
    integer id = tree:CreateNode(boundingBox, item)
    output "This item was added to node number " + id

    displacement:Set(1, 5)
    if not tree:MoveNode(id, boundingBox, displacement)
        output "The item in node number " + id + " did not need reinserted"
    end

MoveNode(Libraries.Game.Collision.DynamicBoundingVolumeTreeNode2D node, Libraries.Game.Collision.BoundingBox2D boundingBox, Libraries.Compute.Vector2 displacement)

This action moves the object. If the object moves outside of its existing big bounding box, then the object is removed from the tree, a new bounding box is calculated, and it is reinserted into the tree. Otherwise, the action returns immediately.

Parameters

Return

boolean: Whether the item is reinserted into the tree

Query(Libraries.Game.Collision.BroadphaseCollision2D broadphase, Libraries.Game.Collision.BoundingBox2D boundingBox)

This action queries the tree for bounding boxes that overlap with the given bounding box and returns all leafs whose bounding boxes intersect the given bounding box in a list.

Parameters

Example

use Libraries.Game.Collision.DynamicBoundingVolumeTree2D
use Libraries.Game.Collision.BoundingBox2D
use Libraries.Game.Collision.Item2DNode
use Libraries.Compute.Vector2
use Libraries.Game.Collision.BroadphaseCollision2D

    BroadphaseCollision2D broadphase
    DynamicBoundingVolumeTree2D tree
    BoundingBox2D boundingBox1
    BoundingBox2D boundingBox2

    Vector2 minimum1
    Vector2 minimum2
    Vector2 maximum1
    Vector2 maximum2

    minimum1:Set(-1,-1)
    minimum2:Set(-2, -2)
    maximum1:Set(0, 0)
    maximum2:Set(-1, -1)
    boundingBox1:Set(minimum1, maximum1)
    boundingBox2:Set(minimum2, maximum2)

    Item2DNode item1
    Item2DNode item2

    integer id1 = tree:CreateNode(boundingBox1, item1)
    integer id2 = tree:CreateNode(boundingBox2, item2)

    output "The tree has " + tree:GetNodeCount() + " nodes."

    BoundingBox2D overlap
    Vector2 minimumOverlap
    Vector2 maximumOverlap
    minimumOverlap:Set(-1.5, -1.5)
    maximumOverlap:Set(-0.5, -0.5)
    overlap:Set(minimumOverlap, maximumOverlap)

    tree:Query(broadphase, overlap)