Skip to content

Commit e3f139b

Browse files
committed
Adjusts "UseAccurateSphereToRectCollision" boundary checks + Collision Detections only calculates and registers the distance only when necessary
1 parent 99790ab commit e3f139b

File tree

2 files changed

+42
-20
lines changed

2 files changed

+42
-20
lines changed

GeneralsMD/Code/GameEngine/Include/GameLogic/PartitionManager.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ enum DistanceCalculationType CPP_11(: Int)
164164
//=====================================
165165
enum HeightBoundaryCheckType CPP_11(: Int)
166166
{
167-
DEFAULT_HEIGHT_CHECK = 0,
168-
SKIP_HEIGHT_CHECK = 1,
167+
DEFAULT_HEIGHT_CHECK = 0,
168+
SKIP_HEIGHT_CHECK = 1,
169169
BOUNDARY_HEIGHT_CHECK = 2
170170
};
171171

@@ -178,6 +178,7 @@ struct CollideLocAndNormal
178178
Coord3D loc;
179179
Coord3D normal;
180180
Real distSqr;
181+
Bool getDistance;
181182
};
182183

183184
//=====================================

GeneralsMD/Code/GameEngine/Source/GameLogic/Object/PartitionManager.cpp

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -456,9 +456,12 @@ static void testRotatedPointsAgainstRect(
456456
avg->y += pts->y;
457457
*avgTot += 1;
458458

459-
Real distanceSqr = sqr(ptx_new) + sqr(pty_new);
460-
if(*minDistSqr > distanceSqr)
461-
*minDistSqr = distanceSqr;
459+
if(minDistSqr)
460+
{
461+
Real distanceSqr = sqr(ptx_new) + sqr(pty_new);
462+
if(*minDistSqr > distanceSqr)
463+
*minDistSqr = distanceSqr;
464+
}
462465
}
463466
}
464467
}
@@ -841,7 +844,8 @@ static Bool xy_collideTest_Rect_Circle(const CollideInfo *a, const CollideInfo *
841844
//cinfo->distSqr = minDist > 0.0f ? sqr(minDist) : 0.0f;
842845
//cinfo->distSqr = minDistSqr;
843846
//}
844-
cinfo->distSqr = sqr(distance);
847+
if(cinfo->getDistance)
848+
cinfo->distSqr = sqr(distance);
845849
}
846850
return true;
847851
}
@@ -872,9 +876,12 @@ static Bool xy_collideTest_Circle_Circle(const CollideInfo *a, const CollideInfo
872876
cinfo->normal.normalize();
873877
cinfo->loc = a->position;
874878
projectCoord3D(&cinfo->loc, &cinfo->normal, a->geom.getMajorRadius());
875-
//cinfo->distSqr = sqr(sqrtf(distSqr) - b->geom.getMajorRadius()); // Formula is touchingDistSqr - b_Radius, this is the summarization
876-
Real distance = sqrtf(distSqr) - b->geom.getBoundingSphereRadius();
877-
cinfo->distSqr = distance > 0.0f ? sqr(distance) : 0.0f;
879+
if(cinfo->getDistance)
880+
{
881+
//cinfo->distSqr = sqr(sqrtf(distSqr) - b->geom.getMajorRadius()); // Formula is touchingDistSqr - b_Radius, this is the summarization
882+
Real distance = sqrtf(distSqr) - b->geom.getBoundingSphereRadius();
883+
cinfo->distSqr = distance > 0.0f ? sqr(distance) : 0.0f;
884+
}
878885
}
879886

