[英]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. 我正在使用c ++在UE4中创建一个四叉树,我对c ++很新,所以我需要帮助。
My main concern is about visualizing the Quadtree. 我主要担心的是可视化四叉树。
Basically I have a native c++ class called Quadtree. 基本上我有一个名为Quadtree的本地c ++类。 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. 在这个类的内部我有所有的功能,如插入点,细分等...我有一个名为C_Quadtree的AActor c ++类(我知道这个名字),它有一个Blueprint可调用函数来插入点,以及一个可视化方法。
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 : 我已声明一个Array来存储TreeNodes,如下所示:
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. 但是,我不知道如何将创建的Quadtree从本地Quadtree类添加到Actor C_Quadtree类内部的数组中。
Here's the subdivide function declared in the Quadtree class : 这是Quadtree类中声明的细分函数:
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. 您可以使用数组来存储Quadtree指针的副本,但我建议在Quadtree对象本身上使用递归函数来实现功能。 The recursion would follow the pointers down to every branch of the quadtree, ending at nullptr. 递归将跟随指针向下到四叉树的每个分支,结束于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. 要保持View-In-Editor和Blueprint兼容性,您可能需要为Quadtree类添加UE4 API。 This is easiest done by doing File -> "New C++ Class" -> options for Subclass of UObject, then copy pasting your existing code. 这是最简单的方法,通过执行文件 - >“新建C ++类” - > UObject子类的选项,然后复制粘贴现有代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.