summaryrefslogtreecommitdiff
path: root/examples/sectorview.c
diff options
context:
space:
mode:
authorKonAki <konaki@anokonaki.org>2026-10-11 22:06:56 +0800
committerKonAki <konaki@anokonaki.org>2026-10-11 22:06:56 +0800
commit20bbe2731a2d04b6dde65e9f893ad360915a889b (patch)
tree787880beefd26783f75c8399a586970c036d3ca9 /examples/sectorview.c
parent95bd48a7b0556adab5250cae766ee9dd9c6b2cde (diff)
world: rays and line of sight through sectors, terrain, tiles and shapes
Diffstat (limited to 'examples/sectorview.c')
-rw-r--r--examples/sectorview.c15
1 files changed, 13 insertions, 2 deletions
diff --git a/examples/sectorview.c b/examples/sectorview.c
index 821ce52..2d5b5df 100644
--- a/examples/sectorview.c
+++ b/examples/sectorview.c
@@ -4,7 +4,8 @@
* build: ./build.sh -d -m 3d,sector,world,font obj/sectorview
* run: obj/sectorview (from the folder with examples/)
*
- * click to grab the mouse, tab lets go. w and s walk, a and d strafe, space jumps, e presses a button close to you.
+ * click to grab the mouse, tab lets go, a click after that shoots a ray and the top line says what it hit. w and s walk, a and d strafe,
+ * space jumps, e presses a button close to you.
* the door is a box in the world that the program lifts, nothing else about it is built in
* the map is examples/maps/house.kmap, it is plain text so change it and run again. try moving the watcher
*/
@@ -118,6 +119,16 @@ static int event(kon_program_t *program, const kon_event_t *ev, void *user)
int i, n, ids[8];
if (kon_fpEvent(&state->input, program, ev)) return 1;
+ if (ev->type == KON_EVENT_MOUSE_DOWN && kon_isMouseCaptured(program->window)) {
+ static const char *const kinds[] = { "nothing", "a wall", "the floor", "the ceiling", "a collider", "the ground", "a tile" };
+ kon_rayHit_t hit;
+ kon_vec3_t eye = KON_VEC3(state->body.position.x, state->feet + EYE, state->body.position.z);
+ kon_vec3_t dir = KON_VEC3(sinf(state->input.yaw) * cosf(state->input.pitch), sinf(state->input.pitch), -cosf(state->input.yaw) * cosf(state->input.pitch));
+
+ if (kon_worldRay(&state->world, eye, dir, 50.0f, &hit)) snprintf(state->note, sizeof(state->note), "shot %s, %.1f away", kinds[hit.kind], (double)hit.distance);
+ else snprintf(state->note, sizeof(state->note), "shot, hit nothing");
+ return 1;
+ }
if (ev->type == KON_EVENT_KEY_DOWN && ev->key == KON_KEY_SPACE) {
kon_bodyJump(&state->body);
return 1;
@@ -229,7 +240,7 @@ static void draw(kon_program_t *program, kon_framebuffer_t *game, void *user)
kon_fillRectangle(game, game->width / 2 - 3, game->height / 2, 7, 1, COL_NOTE);
kon_fillRectangle(game, game->width / 2, game->height / 2 - 3, 1, 7, COL_NOTE);
if (program->font) {
- snprintf(line, sizeof(line), "room %d click to look, e presses buttons, space jumps", state->body.sector);
+ snprintf(line, sizeof(line), "room %d click shoots a ray, e presses buttons, space jumps", state->body.sector);
kon_drawText(game, program->font, 8, 16, line, COL_DIM);
if (state->note[0]) kon_drawText(game, program->font, 8, 30, state->note, COL_NOTE);
}