Stop relying on positive infinity for a 'very large distance' value

This commit is contained in:
UnknownShadow200 2024-01-08 22:20:20 +11:00
parent d9b01a0a11
commit bb5ca6a957
3 changed files with 10 additions and 9 deletions

View File

@ -499,20 +499,22 @@ void Entities_Remove(EntityID id) {
EntityID Entities_GetClosest(struct Entity* src) {
Vec3 eyePos = Entity_GetEyePosition(src);
Vec3 dir = Vec3_GetDirVector(src->Yaw * MATH_DEG2RAD, src->Pitch * MATH_DEG2RAD);
float closestDist = MATH_POS_INF;
Vec3 dir = Vec3_GetDirVector(src->Yaw * MATH_DEG2RAD, src->Pitch * MATH_DEG2RAD);
float closestDist = -200.5f; /* NOTE: was previously positive infinity */
EntityID targetId = ENTITIES_SELF_ID;
float t0, t1;
int i;
for (i = 0; i < ENTITIES_SELF_ID; i++) { /* because we don't want to pick against local player */
for (i = 0; i < ENTITIES_SELF_ID; i++) /* because we don't want to pick against local player */
{
struct Entity* entity = Entities.List[i];
if (!entity) continue;
if (!Intersection_RayIntersectsRotatedBox(eyePos, dir, entity, &t0, &t1)) continue;
if (Intersection_RayIntersectsRotatedBox(eyePos, dir, entity, &t0, &t1) && t0 < closestDist) {
if (closestDist < -200 || t0 < closestDist) {
closestDist = t0;
targetId = (EntityID)i;
targetId = (EntityID)i;
}
}
return targetId;

View File

@ -10,7 +10,6 @@
#define MATH_DEG2RAD (MATH_PI / 180.0f)
#define MATH_RAD2DEG (180.0f / MATH_PI)
#define MATH_LARGENUM 1000000000.0f
#define MATH_POS_INF ((float)(1e+300 * 1e+300))
#define Math_Deg2Packed(x) ((cc_uint8)((x) * 256.0f / 360.0f))
#define Math_Packed2Deg(x) ((x) * 360.0f / 256.0f)

View File

@ -218,9 +218,9 @@ void Searcher_CalcTime(Vec3* vel, struct AABB *entityBB, struct AABB* blockBB, f
float dy = vel->y > 0.0f ? blockBB->Min.y - entityBB->Max.y : entityBB->Min.y - blockBB->Max.y;
float dz = vel->z > 0.0f ? blockBB->Min.z - entityBB->Max.z : entityBB->Min.z - blockBB->Max.z;
*tx = vel->x == 0.0f ? MATH_POS_INF : Math_AbsF(dx / vel->x);
*ty = vel->y == 0.0f ? MATH_POS_INF : Math_AbsF(dy / vel->y);
*tz = vel->z == 0.0f ? MATH_POS_INF : Math_AbsF(dz / vel->z);
*tx = vel->x == 0.0f ? MATH_LARGENUM : Math_AbsF(dx / vel->x);
*ty = vel->y == 0.0f ? MATH_LARGENUM : Math_AbsF(dy / vel->y);
*tz = vel->z == 0.0f ? MATH_LARGENUM : Math_AbsF(dz / vel->z);
if (entityBB->Max.x >= blockBB->Min.x && entityBB->Min.x <= blockBB->Max.x) *tx = 0.0f; /* Inlined XIntersects */
if (entityBB->Max.y >= blockBB->Min.y && entityBB->Min.y <= blockBB->Max.y) *ty = 0.0f; /* Inlined YIntersects */