以下是一个基于四叉树的碰撞检测算法,可以用于判断两个 AABB 盒子之间是否发生了碰撞。
#include <iostream>
#include <vector>
using namespace std;
// 定义结构体来表示一个二维向量
struct Vector2 {
double x;
double y;
Vector2(double _x = 0, double _y = 0) : x(_x), y(_y) {}
};
// 定义结构体来表示 AABB 盒子
struct AABBBox {
Vector2 center;
double halfExtent;
AABBBox(Vector2 _center = Vector2(0, 0), double _halfExtent = 0) : center(_center), halfExtent(_halfExtent) {}
};
// 定义结构体来表示四叉树节点
struct QuadTreeNode {
vector<AABBBox*> boxes;
QuadTreeNode* children[4];
QuadTreeNode() {
for (int i = 0; i < 4; i++) {
children[i] = nullptr;
}
}
};
class QuadTree {
public:
// 构造函数
QuadTree(double _width, double _height, int _depthLimit, int _boxLimit) : width(_width), height(_height), depthLimit(_depthLimit), boxLimit(_boxLimit) {
root = new QuadTreeNode();
}
// 析构函数
~QuadTree() {
clear(root);
}
// 添加 AABB 盒子
void insert(AABBBox* box) {
insert(root, box, 0, 0, width, height);
}
// 查找发生碰撞的 AABB 盒子
void queryCollisions(vector<pair<AABBBox*, AABBBox*>>& collisions) {
queryCollisions(root, collisions);
}
private:
double width;
double height;
int depthLimit;
int boxLimit;
QuadTreeNode* root;
// 释放四叉树节点
void clear(QuadTreeNode* node) {
if (node == nullptr) {
return;
}
for (int i = 0; i < 4; i++) {
clear(node->children[i]);
}
for (auto& box : node->boxes) {
delete box;
}
delete node;
}
// 插入 AABB 盒子
void insert(QuadTreeNode* node, AABBBox* box, double left, double top, double right, double bottom, int depth = 0) {
if (depth++ > depthLimit || left > box->center.x + box->halfExtent || top > box->center.y + box->halfExtent || right < box->center.x - box->halfExtent || bottom < box->center.y - box->halfExtent) {
return;
}
if (node->boxes.size() < boxLimit) {
node->boxes.push_back(box);
} else {
double midX = (left + right) / 2;
double midY = (top + bottom) / 2;
if (node->children[0] == nullptr) {
node->children[0] = new QuadTreeNode();
node->children[1] = new QuadTreeNode();
node->children[2] = new QuadTreeNode();
node->children[3] = new QuadTreeNode();
}
insert(node->children[0], box, left, top, midX, midY, depth);
insert(node->children[1], box, midX, top, right, midY, depth);
insert(node->children[2], box, left, midY, midX, bottom, depth);
insert(node->children[3], box, midX, midY, right, bottom, depth);
}
}
// 查找发生碰撞的 AABB 盒子
void queryCollisions(QuadTreeNode* node, vector<pair<AABBBox*, AABBBox*>>& collisions) {
if (node == nullptr) {
return;
}
if (node->children[0] == nullptr) {
for (int i = 0; i < node->boxes.size(); i++) {
for (int j = i+1; j < node->boxes.size(); j++) {
if (isCollided(node->boxes[i], node->boxes[j])) {
collisions.emplace_back(node->boxes[i], node->boxes[j]);
}
}
}
} else {
for (auto& child : node->children) {
queryCollisions(child, collisions);
}
}
}
// 判断两个 AABB 盒子是否发生碰撞
bool isCollided(AABBBox* a, AABBBox* b) {
double dx = abs(a->center.x - b->center.x);
double dy = abs(a->center.y - b->center.y);
double halfExtentA = a->halfExtent;
double halfExtentB = b->halfExtent;
if (dx > halfExtentA + halfExtentB || dy > halfExtentA + halfExtentB) {
return false;
}
double overlapX = halfExtentA + halfExtentB - dx;
double overlapY = halfExtentA + halfExtentB - dy;
return (overlapX < overlapY) ? (dx <= halfExtentA + halfExtentB && dy <= overlapX) : (dx <= overlapY && dy <= halfExtentA + halfExtentB);
}
};
int main() {
// 创建 AABB 盒子
AABBBox box1(Vector2(10, 10), 5);
AABBBox box2(Vector2(15, 15), 10);
AABBBox box3(Vector2(40, 40), 20);
AABBBox box4(Vector2(50, 50), 10);
// 创建四叉树
QuadTree qt(100, 100, 3, 2);
qt.insert(&box1);
qt.insert(&box2);
qt.insert(&box3);
qt.insert(&box4);
// 查找发生碰撞的 AABB 盒子
vector<pair<AABBBox*, AABBBox*>> collisions;
qt.queryCollisions(collisions);
// 输出结果
for (auto& collision : collisions) {
cout << "Box [" << collision.first->center.x << ", " << collision.first->center.y << ", " << collision.first->halfExtent << "] and Box [" << collision.second->center.x << ", " << collision.second->center.y << ", " << collision.second->halfExtent << "] collided." << endl;
}
// 释放 AABB 盒子内存
return 0;
}