以下是一个基于AABB树的简单碰撞检测实现,包括插入、删除和查询。其中,节点存储的是AABB的中心坐标和半边长。
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAX_DEPTH = 20;
const int MAX_OBJECTS = 20;
class AABB
{
public:
AABB(float x, float y, float half_w, float half_h) : x(x), y(y), half_w(half_w), half_h(half_h) {}
AABB() : AABB(0, 0, 0, 0) {}
float x, y, half_w, half_h;
float left() const { return x - half_w; }
float right() const { return x + half_w; }
float bottom() const { return y - half_h; }
float top() const { return y + half_h; }
bool intersects(const AABB &other) const
{
return left() < other.right() && right() > other.left() &&
bottom() < other.top() && top() > other.bottom();
}
};
class AABBNode
{
public:
AABBNode(const AABB &aabb, AABBNode *parent, int depth) : aabb(aabb), parent(parent), depth(depth) {}
void insert(const AABB &aabb)
{
if (!is_leaf())
{
if (left->aabb.intersects(aabb))
{
left->insert(aabb);
}
if (right->aabb.intersects(aabb))
{
right->insert(aabb);
}
aabb = aabb_union(left->aabb, right->aabb);
}
else
{
objs.push_back(aabb);
if (objs.size() > MAX_OBJECTS && depth < MAX_DEPTH)
{
split();
}
}
}
void remove(const AABB &aabb)
{
auto it = find(objs.begin(), objs.end(), aabb);
if (it != objs.end())
{
objs.erase(it);
if (parent)
{
auto &siblings = is_left_child() ? parent->right->objs : parent->left->objs;
siblings.insert(siblings.end(), objs.begin(), objs.end());
if (parent->parent && parent->is_leaf())
{
parent->merge();
}
else if (!is_leaf())
{
aabb = aabb_union(left->aabb, right->aabb);
}
}
}
}
bool collide(const AABB &target, vector<AABB> &collisions) const
{
bool collision = false;
if (aabb.intersects(target))
{
if (is_leaf())
{
for (const auto &obj : objs)
{
if (obj.intersects(target))
{
collisions.push_back(obj);
collision = true;
}
}
}
else
{
collision |= left->collide(target, collisions);
collision |= right->collide(target, collisions);
}
}
return collision;
}
private:
AABB aabb;
AABBNode *parent = nullptr;
AABBNode *left = nullptr, *right = nullptr;
vector<AABB> objs;
int depth;
bool is_leaf() const { return !left && !right; }
bool is_left_child() const { return parent && parent->left == this; }
void split()
{
left = new AABBNode(AABB(aabb.left(), aabb.top(), aabb.half_w / 2, aabb.half_h / 2), this, depth + 1);
right = new AABBNode(AABB(aabb.right(), aabb.bottom(), aabb.half_w / 2, aabb.half_h / 2), this, depth + 1);
for (auto obj : objs)
{
if (left->aabb.intersects(obj))
{
left->insert(obj);
}
if (right->aabb.intersects(obj))
{
right->insert(obj);
}
}
objs.clear();
}
void merge()
{
objs.clear();
auto &siblings = is_left_child() ? parent->right->objs : parent->left->objs;
for (auto obj : siblings)
{
objs.push_back(obj);
}
delete left;
delete right;
left = nullptr;
right = nullptr;
}
AABB aabb_union(const AABB &a, const AABB &b) const
{
auto x = (a.x + b.x) / 2;
auto y = (a.y + b.y) / 2;
auto w = max(abs(a.x - b.x) / 2 + a.half_w, abs(a.y - b.y) / 2 + a.half_h);
auto h = max(abs(a.x - b.x) / 2 + b.half_w, abs(a.y - b.y) / 2 + b.half_h);
return AABB(x, y, w, h);
}
};
int main()
{
AABB root_aabb(0, 0, 100, 100);
AABBNode root_node(root_aabb, nullptr, 0);
root_node.insert(AABB(30, 30, 10, 10));
root_node.insert(AABB(70, 70, 10, 10));
root_node.insert(AABB(50, 50, 20, 20));
vector<AABB> collisions;
root_node.collide(AABB(55, 55, 10, 10), collisions);
for (auto aabb : collisions)
{
cout << aabb.x << ", " << aabb.y << ", " << aabb.half_w << ", " << aabb.half_h << endl;
}
}
在上述代码中,我们实现了AABB树的基本操作:
insert():向AABB树中插入一个对象。
remove():从AABB树中删除一个对象。
collide():检测一个指定区域的碰撞,返回所有与该区域相交的对象。
当AABB树中一个节点中的对象数量超过了一定阈值时,该节点会被划分成两个子节点,递归重复该过程;当移除一个对象后,如果该节点不再需要存在,则和它的兄弟节点合并。这一递归过程会在树的深度达到一个阈值或者对象数量达到一个阈值时停止,以避免过度拆分。