mirror of
https://github.com/zoriya/Quadtree.git
synced 2026-06-06 11:52:45 +00:00
Making collisions works
This commit is contained in:
+18
-12
@@ -11,22 +11,26 @@
|
||||
//X1 is on the line or X2 is on the line or both X are on each side of the map
|
||||
bool collision_overlapx(qt_intrect r1, qt_intrect r2)
|
||||
{
|
||||
if (r1.x >= r2.x && r1.x <= r2.x + r2.w)
|
||||
if (r1.x > r2.x && r1.x < r2.x + r2.w)
|
||||
return (true);
|
||||
if (r1.x + r1.w > r2.x && r1.x + r1.w <= r2.x + r2.w)
|
||||
if (r1.x + r1.w > r2.x && r1.x + r1.w < r2.x + r2.w)
|
||||
return (true);
|
||||
if (r1.x <= r2.x && r1.x + r1.w >= r2.x + r2.w)
|
||||
if (r1.x < r2.x && r1.x + r1.w > r2.x + r2.w)
|
||||
return (true);
|
||||
if (r1.x == r2.x)
|
||||
return (true);
|
||||
return (false);
|
||||
}
|
||||
|
||||
bool collision_overlapy(qt_intrect r1, qt_intrect r2)
|
||||
{
|
||||
if (r1.y >= r2.y && r2.y > r1.y - r1.h)
|
||||
if (r1.y - r1.h < r2.y && r1.y - r1.h > r2.y - r2.h)
|
||||
return (true);
|
||||
if (r1.y - r1.h >= r2.y && r1.y - r1.h <= r2.y - r2.h)
|
||||
if (r1.y < r2.y && r1.y - r1.h > r2.y - r2.h)
|
||||
return (true);
|
||||
if (r1.y <= r2.y && r1.y - r1.h >= r2.y - r2.h)
|
||||
if (r1.y < r2.y && r1.y > r2.y - r2.h)
|
||||
return (true);
|
||||
if (r1.y == r2.y)
|
||||
return (true);
|
||||
return (false);
|
||||
}
|
||||
@@ -34,16 +38,16 @@ bool collision_overlapy(qt_intrect r1, qt_intrect r2)
|
||||
qt_collision collision_complete(qt_collision col, qt_intrect r1, qt_intrect r2)
|
||||
{
|
||||
if (collision_overlapy(r1, r2)) {
|
||||
if (r1.x < r2.x)
|
||||
if (r1.x <= r2.x)
|
||||
col.distance_right = MIN(col.distance_right, r2.x - (r1.x + r1.w));
|
||||
if (r2.x < r1.x)
|
||||
if (r2.x <= r1.x)
|
||||
col.distance_left = MIN(col.distance_left, r1.x - (r2.x + r2.w));
|
||||
}
|
||||
if (collision_overlapx(r1, r2)) {
|
||||
if (r1.y > r2.y)
|
||||
col.distance_down = MIN(col.distance_top, r1.y - (r2.y + r2.h));
|
||||
if (r2.y > r1.y)
|
||||
col.distance_top = MIN(col.distance_down, r2.y - (r1.y + r1.h));
|
||||
if (r1.y >= r2.y)
|
||||
col.distance_down = MIN(col.distance_down, r1.y - r2.y - r1.h);
|
||||
if (r2.y >= r1.y)
|
||||
col.distance_top = MIN(col.distance_top, r2.y - r1.y - r2.h);
|
||||
}
|
||||
col.distance_down = MAX(0, col.distance_down);
|
||||
col.distance_top = MAX(0, col.distance_top);
|
||||
@@ -71,6 +75,8 @@ qt_collision collision_get_real_info(quadtree *tree, int id, qt_collision col)
|
||||
return (col);
|
||||
for (int i = 0; i < cap && ((qt_object *)tree->objects)[i].id != -1; i++) {
|
||||
tmp = &((qt_object *)tree->objects)[i];
|
||||
if (tmp->id == id)
|
||||
continue;
|
||||
col = collision_complete(col, obj->rect, tmp->rect);
|
||||
}
|
||||
return (col);
|
||||
|
||||
Reference in New Issue
Block a user