Skip to content

Commit

Permalink
Fix #165
Browse files Browse the repository at this point in the history
  • Loading branch information
ske2004 committed Mar 11, 2024
1 parent 2d60e20 commit 0d814e1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/umka
32 changes: 28 additions & 4 deletions src/collisions.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// line to line is stolen from: http://jeffreythompson.org/collision-detection/table_of_contents.php
#include <float.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
Expand Down Expand Up @@ -42,14 +43,37 @@ th_point_to_quad(th_vf2 p, th_quad *q, th_vf2 *ic)
return coll;
}

static fu
point_distance(th_vf2 a, th_vf2 b)
{
a.x -= b.x;
a.y -= b.y;
return a.x * a.x + a.y * a.y;
}

uu
th_line_to_quad(th_vf2 b, th_vf2 e, th_quad *q, th_vf2 *ic)
{
for (uu i = 0; i < 4; i++)
if (th_line_to_line(b, e, q->v[i], q->v[(i + 1) % 4], ic))
return 1;
th_vf2 ic_c; // closest incident point
float ic_d = FLT_MAX; // incident distance
bool collision = false;

return 0;
for (uu i = 0; i < 4; i++) {
th_vf2 ic_n; // current incident point

if (th_line_to_line(b, e, q->v[i], q->v[(i + 1) % 4], &ic_n)) {
float ic_nd = point_distance(b, ic_n);
if (ic_nd < ic_d) {
ic_d = ic_nd;
ic_c = ic_n;
}
collision = true;
}
}

*ic = ic_c;

return collision;
}

uu
Expand Down
5 changes: 2 additions & 3 deletions src/raycast.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ th_ray_getcoll(
for (int i = 0; i < sceneLen && *collCount < maxColls; i++) {
th_quad q = th_ent_transform(scene[i]);

if (th_line_to_quad(ra->pos, p2, &q, &colls[*collCount].pos) ||
// in case the ray is completely inside the quad
th_point_to_quad(ra->pos, &q, &colls[*collCount].pos)) {
if (th_point_to_quad(ra->pos, &q, &colls[*collCount].pos) ||
th_line_to_quad(ra->pos, p2, &q, &colls[*collCount].pos)) {
++(*collCount);
}
}
Expand Down

0 comments on commit 0d814e1

Please sign in to comment.