Skip to content

Commit

Permalink
Update entity colliders on faded raised platforms.
Browse files Browse the repository at this point in the history
  • Loading branch information
afritz1 committed Jan 12, 2025
1 parent 125e5e6 commit a339ca7
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
35 changes: 35 additions & 0 deletions OpenTESArena/src/Entities/EntityChunkManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,40 @@ void EntityChunkManager::updateCreatureSounds(double dt, EntityChunk &entityChun
}
}

void EntityChunkManager::updateFadedElevatedPlatforms(EntityChunk &entityChunk, const VoxelChunk &voxelChunk, double ceilingScale, JPH::PhysicsSystem &physicsSystem)
{
for (const VoxelFadeAnimationInstance &fadeAnimInst : voxelChunk.getFadeAnimInsts())
{
if (fadeAnimInst.isDoneFading())
{
for (int i = static_cast<int>(entityChunk.entityIDs.size()) - 1; i >= 0; i--)
{
const EntityInstanceID entityInstID = entityChunk.entityIDs[i];
const EntityInstance &entityInst = this->entities.get(entityInstID);
const CoordDouble2 &entityCoord = this->positions.get(entityInst.positionID);
const VoxelInt2 entityVoxel = VoxelUtils::pointToVoxel(entityCoord.point);

const bool matchesFadedVoxel = (entityVoxel.x == fadeAnimInst.x) && (entityVoxel.y == fadeAnimInst.z);

// @todo: we don't know if this was a raised platform because the voxel shape has already changed this frame, so just assume yes for "can be elevated" entities
if (matchesFadedVoxel && entityInst.canUseElevatedPlatforms())
{
JPH::BodyInterface &bodyInterface = physicsSystem.GetBodyInterface();
const JPH::BodyID entityPhysicsBodyID = entityInst.physicsBodyID;
const JPH::RVec3 oldEntityPhysicsPosition = bodyInterface.GetPosition(entityPhysicsBodyID);
const JPH::ShapeRefC entityPhysicsShape = bodyInterface.GetShape(entityPhysicsBodyID);
const JPH::AABox entityColliderBBox = entityPhysicsShape->GetLocalBounds();
const float entityColliderHeight = entityColliderBBox.GetSize().GetY();
const double newEntityFeetY = ceilingScale;
const double newEntityPhysicsCenterY = newEntityFeetY + (entityColliderHeight * 0.50);
const JPH::RVec3 newEntityPhysicsPosition(oldEntityPhysicsPosition.GetX(), newEntityPhysicsCenterY, oldEntityPhysicsPosition.GetZ());
bodyInterface.SetPosition(entityPhysicsBodyID, newEntityPhysicsPosition, JPH::EActivation::Activate);
}
}
}
}
}

void EntityChunkManager::update(double dt, BufferView<const ChunkInt2> activeChunkPositions,
BufferView<const ChunkInt2> newChunkPositions, BufferView<const ChunkInt2> freedChunkPositions,
const Player &player, const LevelDefinition *activeLevelDef, const LevelInfoDefinition *activeLevelInfoDef,
Expand Down Expand Up @@ -1080,6 +1114,7 @@ void EntityChunkManager::update(double dt, BufferView<const ChunkInt2> activeChu
}

this->updateCreatureSounds(dt, entityChunk, playerCoord, ceilingScale, random, audioManager);
this->updateFadedElevatedPlatforms(entityChunk, voxelChunk, ceilingScale, physicsSystem);
}
}

Expand Down
1 change: 1 addition & 0 deletions OpenTESArena/src/Entities/EntityChunkManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class EntityChunkManager final : public SpecializedChunkManager<EntityChunk>
std::string getCreatureSoundFilename(const EntityDefID defID) const;
void updateCreatureSounds(double dt, EntityChunk &entityChunk, const CoordDouble3 &playerCoord,
double ceilingScale, Random &random, AudioManager &audioManager);
void updateFadedElevatedPlatforms(EntityChunk &entityChunk, const VoxelChunk &voxelChunk, double ceilingScale, JPH::PhysicsSystem &physicsSystem);
public:
const EntityDefinition &getEntityDef(EntityDefID defID) const;
const EntityInstance &getEntity(EntityInstanceID id) const;
Expand Down
5 changes: 5 additions & 0 deletions OpenTESArena/src/Entities/EntityInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ bool EntityInstance::isDynamic() const
return this->directionID >= 0;
}

bool EntityInstance::canUseElevatedPlatforms() const
{
return !this->isDynamic();
}

bool EntityInstance::isCitizen() const
{
return this->citizenDirectionIndexID >= 0;
Expand Down
3 changes: 3 additions & 0 deletions OpenTESArena/src/Entities/EntityInstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ struct EntityInstance
// Whether the entity is capable of moving + looking.
bool isDynamic() const;

// Whether the entity can be placed on raised platforms.
bool canUseElevatedPlatforms() const;

bool isCitizen() const;

bool hasInventory() const;
Expand Down

0 comments on commit a339ca7

Please sign in to comment.