880887
return true;
@@ -894,10 +901,10 @@ static Bool xy_collideTest_Rect_Rect(const CollideInfo *a, const CollideInfo *b,
894901
Real minDistSqr = HUGE_DIST_SQR;
895902

896903
rectToFourPoints(a, pts);
897-
testRotatedPointsAgainstRect(pts, b, &avg, &avgTot, &minDistSqr);
904+
testRotatedPointsAgainstRect(pts, b, &avg, &avgTot, cinfo->getDistance ? &minDistSqr : NULL);
898905

899906
rectToFourPoints(b, pts);
900-
testRotatedPointsAgainstRect(pts, a, &avg, &avgTot, &minDistSqr);
907+
testRotatedPointsAgainstRect(pts, a, &avg, &avgTot, cinfo->getDistance ? &minDistSqr : NULL);
901908

902909
if (avgTot > 0)
903910
{
@@ -925,7 +932,7 @@ static Bool xy_collideTest_Rect_Rect(const CollideInfo *a, const CollideInfo *b,
925932
cinfo->normal.normalize();
926933

927934
// Get the distance for damage calculation
928-
if(minDistSqr < HUGE_DIST_SQR)
935+
if(cinfo->getDistance && minDistSqr < HUGE_DIST_SQR)
929936
{
930937
//Real minDist = sqrtf(minDistSqr);
931938
//Real boundingDistance = a->geom.getBoundingSphereRadius() - a->geom.getMajorRadius();
@@ -1058,9 +1065,12 @@ static Bool collideTest_Sphere_Sphere(const CollideInfo *a, const CollideInfo *b
10581065
cinfo->normal.normalize();
10591066
cinfo->loc = a->position;
10601067
projectCoord3D(&cinfo->loc, &cinfo->normal, a->geom.getMajorRadius());
1061-
//cinfo->distSqr = sqr(sqrtf(distSqr) - b->geom.getMajorRadius()); // Formula is touchingDistSqr - b_Radius, this is the summarization
1062-
Real distance = sqrtf(distSqr) - b->geom.getBoundingSphereRadius();
1063-
cinfo->distSqr = distance > 0.0f ? sqr(distance) : 0.0f;
1068+
if(cinfo->getDistance)
1069+
{
1070+
//cinfo->distSqr = sqr(sqrtf(distSqr) - b->geom.getMajorRadius()); // Formula is touchingDistSqr - b_Radius, this is the summarization
1071+
Real distance = sqrtf(distSqr) - b->geom.getBoundingSphereRadius();
1072+
cinfo->distSqr = distance > 0.0f ? sqr(distance) : 0.0f;
1073+
}
10641074
}
10651075

10661076
return true;
@@ -2378,11 +2388,21 @@ Bool PartitionManager::geomCollidesWithGeom(const Coord3D* pos1,
23782388
b_LowHeightBoundary = b_HiHeightBoundary = pos2->z;
23792389
break;
23802390
case BOUNDARY_HEIGHT_CHECK:
2391+
{
23812392
a_LowHeightBoundary = pos1->z - geom1.getMaxHeightAbovePosition();
23822393
a_HiHeightBoundary = pos1->z + geom1.getMaxHeightAbovePosition();
2383-
b_LowHeightBoundary = pos2->z + geom2.getZDeltaToCenterPosition() - geom2.getBoundingSphereRadius();
2384-
b_HiHeightBoundary = pos2->z + geom2.getZDeltaToCenterPosition() + geom2.getBoundingSphereRadius();
2394+
if(TheGlobalData->m_useAccurateSphereToRectCollision)
2395+
{
2396+
b_LowHeightBoundary = pos2->z;
2397+
b_HiHeightBoundary = pos2->z + geom2.getMaxHeightAbovePosition();
2398+
}
2399+
else
2400+
{
2401+
b_LowHeightBoundary = pos2->z + geom2.getZDeltaToCenterPosition() - geom2.getBoundingSphereRadius();
2402+
b_HiHeightBoundary = pos2->z + geom2.getZDeltaToCenterPosition() + geom2.getBoundingSphereRadius();
2403+
}
23852404
break;
2405+
}
23862406
default:
23872407
a_LowHeightBoundary = pos1->z;
23882408
a_HiHeightBoundary = pos1->z + geom1.getMaxHeightAbovePosition();
@@ -2416,11 +2436,12 @@ Bool PartitionManager::geomCollidesWithGeom(const Coord3D* pos1,
24162436
//
24172437
CollideTestProc collideProc = theCollideTestProcs[ (thisGeom - GEOMETRY_FIRST) * GEOMETRY_NUM_TYPES + (thatGeom - GEOMETRY_FIRST) ];
24182438
CollideLocAndNormal cloc;
2419-
Bool passCollide = (*collideProc)(&thisInfo, &thatInfo, &cloc);
2439+
cloc.getDistance = heightCheckType == BOUNDARY_HEIGHT_CHECK;
2440+
Bool doesCollide = (*collideProc)(&thisInfo, &thatInfo, &cloc);
24202441

2421-
if(abDistSqr)
2442+
if(cloc.getDistance && abDistSqr)
24222443
*abDistSqr = cloc.distSqr;
2423-
return passCollide;
2444+
return doesCollide;
24242445
}
24252446
else
24262447
{

0 commit comments

Comments
 (0)