Refactor collision handling and item management in the Remorse project

- Introduced new structures for handling sweep impacts and collision results, including SweepItemImpact, SweepCandidateImpact, and SweepCollisionResult.
- Implemented functions to check if a world point or sweep footprint is within a defined fast area.
- Enhanced LiveFixedMapItems to manage contained items and support item allocation with parent-child relationships.
- Updated collision detection logic to handle solid item impacts and collect sweep impacts from live items.
- Added tests for new collision functionalities, including sweep impact resolution and candidate selection.
- Modified existing tests to validate new item management features and ensure proper integration with the collision system.
This commit is contained in:
Maddo 2026-09-26 19:50:26 +02:00
commit b3975ca41b
8 changed files with 467 additions and 8 deletions

View file

@ -4,6 +4,24 @@
int main()
{
const remorse::FastAreaRange fastArea{2, 4, 0, 2};
assert(remorse::isWorldPointInFastArea(512, 512, fastArea));
assert(!remorse::isWorldPointInFastArea(-1, 512, fastArea));
assert(!remorse::isWorldPointInFastArea(0x8000, 512, fastArea));
assert(remorse::isSweepFootprintInFastArea({1000, 1000, 0}, {2000, 1000, 0},
32, 32, fastArea));
assert(!remorse::isSweepFootprintInFastArea({1000, 1000, 0}, {2000, 1000, 0},
32, 32, {2, 3, 0, 2}));
assert(!remorse::isSweepFootprintInFastArea({1000, 1000, 0}, {2000, 1000, 0},
32, 32, {2, 4, 1, 2}));
const auto sweepArea = remorse::areaSweepCandidateBounds({511, 512, 0}, {1024, 1536, 0}, 32, 64);
assert(sweepArea.minX == 479 && sweepArea.minY == 448 &&
sweepArea.maxX == 1280 && sweepArea.maxY == 1792);
const auto reverseArea = remorse::areaSweepCandidateBounds({1024, 1536, 0}, {511, 512, 0}, 32, 64);
assert(reverseArea.minX == sweepArea.minX && reverseArea.maxY == sweepArea.maxY);
const auto edgeSweepArea = remorse::areaSweepCandidateBounds({10, 10, 0}, {0x7fff, 0x7fff, 0}, 32, 64);
assert(edgeSweepArea.minX == 0 && edgeSweepArea.minY == 0 &&
edgeSweepArea.maxX == 0x7fff && edgeSweepArea.maxY == 0x7fff);
const auto outsideBounds = remorse::clampAreaSweepBounds({10, 20, 10}, {-3, 0x8000, 252});
assert(outsideBounds.destination.x == 0 && outsideBounds.destination.y == 0x7fff &&
outsideBounds.destination.z == 250 && outsideBounds.failedDirections == 7 &&
@ -56,6 +74,105 @@ int main()
assert(flipped.extentX == 128);
assert(flipped.extentY == 64);
const auto sweepItem = remorse::makeSweepItemBody({300, 300, 0}, obstacleInfo, true);
assert(sweepItem.extentX == 64 && sweepItem.extentY == 32 && sweepItem.extentZ == 64);
obstacleInfo.z = 0;
const auto zeroHeightItem = remorse::makeSweepItemBody({300, 300, 0}, obstacleInfo);
assert(zeroHeightItem.extentZ == 1);
obstacleInfo.flags = 1;
assert(remorse::makeSweepItemBody({300, 300, 0}, obstacleInfo).extentZ == 0);
obstacleInfo.z = 8;
obstacleInfo.flags = 0;
const remorse::CollisionBody sweepActor{{100, 100, 0}, 32, 32, 16};
const remorse::CollisionBody sweepWall{{200, 100, 0}, 32, 32, 16};
const auto rightHit = remorse::sweepSolidItemImpact(sweepActor, {250, 100, 0}, sweepWall);
assert(rightHit && rightHit->failedDirections == 1 && rightHit->resolved.x == 168);
const auto leftHit = remorse::sweepSolidItemImpact({{300, 100, 0}, 32, 32, 16},
{150, 100, 0}, sweepWall);
assert(leftHit && leftHit->failedDirections == 1 && leftHit->resolved.x == 232);
const auto upHit = remorse::sweepSolidItemImpact(sweepActor, {100, 250, 0},
{{100, 200, 0}, 32, 32, 16});
assert(upHit && upHit->failedDirections == 2 && upHit->resolved.y == 168);
const auto downHit = remorse::sweepSolidItemImpact({{100, 300, 0}, 32, 32, 16},
{100, 150, 0}, {{100, 200, 0}, 32, 32, 16});
assert(downHit && downHit->failedDirections == 2 && downHit->resolved.y == 232);
const auto ascendingHit = remorse::sweepSolidItemImpact(sweepActor, {100, 100, 30},
{{100, 100, 20}, 32, 32, 16});
assert(ascendingHit && ascendingHit->failedDirections == 4 && ascendingHit->resolved.z == 4);
const auto descendingHit = remorse::sweepSolidItemImpact({{100, 100, 40}, 32, 32, 16},
{100, 100, 0}, {{100, 100, 20}, 32, 32, 16});
assert(descendingHit && descendingHit->failedDirections == 4 && descendingHit->resolved.z == 36);
assert(!remorse::sweepSolidItemImpact(sweepActor, {250, 100, 0},
{{200, 200, 0}, 32, 32, 16}));
remorse::StagedFixedMapItem movingItem;
movingItem.itemNumber = 0x100;
movingItem.data.x = 512;
movingItem.data.y = 512;
movingItem.data.shape = 1;
auto wallItem = movingItem;
wallItem.itemNumber = 0x101;
wallItem.data.x = 640;
wallItem.data.shape = 2;
auto harmlessItem = wallItem;
harmlessItem.itemNumber = 0x102;
harmlessItem.data.x = 600;
harmlessItem.data.shape = 3;
remorse::LiveFixedMapItems liveSweepItems({movingItem, wallItem, harmlessItem});
std::vector<remorse::ShapeTypeInfo> sweepShapes(4);
sweepShapes[1].x = sweepShapes[1].y = sweepShapes[1].z = 2;
sweepShapes[2] = sweepShapes[1];
sweepShapes[2].flags = 2;
sweepShapes[3] = sweepShapes[1];
const remorse::FastAreaRange allBlocks{-128, 128, -128, 128};
const auto liveImpacts = remorse::collectSolidSweepImpacts(liveSweepItems, 0x100,
{700, 512, 0}, sweepShapes, allBlocks);
assert(liveImpacts && liveImpacts->size() == 1);
assert((*liveImpacts)[0].itemNumber == 0x101 &&
(*liveImpacts)[0].impact.failedDirections == 1 &&
(*liveImpacts)[0].impact.resolved.x == 608);
auto nextWall = wallItem;
nextWall.itemNumber = 0x103;
nextWall.data.x = 670;
sweepShapes[2].x = 1;
sweepShapes[2].y = 4;
wallItem.data.flags = 0x20;
remorse::LiveFixedMapItems flippedSweepItems({movingItem, wallItem, nextWall, harmlessItem});
const auto flippedImpacts = remorse::collectSolidSweepImpacts(flippedSweepItems, 0x100,
{700, 512, 0}, sweepShapes, allBlocks);
assert(flippedImpacts && flippedImpacts->size() == 2);
assert((*flippedImpacts)[0].itemNumber == 0x103 && (*flippedImpacts)[0].impact.resolved.x == 654);
assert((*flippedImpacts)[1].itemNumber == 0x101 && (*flippedImpacts)[1].impact.resolved.x == 576);
const auto nearestImpact = remorse::selectSolidSweepImpact({512, 512, 0},
{700, 512, 0}, *flippedImpacts);
assert(nearestImpact.firstCollision == 0x101 && nearestImpact.destination.x == 576 &&
nearestImpact.failedDirections == 1);
const auto resolvedLive = remorse::resolveSolidLiveSweep(flippedSweepItems, 0x100,
{700, 512, 0}, sweepShapes, allBlocks);
assert(resolvedLive && resolvedLive->firstCollision == 0x101 &&
resolvedLive->destination.x == 576 && resolvedLive->failedDirections == 1);
assert(!remorse::resolveSolidLiveSweep(flippedSweepItems, 0x100,
{700, 512, 0}, sweepShapes, {2, 2, 0, 0}));
const auto noImpact = remorse::selectSolidSweepImpact({512, 512, 0}, {700, 512, 0}, {});
assert(noImpact.firstCollision == 0 && noImpact.destination.x == 700 &&
noImpact.failedDirections == 0);
const std::vector<remorse::SweepCandidateImpact> tiedImpacts{
{0x101, {{576, 512, 0}, 1}}, {0x103, {{576, 512, 0}, 2}}};
const auto tiedImpact = remorse::selectSolidSweepImpact({512, 512, 0},
{700, 512, 0}, tiedImpacts);
assert(tiedImpact.firstCollision == 0x101 && tiedImpact.failedDirections == 3);
const std::vector<remorse::SweepCandidateImpact> mixedAxisImpacts{
{0x101, {{540, 590, 0}, 1}}, {0x103, {{550, 550, 0}, 2}}};
const auto mixedAxis = remorse::selectSolidSweepImpact({512, 512, 0},
{700, 700, 0}, mixedAxisImpacts);
assert(mixedAxis.firstCollision == 0x103 && mixedAxis.destination.x == 550 &&
mixedAxis.destination.y == 550 && mixedAxis.failedDirections == 3);
assert(!remorse::collectSolidSweepImpacts(liveSweepItems, 0x100,
{700, 512, 0}, sweepShapes, {2, 2, 0, 0}));
assert(!remorse::collectSolidSweepImpacts(liveSweepItems, 0x105,
{700, 512, 0}, sweepShapes, allBlocks));
remorse::FixedMap map;
remorse::FixedMapEntry solid;
solid.x = 500;