From 6af88cc394c2642bb2c3aa6ab712f61158339087 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Fri, 20 Jun 2025 10:06:17 -0500 Subject: [PATCH] Taking a break from hitboxes --- data/map project.tiled-session | 8 +++--- data/map.tmj | 11 ++++---- src/dusk/entity/entity.c | 46 ++++++++++++++++++++++++++++++++++ src/dusk/physics/physics.c | 11 ++++++++ src/dusk/physics/physics.h | 21 ++++++++++++++++ src/dusk/util/fixed.c | 8 ++++++ src/dusk/util/fixed.h | 16 +++++++++++- 7 files changed, 110 insertions(+), 11 deletions(-) diff --git a/data/map project.tiled-session b/data/map project.tiled-session index 9da6811..52f03e9 100644 --- a/data/map project.tiled-session +++ b/data/map project.tiled-session @@ -1,5 +1,5 @@ { - "activeFile": "overworld.tsx", + "activeFile": "map.tmj", "expandedProjectPaths": [ "templates", "." @@ -16,8 +16,8 @@ "scale": 4, "selectedLayer": 2, "viewCenter": { - "x": 6625.125, - "y": 6667.875 + "x": 6599.375, + "y": 6795.125 } }, "overworld.tsx": { @@ -35,8 +35,8 @@ "project": "map project.tiled-project", "property.type": "int", "recentFiles": [ - "map.tmj", "overworld.tsx", + "map.tmj", "entities.tsx" ], "tileset.lastUsedFilter": "Tiled tileset files (*.tsx *.xml)", diff --git a/data/map.tmj b/data/map.tmj index fe06d5f..bd88d28 100644 --- a/data/map.tmj +++ b/data/map.tmj @@ -40,9 +40,9 @@ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 18, 19, 20, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 34, 35, 36, 1, 1], "height":16, "width":16, "x":400, @@ -270,7 +270,6 @@ }], "height":32, "id":1, - "locked":true, "name":"Base Layer", "opacity":1, "startx":384, @@ -426,8 +425,8 @@ "type":"player_spawn", "visible":true, "width":16, - "x":6591.33333333333, - "y":6787 + "x":6607.58333333333, + "y":6944.25 }, { "id":7, diff --git a/src/dusk/entity/entity.c b/src/dusk/entity/entity.c index 649f8df..fdb75bf 100644 --- a/src/dusk/entity/entity.c +++ b/src/dusk/entity/entity.c @@ -90,6 +90,11 @@ void entityUpdate(entity_t *entity) { uint8_t chunkTileX = tileX % CHUNK_WIDTH; uint8_t chunkTileY = tileY % CHUNK_HEIGHT; tile_t tile = chunk->tilesBase[chunkTileY * CHUNK_WIDTH + chunkTileX]; + + fixed248_t lx = fx248Mulfx248(x, FIXED248(TILE_WIDTH_HEIGHT, 0)); + fixed248_t ty = fx248Mulfx248(y, FIXED248(TILE_WIDTH_HEIGHT, 0)); + fixed248_t rx = fx248Addfx248(lx, FIXED248(TILE_WIDTH_HEIGHT, 0)); + fixed248_t by = fx248Addfx248(ty, FIXED248(TILE_WIDTH_HEIGHT, 0)); // Determine tile collision type collisionresult_t collision; @@ -104,6 +109,47 @@ void entityUpdate(entity_t *entity) { ); break; + case TILE_SOLID_TRIANGLE_TOP_RIGHT: + collision = physicsCheckCircleTriangle( + newX, newY, selfCircR, + lx, ty, + rx, ty, + rx, by + ); + break; + + // case TILE_SOLID_TRIANGLE_TOP_LEFT: + // collision = physicsCheckCircleTriangle( + // newX, newY, selfCircR, + // lx, ty, + // rx, ty, + // lx, by + // ); + // break; + // case TILE_SOLID_TRIANGLE_BOTTOM_RIGHT: + // collision = physicsCheckCircleTriangle( + // newX, newY, selfCircR, + // fx248Mulfx248(x, FIXED248(TILE_WIDTH_HEIGHT, 0)), + // fx248Addfx248(fx248Mulfx248(y, FIXED248(TILE_WIDTH_HEIGHT, 0)), FIXED248(TILE_WIDTH_HEIGHT, 0)), + // fx248Addfx248(fx248Mulfx248(x, FIXED248(TILE_WIDTH_HEIGHT, 0)), FIXED248(TILE_WIDTH_HEIGHT, 0)), + // fx248Addfx248(fx248Mulfx248(y, FIXED248(TILE_WIDTH_HEIGHT, 0)), FIXED248(TILE_WIDTH_HEIGHT, 0)), + // fx248Mulfx248(x, FIXED248(TILE_WIDTH_HEIGHT, 0)), + // fx248Mulfx248(y, FIXED248(TILE_WIDTH_HEIGHT, 0)) + // ); + // break; + + // case TILE_SOLID_TRIANGLE_BOTTOM_LEFT: + // collision = physicsCheckCircleTriangle( + // newX, newY, selfCircR, + // fx248Mulfx248(x, FIXED248(TILE_WIDTH_HEIGHT, 0)), + // fx248Addfx248(fx248Mulfx248(y, FIXED248(TILE_WIDTH_HEIGHT, 0)), FIXED248(TILE_WIDTH_HEIGHT, 0)), + // fx248Addfx248(fx248Mulfx248(x, FIXED248(TILE_WIDTH_HEIGHT, 0)), FIXED248(TILE_WIDTH_HEIGHT, 0)), + // fx248Addfx248(fx248Mulfx248(y, FIXED248(TILE_WIDTH_HEIGHT, 0)), FIXED248(TILE_WIDTH_HEIGHT, 0)), + // fx248Addfx248(fx248Mulfx248(x, FIXED248(TILE_WIDTH_HEIGHT, 0)), FIXED248(TILE_WIDTH_HEIGHT, 0)), + // fx248Mulfx248(y, FIXED248(TILE_WIDTH_HEIGHT, 0)) + // ); + // break; + default: continue; } diff --git a/src/dusk/physics/physics.c b/src/dusk/physics/physics.c index 45c49d9..8f4dab3 100644 --- a/src/dusk/physics/physics.c +++ b/src/dusk/physics/physics.c @@ -97,4 +97,15 @@ collisionresult_t physicsCheckCircleAABB( result.hit = true; return result; +} + +collisionresult_t physicsCheckCircleTriangle( + fixed248_t circleX, fixed248_t circleY, fixed248_t circleR, + fixed248_t triX0, fixed248_t triY0, + fixed248_t triX1, fixed248_t triY1, + fixed248_t triX2, fixed248_t triY2 +) { + collisionresult_t result; + result.hit = false; + return result; } \ No newline at end of file diff --git a/src/dusk/physics/physics.h b/src/dusk/physics/physics.h index 8bc32f6..fedfa20 100644 --- a/src/dusk/physics/physics.h +++ b/src/dusk/physics/physics.h @@ -46,4 +46,25 @@ collisionresult_t physicsCheckCircleAABB( fixed248_t circleX, fixed248_t circleY, fixed248_t circleR, fixed248_t aabb, fixed248_t aabbY, fixed248_t aabbWidth, fixed248_t aabbHeight +); + +/** + * Check for collision between a circle and a triangle. + * + * @param circleX X coordinate of the circle's center. + * @param circleY Y coordinate of the circle's center. + * @param circleR Radius of the circle. + * @param triX0 X coordinate of the first vertex of the triangle. + * @param triY0 Y coordinate of the first vertex of the triangle. + * @param triX1 X coordinate of the second vertex of the triangle. + * @param triY1 Y coordinate of the second vertex of the triangle. + * @param triX2 X coordinate of the third vertex of the triangle. + * @param triY2 Y coordinate of the third vertex of the triangle. + * @return A collisionresult_t structure containing collision information. + */ +collisionresult_t physicsCheckCircleTriangle( + fixed248_t circleX, fixed248_t circleY, fixed248_t circleR, + fixed248_t triX0, fixed248_t triY0, + fixed248_t triX1, fixed248_t triY1, + fixed248_t triX2, fixed248_t triY2 ); \ No newline at end of file diff --git a/src/dusk/util/fixed.c b/src/dusk/util/fixed.c index e54b454..3612116 100644 --- a/src/dusk/util/fixed.c +++ b/src/dusk/util/fixed.c @@ -190,4 +190,12 @@ fixed248_t fx248Max(const fixed248_t a, const fixed248_t b) { fixed248_t fx248Min(const fixed248_t a, const fixed248_t b) { return (a < b) ? a : b; +} + +fixed248_t fx248Clamp( + const fixed248_t a, + const fixed248_t min, + const fixed248_t max +) { + return (a < min) ? min : (a > max) ? max : a; } \ No newline at end of file diff --git a/src/dusk/util/fixed.h b/src/dusk/util/fixed.h index 63bbc54..bd8c4c3 100644 --- a/src/dusk/util/fixed.h +++ b/src/dusk/util/fixed.h @@ -335,4 +335,18 @@ fixed248_t fx248Max(const fixed248_t a, const fixed248_t b); * @param b Second fixed248_t value. * @return The minimum of the two values. */ -fixed248_t fx248Min(const fixed248_t a, const fixed248_t b); \ No newline at end of file +fixed248_t fx248Min(const fixed248_t a, const fixed248_t b); + +/** + * Clamp a fixed248_t value between a minimum and maximum value. + * + * @param a The fixed248_t value to clamp. + * @param min The minimum value to clamp to. + * @param max The maximum value to clamp to. + * @return The clamped fixed248_t value. + */ +fixed248_t fx248Clamp( + const fixed248_t a, + const fixed248_t min, + const fixed248_t max +); \ No newline at end of file