fixed rounding funcs

This commit is contained in:
2025-06-19 16:48:04 -05:00
parent 350d6dd498
commit 6b16c97c64
3 changed files with 77 additions and 31 deletions

View File

@ -52,29 +52,48 @@ void entityUpdate(entity_t *entity) {
fixed248_t newX = entity->x + entity->vx; fixed248_t newX = entity->x + entity->vx;
fixed248_t newY = entity->y + entity->vy; fixed248_t newY = entity->y + entity->vy;
fixed248_t halfTileWH = FIXED248(TILE_WIDTH_HEIGHT / 2, 0);
fixed248_t selfCircR = halfTileWH;
// Check for collisions with the world boundaries // Check for collisions with the world boundaries
// Check for collisions with tiles // Check for collisions with tile
fixed248_t tileStartX = fx248Floor(newX - halfTileWH);
fixed248_t tileStartY = fx248Floor(newY - halfTileWH);
fixed248_t tileEndX = fx248Ceil(newX + halfTileWH);
fixed248_t tileEndY = fx248Ceil(newY + halfTileWH);
for(fixed248_t y = tileStartY; y <= tileEndY; y+= FIXED248_ONE) {
for(fixed248_t x = tileStartX; x <= tileEndX; x+= FIXED248_ONE) {
//
}
}
// uint16_t tileStartX = fx248Tou16(fx248Floor(newX - halfTileWH));
// uint16_t tileStartY = fx248Tou16(fx248Floor(newY - halfTileWH));
// uint16_t tileEndX = fx248Tou16(fx248Ceil(newX + halfTileWH)) + 1;
// uint16_t tileEndY = fx248Tou16(fx248Ceil(newY + halfTileWH)) + 1;
// printf("Checking tiles between (%u, %u) and (%u, %u)\n",
// tileStartX, tileStartY, tileEndX, tileEndY
// );
// for(uint16_t tileY = tileStartY; tileY <= tileEndY; tileY++) {
// for(uint16_t tileX = tileStartX; tileX <= tileEndX; tileX++) {
// }
// }
// Check for collisions with other entities // Check for collisions with other entities
fixed248_t halfTileW = FIXED248(TILE_WIDTH_HEIGHT / 2, 0); entity_t *otherEntity = ENTITIES;
fixed248_t halfTileH = FIXED248(TILE_WIDTH_HEIGHT / 2, 0); do {
fixed248_t selfCircR = halfTileW;
fixed248_t selfCircX = newX - halfTileW;
fixed248_t selfCircY = newY - halfTileH;
for(entity_t *otherEntity = ENTITIES; otherEntity < ENTITIES + ENTITY_COUNT_MAX; ++otherEntity) {
// Skip self and null entities // Skip self and null entities
if(otherEntity == entity || otherEntity->type == ENTITY_TYPE_NULL) continue; if(otherEntity == entity || otherEntity->type == ENTITY_TYPE_NULL) continue;
fixed248_t otherCircX = otherEntity->x - halfTileW; fixed248_t otherCircR = halfTileWH;
fixed248_t otherCircY = otherEntity->y - halfTileH;
fixed248_t otherCircR = halfTileW;
collisionresult_t collision = physicsCheckCircleCircle( collisionresult_t collision = physicsCheckCircleCircle(
selfCircX, selfCircY, selfCircR, newX, newY, selfCircR,
otherCircX, otherCircY, otherCircR otherEntity->x, otherEntity->y, otherCircR
); );
if(!collision.hit) continue; if(!collision.hit) continue;
@ -83,11 +102,7 @@ void entityUpdate(entity_t *entity) {
fixed248_t slideY = fx248Mulf32(collision.normalY, fx248Tof32(collision.depth)); fixed248_t slideY = fx248Mulf32(collision.normalY, fx248Tof32(collision.depth));
newX -= slideX; newX -= slideX;
newY -= slideY; newY -= slideY;
} while(++otherEntity < ENTITIES + ENTITY_COUNT_MAX);
// Update selfCircX/Y for next collision check
selfCircX = newX - halfTileW;
selfCircY = newY - halfTileH;
}
entity->x = newX; entity->x = newX;
entity->y = newY; entity->y = newY;

View File

@ -20,6 +20,11 @@ fixed248_t fx248Fromf32(const float_t b) {
return (fixed248_t)(b * (1 << FIXED248_FRACTION_BITS)); return (fixed248_t)(b * (1 << FIXED248_FRACTION_BITS));
} }
fixed248_t fx248Fromu16(const uint16_t b) {
return (fixed248_t)((int32_t)b << FIXED248_FRACTION_BITS);
}
int32_t fx248Toi32(const fixed248_t a) { int32_t fx248Toi32(const fixed248_t a) {
return (int32_t)(a >> FIXED248_FRACTION_BITS); return (int32_t)(a >> FIXED248_FRACTION_BITS);
@ -33,6 +38,9 @@ float_t fx248Tof32(const fixed248_t a) {
return (float_t)(a / (float_t)(1 << FIXED248_FRACTION_BITS)); return (float_t)(a / (float_t)(1 << FIXED248_FRACTION_BITS));
} }
uint16_t fx248Tou16(const fixed248_t a) {
return (uint16_t)((a >> FIXED248_FRACTION_BITS) & 0xFFFF);
}
fixed248_t fx248Addfx248(const fixed248_t a, const fixed248_t b) { fixed248_t fx248Addfx248(const fixed248_t a, const fixed248_t b) {
return a + b; return a + b;
@ -118,22 +126,26 @@ fixed248_t fx248Divf32(const fixed248_t a, const float_t b) {
int32_t fx248Floor(const fixed248_t a) { fixed248_t fx248Floor(const fixed248_t a) {
return (int32_t)(a >> FIXED248_FRACTION_BITS); return a & ~((1 << FIXED248_FRACTION_BITS) - 1);
} }
int32_t fx248Ceil(const fixed248_t a) { fixed248_t fx248Ceil(const fixed248_t a) {
return (int32_t)(( if(a & ((1 << FIXED248_FRACTION_BITS) - 1)) {
a + ((1 << FIXED248_FRACTION_BITS) - 1) return (a & ~((1 << FIXED248_FRACTION_BITS) - 1)) + (1 << FIXED248_FRACTION_BITS);
) >> FIXED248_FRACTION_BITS); }
return a;
} }
int32_t fx248Round(const fixed248_t a) { fixed248_t fx248Round(const fixed248_t a) {
return (int32_t)( if(a & ((1 << (FIXED248_FRACTION_BITS - 1)) - 1)) {
(a + (1 << (FIXED248_FRACTION_BITS - 1)) return (a & ~((1 << FIXED248_FRACTION_BITS) - 1)) + (1 << FIXED248_FRACTION_BITS);
) >> FIXED248_FRACTION_BITS); }
return a & ~((1 << FIXED248_FRACTION_BITS) - 1);
} }
uint32_t fx248Flooru32(const fixed248_t a) { uint32_t fx248Flooru32(const fixed248_t a) {
return (uint32_t)((a >> FIXED248_FRACTION_BITS) & 0xFFFFFFFF); return (uint32_t)((a >> FIXED248_FRACTION_BITS) & 0xFFFFFFFF);
} }

View File

@ -18,6 +18,7 @@ typedef int32_t fixed248_t;
((i) << FIXED248_FRACTION_BITS) + \ ((i) << FIXED248_FRACTION_BITS) + \
(((f) * FIXED248_HIGH_MULTIPLIER) / 100) \ (((f) * FIXED248_HIGH_MULTIPLIER) / 100) \
)) ))
#define FIXED248_ONE (FIXED248(1, 0))
/** /**
* Convert an int32_t value to a fixed248_t value. * Convert an int32_t value to a fixed248_t value.
@ -43,6 +44,14 @@ fixed248_t fx248Fromu32(const uint32_t b);
*/ */
fixed248_t fx248Fromf32(const float_t b); fixed248_t fx248Fromf32(const float_t b);
/**
* Convert a uint16_t value to a fixed248_t value.
*
* @param b The uint16_t value to convert.
* @return The converted fixed248_t value.
*/
fixed248_t fx248Fromu16(const uint16_t b);
/** /**
@ -69,6 +78,14 @@ uint32_t fx248Tou32(const fixed248_t a);
*/ */
float_t fx248Tof32(const fixed248_t a); float_t fx248Tof32(const fixed248_t a);
/**
* Convert a fixed248_t value to a uint16_t value.
*
* @param a The fixed248_t value to convert.
* @return The converted uint16_t value.
*/
uint16_t fx248Tou16(const fixed248_t a);
/** /**
@ -229,7 +246,7 @@ fixed248_t fx248Divf32(const fixed248_t a, const float_t b);
* @param a The fixed248_t value to convert. * @param a The fixed248_t value to convert.
* @return The converted int32_t value. * @return The converted int32_t value.
*/ */
int32_t fx248Floor(const fixed248_t a); fixed248_t fx248Floor(const fixed248_t a);
/** /**
* Convert a fixed248_t value to an int32_t value, rounding towards positive * Convert a fixed248_t value to an int32_t value, rounding towards positive
@ -238,7 +255,7 @@ int32_t fx248Floor(const fixed248_t a);
* @param a The fixed248_t value to convert. * @param a The fixed248_t value to convert.
* @return The converted int32_t value. * @return The converted int32_t value.
*/ */
int32_t fx248Ceil(const fixed248_t a); fixed248_t fx248Ceil(const fixed248_t a);
/** /**
* Convert a fixed248_t value to an int32_t value, rounding to the nearest * Convert a fixed248_t value to an int32_t value, rounding to the nearest
@ -247,7 +264,9 @@ int32_t fx248Ceil(const fixed248_t a);
* @param a The fixed248_t value to convert. * @param a The fixed248_t value to convert.
* @return The converted int32_t value. * @return The converted int32_t value.
*/ */
int32_t fx248Round(const fixed248_t a); fixed248_t fx248Round(const fixed248_t a);
/** /**
* Convert a fixed248_t value to a uint32_t value, rounding towards zero. * Convert a fixed248_t value to a uint32_t value, rounding towards zero.