Skip to content

Commit

Permalink
Add coll::rectIntersect (#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
ske2004 committed Jul 10, 2024
1 parent 1d161c2 commit 6ea698d
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 1 deletion.
26 changes: 26 additions & 0 deletions samples/rect/main.um
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import (
"window.um"
"canvas.um"
"th.um"
"input.um"
"image.um"
"rect.um"
"coll.um"
)

fn init*() {
window::setup("Rect test", 800, 600)

window::onFrame.register({
a := rect::mk(400-40, 300-40, 80, 80)
b := rect::mk(input::getMousePos().x-25, input::getMousePos().y-25, 50, 50)

canvas::drawRectLines(0x999999FF, a)
canvas::drawRectLines(0x000000FF, b)

if coll::rectToRect(a, b) {
r := coll::rectIntersect(a, b)
canvas::drawRectLines(0xFF0000FF, r)
}
})
}
12 changes: 11 additions & 1 deletion src/bindings.c
Original file line number Diff line number Diff line change
Expand Up @@ -1334,6 +1334,15 @@ umth_coll_rect_to_rect(UmkaStackSlot *p, UmkaStackSlot *r)
umkaGetResult(p, r)->intVal = th_rect_to_rect(&r1, &r2);
}

void
umth_coll_rect_intersect(UmkaStackSlot *p, UmkaStackSlot *r)
{
th_rect r1 = *(th_rect *)umkaGetParam(p, 0);
th_rect r2 = *(th_rect *)umkaGetParam(p, 1);

*(th_rect *)umkaGetResult(p, r)->ptrVal = th_rect_intersect(r1, r2);
}

// fn umth_nav_mesh_add_quad(m: ^Mesh, q: th::Quad)
void
umth_nav_mesh_add_quad(UmkaStackSlot *p, UmkaStackSlot *r)
Expand Down Expand Up @@ -1497,13 +1506,14 @@ _th_umka_bind(void *umka)
umkaAddFunc(umka, "umth_transform_vf2", &umth_transform_vf2);
umkaAddFunc(umka, "umth_transform_transform", &umth_transform_transform);

// colisions
// collisions
umkaAddFunc(umka, "umth_coll_line_to_line", &umth_coll_line_to_line);
umkaAddFunc(umka, "umth_coll_point_to_quad", &umth_coll_point_to_quad);
umkaAddFunc(umka, "umth_coll_line_to_quad", &umth_coll_line_to_quad);
umkaAddFunc(umka, "umth_coll_quad_to_quad", &umth_coll_quad_to_quad);
umkaAddFunc(umka, "umth_coll_point_to_rect", &umth_coll_point_to_rect);
umkaAddFunc(umka, "umth_coll_rect_to_rect", &umth_coll_rect_to_rect);
umkaAddFunc(umka, "umth_coll_rect_intersect", &umth_coll_rect_intersect);

// nav
umkaAddFunc(umka, "umth_nav_mesh_add_quad", &umth_nav_mesh_add_quad);
Expand Down
11 changes: 11 additions & 0 deletions src/collisions.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,14 @@ th_coll_point_on_rect(th_vf2 p, th_rect *r)
{
return p.x >= r->x && p.y >= r->y && p.x <= r->x + r->w && p.y <= r->y + r->h;
}

th_rect
th_rect_intersect(th_rect a, th_rect b)
{
float x = fmax(a.x, b.x);
float y = fmax(a.y, b.y);
float w = fmin(a.x + a.w, b.x + b.w) - x;
float h = fmin(a.y + a.h, b.y + b.h) - y;

return (th_rect){x, y, w, h};
}
16 changes: 16 additions & 0 deletions src/staembed.c
Original file line number Diff line number Diff line change
Expand Up @@ -2881,6 +2881,14 @@ const char *th_em_modulesrc[] = {
"\treturn umth_coll_rect_to_rect(r1, r2)\n"
"}\n"
"\n"
"fn umth_coll_rect_intersect(r1, r2: rect::Rect): rect::Rect\n"
"//~~fn rectIntersect\n"
"// Get the intersection of two rects.\n"
"fn rectIntersect*(r1, r2: rect::Rect): rect::Rect {\n"
"//~~\n"
"\treturn umth_coll_rect_intersect(r1, r2)\n"
"}\n"
"\n"
"",
"import(\"image.um\")\n"
"\n"
Expand Down Expand Up @@ -6253,6 +6261,14 @@ const char *th_em_moduledocs[] = {
"\n"
"---------\n"
"\n"
"fn rectIntersect\n"
"\n"
"fn rectIntersect*(r1, r2: rect::Rect): rect::Rect {\n"
"\n"
"Get the intersection of two rects.\n"
"\n"
"---------\n"
"\n"
"",
"Placeholder images\n"
"\n"
Expand Down
1 change: 1 addition & 0 deletions src/thextdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ THEXT(uu, th_coll_on_tilemap, th_ent *e, th_tmap *t, th_vf2 *ic, th_vf2 *tc);
THEXT(uu, th_coll_point_on_rect, th_vf2 p, th_rect *r);
THEXT(uu, th_rect_to_rect, th_rect *r1, th_rect *r2);
THEXT(uu, th_line_to_tilemap, th_vf2 b, th_vf2 e, th_tmap *t, th_vf2 *ic);
THEXT(th_rect, th_rect_intersect, th_rect a, th_rect b);
THEXT(th_quad, th_ent_transform, th_ent *e);
THEXT(void, th_ent_draw, th_ent *o);
THEXT(void, th_ent_getcoll, th_ent *e, th_ent **scene, uu count, uu *collC, uu maxColls, th_coll *colls);
Expand Down
2 changes: 2 additions & 0 deletions src/tophat.h
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,8 @@ uu
th_rect_to_rect(th_rect *r1, th_rect *r2);
uu
th_line_to_tilemap(th_vf2 b, th_vf2 e, th_tmap *t, th_vf2 *ic);
th_rect
th_rect_intersect(th_rect a, th_rect b);

// entity
th_quad
Expand Down
8 changes: 8 additions & 0 deletions umka/coll.um
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,11 @@ fn rectToRect*(r1, r2: rect::Rect): bool {
return umth_coll_rect_to_rect(r1, r2)
}

fn umth_coll_rect_intersect(r1, r2: rect::Rect): rect::Rect
//~~fn rectIntersect
// Get the intersection of two rects.
fn rectIntersect*(r1, r2: rect::Rect): rect::Rect {
//~~
return umth_coll_rect_intersect(r1, r2)
}

0 comments on commit 6ea698d

Please sign in to comment.