-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquadtree.ts
More file actions
155 lines (130 loc) · 3.78 KB
/
quadtree.ts
File metadata and controls
155 lines (130 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import type { Node } from '@xyflow/svelte';
import type { CollisionAlgorithm } from '.';
import { Quadtree, Rectangle } from '@timohausmann/quadtree-ts';
type Rect = Rectangle<{
id: string;
node: Node;
moved: boolean;
}>;
const quadtreeMargin = 25;
function getBoxesFromNodes(nodes: Node[], margin: number = 0) {
const rects: Rect[] = new Array(nodes.length);
let minX = Infinity;
let minY = Infinity;
let maxX = -Infinity;
let maxY = -Infinity;
for (let i = 0; i < nodes.length; i++) {
const node = nodes[i];
const x = node.position.x - margin;
const y = node.position.y - margin;
const width = node.width! + margin * 2;
const height = node.height! + margin * 2;
minX = Math.min(minX, x);
minY = Math.min(minY, y);
maxX = Math.max(maxX, x + width);
maxY = Math.max(maxY, y + height);
rects[i] = new Rectangle({
x: node.position.x - margin,
y: node.position.y - margin,
width: node.width! + margin * 2,
height: node.height! + margin * 2,
data: { id: node.id, node, moved: false }
});
}
return {
rects,
quadtreeSize: {
width: maxX - minX + quadtreeMargin * 2,
height: maxY - minY + quadtreeMargin * 2,
x: minX - quadtreeMargin,
y: minY - quadtreeMargin
}
};
}
export const quadtree: CollisionAlgorithm = (
nodes,
{ iterations = 50, overlapThreshold = 0.5, margin = 0 }
) => {
const { rects, quadtreeSize: initialQuadtreeSize } = getBoxesFromNodes(nodes, margin);
let quadtreeSize = { ...initialQuadtreeSize };
let quadtree = new Quadtree({ ...quadtreeSize });
for (const rect of rects) {
quadtree.insert(rect);
}
let numIterations = 0;
for (let iter = 0; iter <= iterations; iter++) {
let moved = false;
for (let i = 0; i < rects.length; i++) {
const A = rects[i];
const candidates = quadtree.retrieve(A) as Rect[];
for (const B of candidates) {
if (A.data!.id === B.data!.id) continue;
// Calculate center positions
const centerAX = A.x + A.width * 0.5;
const centerAY = A.y + A.height * 0.5;
const centerBX = B.x + B.width * 0.5;
const centerBY = B.y + B.height * 0.5;
// Calculate distance between centers
const dx = centerAX - centerBX;
const dy = centerAY - centerBY;
// Calculate overlap along each axis
const px = (A.width + B.width) * 0.5 - Math.abs(dx);
const py = (A.height + B.height) * 0.5 - Math.abs(dy);
// Check if there's significant overlap
if (px > overlapThreshold && py > overlapThreshold) {
A.data!.moved = B.data!.moved = moved = true;
// Resolve along the smallest overlap axis
if (px < py) {
// Move along x-axis
const sx = dx > 0 ? 1 : -1;
const moveAmount = (px / 2) * sx;
A.x += moveAmount;
B.x -= moveAmount;
} else {
// Move along y-axis
const sy = dy > 0 ? 1 : -1;
const moveAmount = (py / 2) * sy;
A.y += moveAmount;
B.y -= moveAmount;
}
quadtreeSize = {
width: Math.max(
quadtreeSize.width,
A.x + A.width + quadtreeMargin * 2,
B.x + B.width + quadtreeMargin * 2
),
height: Math.max(
quadtreeSize.height,
A.y + A.height + quadtreeMargin * 2,
B.y + B.height + quadtreeMargin * 2
),
x: Math.min(quadtreeSize.x, A.x - quadtreeMargin, B.x - quadtreeMargin),
y: Math.min(quadtreeSize.y, A.y - quadtreeMargin, B.y - quadtreeMargin)
};
}
}
}
numIterations++;
// Early exit if no overlaps were found
if (!moved) {
break;
}
quadtree = new Quadtree({ ...quadtreeSize });
for (const rect of rects) {
quadtree.insert(rect);
}
}
const newNodes = rects.map((rect) => {
if (rect.data!.moved) {
return {
...rect.data!.node,
position: {
x: rect.x + margin,
y: rect.y + margin
}
};
}
return rect.data!.node;
});
return { newNodes, numIterations };
};