Implement gravity resolution and fast area calculations in collision handling
This commit is contained in:
parent
b3975ca41b
commit
2e16b2d524
8 changed files with 301 additions and 26 deletions
|
|
@ -78,6 +78,15 @@ void appendSolidBody(std::vector<CollisionBody>& bodies, const std::vector<Shape
|
|||
|
||||
} // namespace
|
||||
|
||||
FastAreaRange calculateFastAreaRange(WorldPoint center)
|
||||
{
|
||||
const s32 blockX = center.x >> 9;
|
||||
const s32 blockY = center.y >> 9;
|
||||
const s32 sum = blockX + blockY;
|
||||
const s32 difference = blockX - blockY;
|
||||
return {sum - 5, sum + 5, difference - 3, difference + 3};
|
||||
}
|
||||
|
||||
bool isWorldPointInFastArea(s32 x, s32 y, const FastAreaRange& range)
|
||||
{
|
||||
if (x < 0 || x > 0x7fff || y < 0 || y > 0x7fff) {
|
||||
|
|
@ -219,9 +228,10 @@ std::optional<SweepItemImpact> sweepSolidItemImpact(
|
|||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<std::vector<SweepCandidateImpact>> collectSolidSweepImpacts(
|
||||
std::optional<std::vector<SweepCandidateImpact>> collectSweepItemImpacts(
|
||||
const LiveFixedMapItems& items, u16 movingItemNumber, WorldPoint destination,
|
||||
const std::vector<ShapeTypeInfo>& shapes, const FastAreaRange& fastArea)
|
||||
const std::vector<ShapeTypeInfo>& shapes, const FastAreaRange& fastArea,
|
||||
std::optional<u8> floorGapLimit)
|
||||
{
|
||||
const auto* movingItem = items.findItem(movingItemNumber);
|
||||
if (movingItem == nullptr || movingItem->data.shape >= shapes.size() ||
|
||||
|
|
@ -248,13 +258,27 @@ std::optional<std::vector<SweepCandidateImpact>> collectSolidSweepImpacts(
|
|||
return std::nullopt;
|
||||
}
|
||||
const auto& shape = shapes[item->data.shape];
|
||||
if ((shape.flags & solidFlag) == 0) {
|
||||
continue;
|
||||
}
|
||||
const auto obstacle = makeSweepItemBody({item->data.x, item->data.y, item->data.z},
|
||||
shape, (item->data.flags & flippedFlag) != 0);
|
||||
std::optional<u8> floorGap;
|
||||
const s32 gap = destination.z - item->data.z - obstacle.extentZ;
|
||||
if (floorGapLimit && (shape.flags & solidFlag) != 0 && gap >= 0 &&
|
||||
gap <= *floorGapLimit &&
|
||||
destination.x - movingBody.extentX + 1 <= item->data.x &&
|
||||
item->data.x - obstacle.extentX + 1 <= destination.x &&
|
||||
destination.y - movingBody.extentY + 1 <= item->data.y &&
|
||||
item->data.y - obstacle.extentY + 1 <= destination.y) {
|
||||
floorGap = static_cast<u8>(gap);
|
||||
}
|
||||
if ((shape.flags & (solidFlag | 0x40)) == 0) {
|
||||
continue;
|
||||
}
|
||||
if (const auto impact = sweepSolidItemImpact(movingBody, destination, obstacle)) {
|
||||
impacts.push_back({itemNumber, *impact});
|
||||
impacts.push_back({itemNumber,
|
||||
{impact->resolved, (shape.flags & solidFlag) != 0 ? impact->failedDirections : u8{0}},
|
||||
(shape.flags & 0x40) != 0, floorGap});
|
||||
} else if (floorGap) {
|
||||
impacts.push_back({itemNumber, {}, false, floorGap});
|
||||
}
|
||||
}
|
||||
return impacts;
|
||||
|
|
@ -265,9 +289,16 @@ SweepCollisionResult selectSolidSweepImpact(WorldPoint from, WorldPoint destinat
|
|||
{
|
||||
SweepCollisionResult result{destination};
|
||||
for (const auto& candidate : impacts) {
|
||||
if (candidate.floorGap && (!result.supportGap || *candidate.floorGap <= *result.supportGap)) {
|
||||
result.supportItem = candidate.itemNumber;
|
||||
result.supportGap = candidate.floorGap;
|
||||
}
|
||||
if (candidate.damaging) {
|
||||
result.damagingContacts.push_back(candidate.itemNumber);
|
||||
}
|
||||
result.failedDirections |= candidate.impact.failedDirections;
|
||||
const auto& point = candidate.impact.resolved;
|
||||
if (candidate.itemNumber != 0 &&
|
||||
if (candidate.itemNumber != 0 && candidate.impact.failedDirections != 0 &&
|
||||
(std::abs(static_cast<std::int64_t>(point.x) - from.x) <
|
||||
std::abs(static_cast<std::int64_t>(result.destination.x) - from.x) ||
|
||||
std::abs(static_cast<std::int64_t>(point.y) - from.y) <
|
||||
|
|
@ -283,19 +314,31 @@ SweepCollisionResult selectSolidSweepImpact(WorldPoint from, WorldPoint destinat
|
|||
|
||||
std::optional<SweepCollisionResult> resolveSolidLiveSweep(
|
||||
const LiveFixedMapItems& items, u16 movingItemNumber, WorldPoint destination,
|
||||
const std::vector<ShapeTypeInfo>& shapes, const FastAreaRange& fastArea)
|
||||
const std::vector<ShapeTypeInfo>& shapes, const FastAreaRange& fastArea,
|
||||
std::optional<u8> floorGapLimit)
|
||||
{
|
||||
const auto* movingItem = items.findItem(movingItemNumber);
|
||||
if (movingItem == nullptr) {
|
||||
if (movingItem == nullptr || movingItem->data.shape >= shapes.size()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
const auto impacts = collectSolidSweepImpacts(items, movingItemNumber, destination,
|
||||
shapes, fastArea);
|
||||
const WorldPoint from{movingItem->data.x, movingItem->data.y, movingItem->data.z};
|
||||
const auto bounds = clampAreaSweepBounds(from, destination);
|
||||
const auto body = makeSweepItemBody(from, shapes[movingItem->data.shape],
|
||||
(movingItem->data.flags & flippedFlag) != 0);
|
||||
if (!isSweepFootprintInFastArea(from, destination, body.extentX, body.extentY, fastArea)) {
|
||||
SweepCollisionResult result{from};
|
||||
result.failedDirections = static_cast<u8>(bounds.failedDirections | 3);
|
||||
result.failed = true;
|
||||
return result;
|
||||
}
|
||||
const auto impacts = collectSweepItemImpacts(items, movingItemNumber, bounds.destination,
|
||||
shapes, fastArea, floorGapLimit);
|
||||
if (!impacts) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return selectSolidSweepImpact({movingItem->data.x, movingItem->data.y, movingItem->data.z},
|
||||
destination, *impacts);
|
||||
auto result = selectSolidSweepImpact(from, bounds.destination, *impacts);
|
||||
result.failedDirections |= bounds.failedDirections;
|
||||
return result;
|
||||
}
|
||||
|
||||
bool collisionBodiesOverlap(const CollisionBody& left, const CollisionBody& right)
|
||||
|
|
|
|||
|
|
@ -233,6 +233,86 @@ bool ProcessKernel::runUnobstructedGravity(u16 processId, LiveFixedMapItems& ite
|
|||
return true;
|
||||
}
|
||||
|
||||
bool ProcessKernel::runResolvedGravity(u16 processId, LiveFixedMapItems& items,
|
||||
const std::vector<ShapeTypeInfo>& shapes, const FastAreaRange& fastArea,
|
||||
u32 timeScale, u32 timerTick, u16 (&groupCounts)[32])
|
||||
{
|
||||
const auto* process = processAt(processId);
|
||||
if (process == nullptr || allocationSizeAt(processId) != sizeof(GravityProcessEntry) ||
|
||||
(process->bytes[0x16] & 0x08) != 0 || (process->bytes[0x19] & 0x10) != 0 ||
|
||||
std::none_of(ownedGravityProcesses_.begin(), ownedGravityProcesses_.end(),
|
||||
[process](const auto& owned) { return &owned->base == process; })) {
|
||||
return false;
|
||||
}
|
||||
const u16 itemNumber = readLe16(*process, 0x04);
|
||||
const auto* item = items.findItem(itemNumber);
|
||||
const auto zSpeed = static_cast<s16>(readLe16(*process, 0x36));
|
||||
if (item == nullptr || item->data.z > 250) {
|
||||
return false;
|
||||
}
|
||||
if (zSpeed == 0 &&
|
||||
((readLe16(*process, 0x32) == 0 && readLe16(*process, 0x34) == 0) ||
|
||||
!items.grabItem(itemNumber, shapes, *this, groupCounts, timerTick))) {
|
||||
return false;
|
||||
}
|
||||
const WorldPoint from{item->data.x, item->data.y, item->data.z};
|
||||
const auto step = planGravitySweep(from, static_cast<s16>(readLe16(*process, 0x32)),
|
||||
static_cast<s16>(readLe16(*process, 0x34)), zSpeed,
|
||||
static_cast<s16>(readLe16(*process, 0x38)), timeScale);
|
||||
const auto sweep = resolveSolidLiveSweep(items, itemNumber, step.destination, shapes, fastArea);
|
||||
if (!sweep || sweep->failed || (sweep->failedDirections & 4) != 0) {
|
||||
return false;
|
||||
}
|
||||
const auto& resolved = sweep->destination;
|
||||
if (!items.moveItem(itemNumber, static_cast<u16>(resolved.x),
|
||||
static_cast<u16>(resolved.y), static_cast<u8>(resolved.z))) {
|
||||
return false;
|
||||
}
|
||||
auto* mutableProcess = processAt(processId);
|
||||
auto nextZSpeed = step.nextZSpeed;
|
||||
if (sweep->failedDirections != 0) {
|
||||
const auto damped = [](s16 speed) {
|
||||
return static_cast<s16>((static_cast<s32>(speed) + (speed < 0 ? 1 : 0)) >> 2);
|
||||
};
|
||||
nextZSpeed = damped(nextZSpeed);
|
||||
for (const u8 axis : {u8{1}, u8{2}}) {
|
||||
const u16 offset = axis == 1 ? 0x32 : 0x34;
|
||||
const s16 speed = static_cast<s16>(readLe16(*mutableProcess, offset));
|
||||
const s16 response = static_cast<s16>((sweep->failedDirections & axis) != 0 ?
|
||||
-damped(speed) : damped(speed));
|
||||
writeLe16(*mutableProcess, offset, static_cast<u16>(response));
|
||||
}
|
||||
}
|
||||
writeLe16(*mutableProcess, 0x36, static_cast<u16>(nextZSpeed));
|
||||
EntityDispatchEntry::sleep(*mutableProcess, timerTick, 4 * std::clamp(timeScale, 1u, 4u));
|
||||
return true;
|
||||
}
|
||||
|
||||
u32 ProcessKernel::scanResolvedGravity(u8 activeGroup, LiveFixedMapItems& items,
|
||||
const std::vector<ShapeTypeInfo>& shapes, const FastAreaRange& fastArea,
|
||||
u32 timeScale, u32 timerTick, u16 (&groupCounts)[32])
|
||||
{
|
||||
struct ScanContext {
|
||||
ProcessKernel& kernel;
|
||||
LiveFixedMapItems& items;
|
||||
const std::vector<ShapeTypeInfo>& shapes;
|
||||
const FastAreaRange& fastArea;
|
||||
u32 timeScale;
|
||||
u32 timerTick;
|
||||
u16 (&groupCounts)[32];
|
||||
u32 moved = 0;
|
||||
} context{*this, items, shapes, fastArea, timeScale, timerTick, groupCounts};
|
||||
scanRunnableProcesses(activeGroup, [](EntityDispatchEntryBase& process, void* state) {
|
||||
auto& scan = *static_cast<ScanContext*>(state);
|
||||
if (readLe16(process, 0x00) == 0x0610 &&
|
||||
scan.kernel.runResolvedGravity(readLe16(process, 0x02), scan.items,
|
||||
scan.shapes, scan.fastArea, scan.timeScale, scan.timerTick, scan.groupCounts)) {
|
||||
++scan.moved;
|
||||
}
|
||||
}, &context, timerTick);
|
||||
return context.moved;
|
||||
}
|
||||
|
||||
SystemTimerProcessEntry* ProcessKernel::createSystemTimerProcess(u8 activeGroup, u16 (&groupCounts)[32])
|
||||
{
|
||||
if (systemTimerProcess_ != nullptr) {
|
||||
|
|
|
|||
|
|
@ -72,6 +72,7 @@ struct AppState {
|
|||
std::vector<std::uint8_t> viewPixels;
|
||||
remorse::WorldPoint spawn{};
|
||||
remorse::WorldPoint playerWorld{};
|
||||
remorse::FastAreaRange fastArea{};
|
||||
remorse::s32 minWorldX = 0;
|
||||
remorse::s32 minWorldY = 0;
|
||||
remorse::s32 maxWorldX = 0;
|
||||
|
|
@ -272,6 +273,7 @@ void loadSceneAsync(HWND window, AppState& state)
|
|||
std::lock_guard<std::mutex> lock(state.sceneMutex);
|
||||
state.spawn = spawn;
|
||||
state.playerWorld = spawn;
|
||||
state.fastArea = remorse::calculateFastAreaRange(spawn);
|
||||
state.playerCollisionBody = remorse::makeCollisionBody(spawn, (*shapeTypes)[1]);
|
||||
state.collisionBodies = std::move(collisionBodies);
|
||||
state.shapeTypes = *shapeTypes;
|
||||
|
|
@ -646,6 +648,12 @@ void updatePlayer(HWND window, AppState& state)
|
|||
}
|
||||
}
|
||||
}, &app);
|
||||
} else if (entry.bytes[0] == 0x10 && entry.bytes[1] == 0x06 && app.liveItems) {
|
||||
const remorse::u16 processId = static_cast<remorse::u16>(entry.bytes[2] |
|
||||
(static_cast<remorse::u16>(entry.bytes[3]) << 8));
|
||||
app.diagnosticKernel.runResolvedGravity(processId, *app.liveItems,
|
||||
app.shapeTypes, app.fastArea, 1, app.diagnosticKernel.systemTimerTick(),
|
||||
app.diagnosticGroupCounts);
|
||||
}
|
||||
}, &state);
|
||||
state.diagnosticKernel.refreshActiveGroup(state.diagnosticGroupCounts);
|
||||
|
|
@ -697,6 +705,7 @@ void updatePlayer(HWND window, AppState& state)
|
|||
if (withinMapBounds && withinSceneBounds && remorse::canMoveCollisionBody(
|
||||
state.playerCollisionBody, destination, state.collisionBodies)) {
|
||||
state.playerWorld = destination;
|
||||
state.fastArea = remorse::calculateFastAreaRange(destination);
|
||||
state.playerCollisionBody.location = destination;
|
||||
state.playerOffsetX = nextOffsetX;
|
||||
state.playerOffsetY = nextOffsetY;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue