diff --git a/src/dusk/entity/entity.c b/src/dusk/entity/entity.c index 2bff5c7..85594eb 100644 --- a/src/dusk/entity/entity.c +++ b/src/dusk/entity/entity.c @@ -62,58 +62,48 @@ void entityUpdate(entity_t *entity) { fixed248_t selfCircR = halfTileWH; // Check for collisions with tiles - uint8_t tileChecks = ENTITY_TILE_COLISSION_RESOLUTION_COUNT; - do { - // loop to resolve tile collisions multiple times to handle sliding - // correctly - bool tileResolved = false; + fixed248_t tileStartX = fx248Floor(fx248Divfx248( + (newX - halfTileWH), FIXED248(TILE_WIDTH_HEIGHT, 0) + )); + fixed248_t tileStartY = fx248Floor(fx248Divfx248( + (newY - halfTileWH), FIXED248(TILE_WIDTH_HEIGHT, 0) + )); + fixed248_t tileEndX = fx248Ceil(fx248Divfx248( + (newX + halfTileWH), FIXED248(TILE_WIDTH_HEIGHT, 0) + )); + fixed248_t tileEndY = fx248Ceil(fx248Divfx248( + (newY + halfTileWH), FIXED248(TILE_WIDTH_HEIGHT, 0) + )); - // Compute affected tile range - fixed248_t tileStartX = fx248Floor(fx248Divfx248( - (newX - halfTileWH), FIXED248(TILE_WIDTH_HEIGHT, 0) - )); - fixed248_t tileStartY = fx248Floor(fx248Divfx248( - (newY - halfTileWH), FIXED248(TILE_WIDTH_HEIGHT, 0) - )); - fixed248_t tileEndX = fx248Ceil(fx248Divfx248( - (newX + halfTileWH), FIXED248(TILE_WIDTH_HEIGHT, 0) - )); - fixed248_t tileEndY = fx248Ceil(fx248Divfx248( - (newY + halfTileWH), FIXED248(TILE_WIDTH_HEIGHT, 0) - )); + // For each tile + for(fixed248_t y = tileStartY; y <= tileEndY; y += FIXED248_ONE) { + for(fixed248_t x = tileStartX; x <= tileEndX; x += FIXED248_ONE) { + uint16_t tileX = fx248Tou16(x); + uint16_t tileY = fx248Tou16(y); + uint16_t chunkX = tileX / CHUNK_WIDTH; + uint16_t chunkY = tileY / CHUNK_HEIGHT; + chunk_t *chunk = chunkGetChunkAt(chunkX, chunkY); + if(chunk == NULL) continue; - // For each tile - for(fixed248_t y = tileStartY; y <= tileEndY; y += FIXED248_ONE) { - for(fixed248_t x = tileStartX; x <= tileEndX; x += FIXED248_ONE) { - uint16_t tileX = fx248Tou16(x); - uint16_t tileY = fx248Tou16(y); - uint16_t chunkX = tileX / CHUNK_WIDTH; - uint16_t chunkY = tileY / CHUNK_HEIGHT; - chunk_t *chunk = chunkGetChunkAt(chunkX, chunkY); - if(chunk == NULL) continue; + uint8_t chunkTileX = tileX % CHUNK_WIDTH; + uint8_t chunkTileY = tileY % CHUNK_HEIGHT; + tile_t tile = chunk->tilesBase[chunkTileY * CHUNK_WIDTH + chunkTileX]; - uint8_t chunkTileX = tileX % CHUNK_WIDTH; - uint8_t chunkTileY = tileY % CHUNK_HEIGHT; - tile_t tile = chunk->tilesBase[chunkTileY * CHUNK_WIDTH + chunkTileX]; - - collisionresult_t collision = physicsCheckCircleTile( - selfCircX, selfCircY, selfCircR, x, y, tile + collisionresult_t collision = physicsCheckCircleTile( + selfCircX, selfCircY, selfCircR, x, y, tile + ); + if(collision.hit && collision.depth > FIXED248(0, 1)) { + fixed248_t slideX = fx248Mulfx248( + collision.normalX, collision.depth ); - if(collision.hit && collision.depth > FIXED248(0, 1)) { - fixed248_t slideX = fx248Mulfx248( - collision.normalX, collision.depth - ); - fixed248_t slideY = fx248Mulfx248( - collision.normalY, collision.depth - ); - newX -= slideX; - newY -= slideY; - tileResolved = true; - } + fixed248_t slideY = fx248Mulfx248( + collision.normalY, collision.depth + ); + newX -= slideX; + newY -= slideY; } } - if(!tileResolved) break; // no more overlaps - } while(--tileChecks > 0); + } // Check for collisions with other entities entity_t *otherEntity = ENTITIES; diff --git a/src/dusk/entity/entity.h b/src/dusk/entity/entity.h index 545d0ce..2ce559d 100644 --- a/src/dusk/entity/entity.h +++ b/src/dusk/entity/entity.h @@ -11,7 +11,6 @@ #include "util/fixed.h" #define ENTITY_COUNT_MAX 32 -#define ENTITY_TILE_COLISSION_RESOLUTION_COUNT 4 typedef enum { ENTITY_DIR_SOUTH = 0,