简体   繁体   中英

How can I visualize my quadtree in UE4 / c++

I'm making a Quadtree in UE4 with c++, I'm quite new to c++, so I need help on this.

My main concern is about visualizing the Quadtree.

Basically I have a native c++ class called Quadtree. Inside of this class I have all the functions like Insert Point, Subdivide, etc... And I have an AActor c++ class called C_Quadtree (dumb name, I know), that has a Blueprint callable function to insert points, and a visualization method.

Here's how I try to visualize it :

    void AC_Quadtree::show(Quadtree* Node)
{
    FVector BoxCenter = FVector(Node->GetBoundary().GetCenter(), 0);
    FVector BoxExtent = FVector(Node->GetBoundary().GetExtent(), 0);
    DrawDebugBox(GetWorld(), BoxCenter, BoxExtent, FColor::White, false, -1, 0, 3);
}

    void AC_Quadtree::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);

    for (Quadtree *Node : TreeNodes) {
        show(Node);
    }
}

I have declared an Array to store the TreeNodes like so :

TArray<Quadtree*> TreeNodes; 

However, I have no Idea how I could add the created Quadtrees from the native Quadtree class, into the array that is inside of the Actor C_Quadtree class.

Here's the subdivide function declared in the Quadtree class :

void Quadtree::subdivide()
{   
    //Init local variables to make the Math clearer.
    FVector2D Center = Boundary.GetCenter();
    FVector2D HalfExtent = Boundary.GetExtent() / 2;

    NorthWest = new Quadtree( FVector2D(Center.X + HalfExtent.X, Center.Y - HalfExtent.Y), HalfExtent);
    NorthEast = new Quadtree( Center + HalfExtent, HalfExtent);
    SouthWest = new Quadtree( Center - HalfExtent, HalfExtent);
    SouthEast = new Quadtree( FVector2D(Center.X - HalfExtent.X, Center.Y + HalfExtent.Y), HalfExtent);
}

Any Ideas?

By squashing your quadtree into an array, you lose the functionality of a quadtree. You could use an array to store copies of your Quadtree pointers, but I recommend using recursive functions on the Quadtree objects themselves to implement functionality. The recursion would follow the pointers down to every branch of the quadtree, ending at nullptr. Example function:

Quadtree::SubdivideEach()
{
    if(NorthWest != nullptr)
        NorthWest->SubdivideEach();
    else
        subdivide();
    if(NorthEast != nullptr)
        NorthEast->SubdivideEach();
    else
        subdivide();
    //and the others
}

To maintain View-In-Editor and Blueprint compatibility, you may want to add in the UE4 API for your Quadtree class. This is easiest done by doing File -> "New C++ Class" -> options for Subclass of UObject, then copy pasting your existing code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM