Files
godot-demo-holes/src/rectangletree.cpp
Collin Campbell 36c648cfe5 Creation of the RTree Godot Class
Basic defintions, we are going to see if it fits the use case.
2025-11-21 20:09:02 -05:00

44 lines
1.3 KiB
C++

#include "rectangletree.hpp"
#include "godot_cpp/core/class_db.hpp"
#include "godot_cpp/variant/packed_int32_array.hpp"
#include "godot_cpp/variant/packed_vector2_array.hpp"
#include "godot_cpp/variant/typed_array.hpp"
RectangleTree::RectangleTree() {}
void RectangleTree::_bind_methods() {
ClassDB::bind_method(
godot::D_METHOD("add", "min_bound", "max_bound", "polygon_index"),
&RectangleTree::add);
ClassDB::bind_method(godot::D_METHOD("remove", "point"),
&RectangleTree::remove);
ClassDB::bind_method(godot::D_METHOD("query", "point"),
&RectangleTree::query);
}
void RectangleTree::add(const Vector2 &min_bound, const Vector2 &max_bound,
const int &polygon_index) {
// convert into c++ types from godot types
Rect rect = Rect(min_bound.x, min_bound.y, max_bound.x, max_bound.y);
tree.Insert(rect.min, rect.max, polygon_index);
}
void RectangleTree::remove(const int &polygon_index) {
tree.Remove(polygon_index);
}
PackedInt32Array RectangleTree::query(const Vector2 &point) {
Rect rect = Rect(point.x, point.y, point.x, point.y);
PackedInt32Array return_value;
auto callbackLambda = [&return_value](int index) {
return_value.push_back(index);
return true;
};
tree.Search(rect.min, rect.max, callbackLambda);
return return_value;
}