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:
parent
b99fb59c12
commit
b3975ca41b
8 changed files with 467 additions and 8 deletions
|
|
@ -1,6 +1,7 @@
|
|||
#include "remorse/collision.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
|
||||
namespace remorse {
|
||||
|
|
@ -77,6 +78,40 @@ void appendSolidBody(std::vector<CollisionBody>& bodies, const std::vector<Shape
|
|||
|
||||
} // namespace
|
||||
|
||||
bool isWorldPointInFastArea(s32 x, s32 y, const FastAreaRange& range)
|
||||
{
|
||||
if (x < 0 || x > 0x7fff || y < 0 || y > 0x7fff) {
|
||||
return false;
|
||||
}
|
||||
const s32 blockX = x >> 9;
|
||||
const s32 blockY = y >> 9;
|
||||
return range.minSum <= blockX + blockY && blockX + blockY <= range.maxSum &&
|
||||
range.minDifference <= blockX - blockY && blockX - blockY <= range.maxDifference;
|
||||
}
|
||||
|
||||
bool isSweepFootprintInFastArea(WorldPoint from, WorldPoint destination,
|
||||
s32 extentX, s32 extentY, const FastAreaRange& range)
|
||||
{
|
||||
for (const WorldPoint point : {from, destination}) {
|
||||
for (const s32 x : {point.x - extentX + 1, point.x}) {
|
||||
for (const s32 y : {point.y - extentY + 1, point.y}) {
|
||||
if (!isWorldPointInFastArea(x, y, range)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
WorldRect areaSweepCandidateBounds(WorldPoint from, WorldPoint destination, s32 extentX, s32 extentY)
|
||||
{
|
||||
return {std::max(0, std::min(from.x, destination.x) - extentX),
|
||||
std::max(0, std::min(from.y, destination.y) - extentY),
|
||||
std::min(0x7fff, std::max(from.x, destination.x) + 0x100),
|
||||
std::min(0x7fff, std::max(from.y, destination.y) + 0x100)};
|
||||
}
|
||||
|
||||
SweepBoundsResult clampAreaSweepBounds(WorldPoint from, WorldPoint destination)
|
||||
{
|
||||
SweepBoundsResult result{destination};
|
||||
|
|
@ -117,6 +152,152 @@ CollisionBody makeCollisionBody(WorldPoint location, const ShapeTypeInfo& shapeI
|
|||
static_cast<s32>(shapeInfo.z) * shapeHeightScale};
|
||||
}
|
||||
|
||||
CollisionBody makeSweepItemBody(WorldPoint location, const ShapeTypeInfo& shapeInfo, bool flipped)
|
||||
{
|
||||
const s32 extentX = static_cast<s32>(flipped ? shapeInfo.y : shapeInfo.x) * 16;
|
||||
const s32 extentY = static_cast<s32>(flipped ? shapeInfo.x : shapeInfo.y) * 16;
|
||||
const s32 extentZ = shapeInfo.z == 0 && (shapeInfo.flags & 1) == 0 ? 1 :
|
||||
static_cast<s32>(shapeInfo.z) * shapeHeightScale;
|
||||
return {location, extentX, extentY, extentZ};
|
||||
}
|
||||
|
||||
std::optional<SweepItemImpact> sweepSolidItemImpact(
|
||||
const CollisionBody& moving, WorldPoint destination, const CollisionBody& obstacle)
|
||||
{
|
||||
if (moving.extentX <= 0 || moving.extentY <= 0 || moving.extentZ <= 0 ||
|
||||
obstacle.extentX <= 0 || obstacle.extentY <= 0 || obstacle.extentZ <= 0) {
|
||||
return std::nullopt;
|
||||
}
|
||||
const std::array<std::int64_t, 3> origin{moving.location.x, moving.location.y, moving.location.z};
|
||||
const std::array<std::int64_t, 3> delta{
|
||||
static_cast<std::int64_t>(destination.x) - origin[0],
|
||||
static_cast<std::int64_t>(destination.y) - origin[1],
|
||||
static_cast<std::int64_t>(destination.z) - origin[2]};
|
||||
const std::array<std::int64_t, 3> obstaclePosition{
|
||||
obstacle.location.x, obstacle.location.y, obstacle.location.z};
|
||||
const std::array<std::int64_t, 3> movingExtent{moving.extentX, moving.extentY, moving.extentZ};
|
||||
const std::array<std::int64_t, 3> obstacleExtent{obstacle.extentX, obstacle.extentY, obstacle.extentZ};
|
||||
std::array<std::int64_t, 3> lower{};
|
||||
std::array<std::int64_t, 3> upper{};
|
||||
for (std::size_t axis = 0; axis < 3; ++axis) {
|
||||
lower[axis] = obstaclePosition[axis] - obstacleExtent[axis] + 1 - origin[axis];
|
||||
upper[axis] = obstaclePosition[axis] + movingExtent[axis] - 1 - origin[axis];
|
||||
}
|
||||
lower[2] = obstaclePosition[2] - movingExtent[2] + 1 - origin[2];
|
||||
upper[2] = obstaclePosition[2] + obstacleExtent[2] - 1 - origin[2];
|
||||
|
||||
for (const int direction : {1, -1}) {
|
||||
for (std::size_t axis = 0; axis < 3; ++axis) {
|
||||
const std::int64_t distance = direction > 0 ? lower[axis] : -upper[axis];
|
||||
const std::int64_t extent = delta[axis] * direction;
|
||||
if (distance <= 0 || distance > extent) {
|
||||
continue;
|
||||
}
|
||||
bool withinOtherAxes = true;
|
||||
for (std::size_t other = 0; other < 3; ++other) {
|
||||
if (other == axis) {
|
||||
continue;
|
||||
}
|
||||
const std::int64_t progress = delta[other] * distance;
|
||||
if (progress < lower[other] * extent || progress > upper[other] * extent) {
|
||||
withinOtherAxes = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (withinOtherAxes) {
|
||||
const std::int64_t beforeContact = distance - 1;
|
||||
std::array<s32, 3> resolved{};
|
||||
for (std::size_t coordinate = 0; coordinate < 3; ++coordinate) {
|
||||
resolved[coordinate] = static_cast<s32>(origin[coordinate] +
|
||||
delta[coordinate] * beforeContact / extent);
|
||||
}
|
||||
return SweepItemImpact{{resolved[0], resolved[1], resolved[2]},
|
||||
static_cast<u8>(1u << axis)};
|
||||
}
|
||||
}
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<std::vector<SweepCandidateImpact>> collectSolidSweepImpacts(
|
||||
const LiveFixedMapItems& items, u16 movingItemNumber, WorldPoint destination,
|
||||
const std::vector<ShapeTypeInfo>& shapes, const FastAreaRange& fastArea)
|
||||
{
|
||||
const auto* movingItem = items.findItem(movingItemNumber);
|
||||
if (movingItem == nullptr || movingItem->data.shape >= shapes.size() ||
|
||||
destination.x < 0 || destination.x > 0x7fff || destination.y < 0 ||
|
||||
destination.y > 0x7fff || destination.z < 0 || destination.z > 0xfa) {
|
||||
return std::nullopt;
|
||||
}
|
||||
const WorldPoint from{movingItem->data.x, movingItem->data.y, movingItem->data.z};
|
||||
const auto movingBody = makeSweepItemBody(from, shapes[movingItem->data.shape],
|
||||
(movingItem->data.flags & flippedFlag) != 0);
|
||||
if (!isSweepFootprintInFastArea(from, destination, movingBody.extentX,
|
||||
movingBody.extentY, fastArea)) {
|
||||
return std::nullopt;
|
||||
}
|
||||
const auto candidates = items.sweepCandidateIds(areaSweepCandidateBounds(from, destination,
|
||||
movingBody.extentX, movingBody.extentY), movingItemNumber);
|
||||
if (!candidates) {
|
||||
return std::nullopt;
|
||||
}
|
||||
std::vector<SweepCandidateImpact> impacts;
|
||||
for (const u16 itemNumber : *candidates) {
|
||||
const auto* item = items.findItem(itemNumber);
|
||||
if (item == nullptr || item->data.shape >= shapes.size()) {
|
||||
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);
|
||||
if (const auto impact = sweepSolidItemImpact(movingBody, destination, obstacle)) {
|
||||
impacts.push_back({itemNumber, *impact});
|
||||
}
|
||||
}
|
||||
return impacts;
|
||||
}
|
||||
|
||||
SweepCollisionResult selectSolidSweepImpact(WorldPoint from, WorldPoint destination,
|
||||
const std::vector<SweepCandidateImpact>& impacts)
|
||||
{
|
||||
SweepCollisionResult result{destination};
|
||||
for (const auto& candidate : impacts) {
|
||||
result.failedDirections |= candidate.impact.failedDirections;
|
||||
const auto& point = candidate.impact.resolved;
|
||||
if (candidate.itemNumber != 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) <
|
||||
std::abs(static_cast<std::int64_t>(result.destination.y) - from.y) ||
|
||||
std::abs(static_cast<std::int64_t>(point.z) - from.z) <
|
||||
std::abs(static_cast<std::int64_t>(result.destination.z) - from.z))) {
|
||||
result.destination = point;
|
||||
result.firstCollision = candidate.itemNumber;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::optional<SweepCollisionResult> resolveSolidLiveSweep(
|
||||
const LiveFixedMapItems& items, u16 movingItemNumber, WorldPoint destination,
|
||||
const std::vector<ShapeTypeInfo>& shapes, const FastAreaRange& fastArea)
|
||||
{
|
||||
const auto* movingItem = items.findItem(movingItemNumber);
|
||||
if (movingItem == nullptr) {
|
||||
return std::nullopt;
|
||||
}
|
||||
const auto impacts = collectSolidSweepImpacts(items, movingItemNumber, destination,
|
||||
shapes, fastArea);
|
||||
if (!impacts) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return selectSolidSweepImpact({movingItem->data.x, movingItem->data.y, movingItem->data.z},
|
||||
destination, *impacts);
|
||||
}
|
||||
|
||||
bool collisionBodiesOverlap(const CollisionBody& left, const CollisionBody& right)
|
||||
{
|
||||
return overlapsAt(left, left.location, right);
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include "remorse/entity_dispatch_entry.h"
|
||||
#include "remorse/file_system.h"
|
||||
#include "remorse/map_renderer.h"
|
||||
#include "remorse/type_flags.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
|
@ -412,11 +413,30 @@ std::optional<std::vector<u16>> dispatchStagedItemGrab(const StagedFixedMapItem&
|
|||
return processIds;
|
||||
}
|
||||
|
||||
LiveFixedMapItems::LiveFixedMapItems(std::vector<StagedFixedMapItem> items) : items_(std::move(items))
|
||||
LiveFixedMapItems::LiveFixedMapItems(std::vector<StagedFixedMapItem> items,
|
||||
std::vector<StagedContainedFixedMapItem> containedItems)
|
||||
: items_(std::move(items)), allocatedIds_(0x2800), parentById_(0x2800)
|
||||
{
|
||||
if (!buildStagedItemBlockChains(items_)) {
|
||||
throw std::invalid_argument("invalid live item IDs or coordinates");
|
||||
}
|
||||
for (const auto& item : items_) {
|
||||
allocatedIds_[item.itemNumber] = true;
|
||||
}
|
||||
for (const auto& contained : containedItems) {
|
||||
const u16 itemNumber = contained.itemNumber;
|
||||
if (itemNumber < 0x100 || itemNumber >= allocatedIds_.size() || allocatedIds_[itemNumber]) {
|
||||
throw std::invalid_argument("invalid contained item ID");
|
||||
}
|
||||
allocatedIds_[itemNumber] = true;
|
||||
}
|
||||
for (const auto& contained : containedItems) {
|
||||
if (contained.parentItemNumber < 0x100 || contained.parentItemNumber >= contained.itemNumber ||
|
||||
!allocatedIds_[contained.parentItemNumber]) {
|
||||
throw std::invalid_argument("invalid contained parent ID");
|
||||
}
|
||||
parentById_[contained.itemNumber] = contained.parentItemNumber;
|
||||
}
|
||||
}
|
||||
|
||||
const StagedFixedMapItem* LiveFixedMapItems::findItem(u16 itemNumber) const
|
||||
|
|
@ -429,12 +449,61 @@ const StagedFixedMapItem* LiveFixedMapItems::findItem(u16 itemNumber) const
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
u16 LiveFixedMapItems::containedParent(u16 itemNumber) const
|
||||
{
|
||||
return itemNumber < parentById_.size() ? parentById_[itemNumber] : 0;
|
||||
}
|
||||
|
||||
std::optional<StagedFixedMapItem> LiveFixedMapItems::nearestItem(s32 worldX, s32 worldY,
|
||||
s32 maxDistance) const
|
||||
{
|
||||
return nearestStagedFixedMapItem(items_, worldX, worldY, maxDistance);
|
||||
}
|
||||
|
||||
std::optional<std::vector<u16>> LiveFixedMapItems::sweepCandidateIds(const WorldRect& area,
|
||||
u16 excludedItemNumber) const
|
||||
{
|
||||
if (area.minX < 0 || area.minY < 0 || area.maxX > 0x7fff || area.maxY > 0x7fff ||
|
||||
area.minX > area.maxX || area.minY > area.maxY) {
|
||||
return std::nullopt;
|
||||
}
|
||||
const auto chains = buildStagedItemBlockChains(items_);
|
||||
if (!chains) {
|
||||
return std::nullopt;
|
||||
}
|
||||
std::vector<const StagedFixedMapItem*> byId(chains->next.size());
|
||||
for (const auto& item : items_) {
|
||||
byId[item.itemNumber] = &item;
|
||||
}
|
||||
const ItemFindArea itemArea{static_cast<u16>(area.minX), static_cast<u16>(area.minY),
|
||||
static_cast<u16>(area.maxX), static_cast<u16>(area.maxY)};
|
||||
std::vector<u16> candidates;
|
||||
for (const u16 itemNumber : itemFindCandidateOrder(itemArea, *chains)) {
|
||||
const auto& item = *byId[itemNumber];
|
||||
if (itemNumber != excludedItemNumber && item.data.x >= area.minX && item.data.x <= area.maxX &&
|
||||
item.data.y >= area.minY && item.data.y <= area.maxY) {
|
||||
candidates.push_back(itemNumber);
|
||||
}
|
||||
}
|
||||
return candidates;
|
||||
}
|
||||
|
||||
std::optional<u16> LiveFixedMapItems::allocateItem(StagedFixedMapItem item)
|
||||
{
|
||||
if (item.data.x > 0x7fff || item.data.y > 0x7fff) {
|
||||
return std::nullopt;
|
||||
}
|
||||
while (nextFreeItemNumber_ < allocatedIds_.size() && allocatedIds_[nextFreeItemNumber_]) {
|
||||
++nextFreeItemNumber_;
|
||||
}
|
||||
if (nextFreeItemNumber_ == allocatedIds_.size()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
item.itemNumber = nextFreeItemNumber_;
|
||||
insertItem(item);
|
||||
return item.itemNumber;
|
||||
}
|
||||
|
||||
bool LiveFixedMapItems::insertItem(StagedFixedMapItem item)
|
||||
{
|
||||
if (item.itemNumber < 0x100 || item.itemNumber >= 0x2800 || item.data.x > 0x7fff ||
|
||||
|
|
@ -443,6 +512,8 @@ bool LiveFixedMapItems::insertItem(StagedFixedMapItem item)
|
|||
}
|
||||
item.data.z = std::min<u8>(item.data.z, 0xfa);
|
||||
items_.push_back(item);
|
||||
allocatedIds_[item.itemNumber] = true;
|
||||
parentById_[item.itemNumber] = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -160,6 +160,16 @@ void loadSceneAsync(HWND window, AppState& state)
|
|||
PostMessageW(window, sceneReadyMessage, 0, 0);
|
||||
return;
|
||||
}
|
||||
std::vector<remorse::StagedContainedFixedMapItem> containedItems;
|
||||
if (map->index != 0) {
|
||||
const auto contained = remorse::stageContainedFixedMapItems(*map, *shapeTypes);
|
||||
if (!contained) {
|
||||
setLoadFailure(state, L"Contained map items could not be staged.");
|
||||
PostMessageW(window, sceneReadyMessage, 0, 0);
|
||||
return;
|
||||
}
|
||||
containedItems = std::move(*contained);
|
||||
}
|
||||
auto collisionBodies = remorse::collectMapCollisionBodies(*map, *shapeTypes, *globObjects);
|
||||
const auto usecodePath = remorse::resolveGameDataPath(
|
||||
state.root, std::filesystem::path("USECODE") / "EUSECODE.FLX");
|
||||
|
|
@ -281,7 +291,7 @@ void loadSceneAsync(HWND window, AppState& state)
|
|||
state.playerFrames = std::move(playerFrames);
|
||||
state.playerPalette = *palette;
|
||||
if (stagedItems) {
|
||||
state.liveItems.emplace(std::move(*stagedItems));
|
||||
state.liveItems.emplace(std::move(*stagedItems), std::move(containedItems));
|
||||
}
|
||||
state.sceneLoaded = true;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue