Files
ashlar/apps/openmw/mwphysics/actor.cpp
T
claudeandClaude Opus 5.5 9e28969629
Ashlar checks / tooling (push) Successful in 5s
Add ledge mantling
New [Game] mantling setting, on by default, with mantle max height (1.1
times the actor's height above its feet) and mantle duration (0.4 s).
When the player jumps or falls into a wall while moving towards it
(within 60 degrees), the movement solver casts the player's collision
shape up, across the edge and down. If it finds a surface flat enough to
stand on, above the feet and within reach, with headroom and room on top,
the player moves up, across and onto it along that checked path. Movement
and jump input are ignored meanwhile, and the climb ends any fall without
fall damage. No animation is needed. A script move or teleport, noclip or
paralysis cancels a climb.

Lua: self.controls.mantle (true by default) vetoes climbs while false,
and the actor's scripts get an AshlarMantle event with the ledge height
when one starts. The engine logs "Ashlar mantle:" lines at verbose level.
Player only; with the setting off nothing changes.

The geometry rules (reach, headroom, room on top, slope, too high, too
low) and the climb path are in mwphysics/mantle.cpp, apart from the
physics calls, with unit tests. The new mantle scenario uses a CC0 box
mesh written for it: the player climbs an 80 unit ledge, doesn't climb it
with the Lua veto, and doesn't climb a 240 unit wall.

Closes #28

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 15:44:40 -05:00

360 lines
11 KiB
C++

#include "actor.hpp"
#include <BulletCollision/CollisionShapes/btCylinderShape.h>
#include <components/debug/debuglog.hpp>
#include <components/misc/convert.hpp>
#include <components/resource/bulletshape.hpp>
#include <components/sceneutil/positionattitudetransform.hpp>
#include "../mwmechanics/creaturestats.hpp"
#include "../mwworld/class.hpp"
#include "collisiontype.hpp"
#include "mtphysics.hpp"
#include "trace.h"
#include <cmath>
namespace MWPhysics
{
Actor::Actor(const MWWorld::Ptr& ptr, const Resource::BulletShape& shape, PhysicsTaskScheduler* scheduler,
bool canWaterWalk, DetourNavigator::CollisionShapeType collisionShapeType)
: PtrHolder(ptr, ptr.getRefData().getPosition().asVec3())
, mStandingOnPtr(nullptr)
, mCanWaterWalk(canWaterWalk)
, mWalkingOnWater(false)
, mMeshTranslation(shape.mCollisionBox.mCenter)
, mOriginalHalfExtents(shape.mCollisionBox.mExtents)
, mStuckFrames(0)
, mLastStuckPosition{ 0, 0, 0 }
, mForce(0.f, 0.f, 0.f)
, mOnGround(ptr.getClass().getCreatureStats(ptr).getFallHeight() == 0)
, mOnSlope(false)
, mInternalCollisionMode(true)
, mExternalCollisionMode(true)
, mActive(false)
, mTaskScheduler(scheduler)
{
// We can not create actor without collisions - he will fall through the ground.
// In this case we should autogenerate collision box based on mesh shape
// (NPCs have bodyparts and use a different approach)
if (!ptr.getClass().isNpc() && mOriginalHalfExtents.length2() == 0.f)
{
if (shape.mCollisionShape)
{
btTransform transform;
transform.setIdentity();
btVector3 min;
btVector3 max;
shape.mCollisionShape->getAabb(transform, min, max);
mOriginalHalfExtents.x() = static_cast<float>((max[0] - min[0]) / 2.f);
mOriginalHalfExtents.y() = static_cast<float>((max[1] - min[1]) / 2.f);
mOriginalHalfExtents.z() = static_cast<float>((max[2] - min[2]) / 2.f);
mMeshTranslation = osg::Vec3f(0.f, 0.f, mOriginalHalfExtents.z());
}
if (mOriginalHalfExtents.length2() == 0.f)
Log(Debug::Error) << "Error: Failed to calculate bounding box for actor \""
<< ptr.getCellRef().getRefId() << "\".";
}
const btVector3 halfExtents = Misc::Convert::toBullet(mOriginalHalfExtents);
float extRatio = 0.f;
if (mOriginalHalfExtents.y() != 0.f)
extRatio = mOriginalHalfExtents.x() / mOriginalHalfExtents.y();
if (mMeshTranslation.x() == 0.0 && mMeshTranslation.y() == 0.0 && extRatio >= 1.f / 1.1f && extRatio <= 1.1f)
{
switch (collisionShapeType)
{
case DetourNavigator::CollisionShapeType::Aabb:
mShape = std::make_unique<btBoxShape>(halfExtents);
mRotationallyInvariant = true;
break;
case DetourNavigator::CollisionShapeType::RotatingBox:
mShape = std::make_unique<btBoxShape>(halfExtents);
mRotationallyInvariant = false;
break;
case DetourNavigator::CollisionShapeType::Cylinder:
mShape = std::make_unique<btCylinderShapeZ>(halfExtents);
mRotationallyInvariant = true;
break;
}
mCollisionShapeType = collisionShapeType;
}
else
{
mShape = std::make_unique<btBoxShape>(halfExtents);
mRotationallyInvariant = false;
mCollisionShapeType = DetourNavigator::CollisionShapeType::RotatingBox;
}
mConvexShape = static_cast<btConvexShape*>(mShape.get());
mConvexShape->setMargin(0.001); // make sure bullet isn't using the huge default convex shape margin of 0.04
mCollisionObject = std::make_unique<btCollisionObject>();
mCollisionObject->setCollisionFlags(btCollisionObject::CF_KINEMATIC_OBJECT);
mCollisionObject->setActivationState(DISABLE_DEACTIVATION);
mCollisionObject->setCollisionShape(mShape.get());
mCollisionObject->setUserPointer(this);
updateScaleUnsafe();
if (!mRotationallyInvariant)
{
const SceneUtil::PositionAttitudeTransform* baseNode = mPtr.getRefData().getBaseNode();
if (baseNode)
mRotation = baseNode->getAttitude();
}
addCollisionMask(getCollisionMask());
updateCollisionObjectPositionUnsafe();
}
Actor::~Actor()
{
mTaskScheduler->removeCollisionObject(mCollisionObject.get());
}
void Actor::enableCollisionMode(bool collision)
{
mInternalCollisionMode = collision;
}
void Actor::enableCollisionBody(bool collision)
{
if (mExternalCollisionMode != collision)
{
mExternalCollisionMode = collision;
updateCollisionMask();
}
}
void Actor::addCollisionMask(int collisionMask)
{
mTaskScheduler->addCollisionObject(mCollisionObject.get(), CollisionType_Actor, collisionMask);
}
void Actor::updateCollisionMask()
{
mTaskScheduler->setCollisionFilterMask(mCollisionObject.get(), getCollisionMask());
}
int Actor::getCollisionMask() const
{
int collisionMask = CollisionType_World | CollisionType_HeightMap;
if (mExternalCollisionMode)
collisionMask |= CollisionType_Actor | CollisionType_Projectile | CollisionType_Door;
if (mCanWaterWalk)
collisionMask |= CollisionType_Water;
return collisionMask;
}
void Actor::updatePosition()
{
std::scoped_lock lock(mPositionMutex);
const auto worldPosition = mPtr.getRefData().getPosition().asVec3();
mPreviousPosition = worldPosition;
mPosition = worldPosition;
mSimulationPosition = worldPosition;
mPositionOffset = osg::Vec3f();
mStandingOnPtr = nullptr;
mSkipSimulation = true;
}
void Actor::setSimulationPosition(const osg::Vec3f& position)
{
if (!std::exchange(mSkipSimulation, false))
mSimulationPosition = position;
}
osg::Vec3f Actor::getScaledMeshTranslation() const
{
return mRotation * osg::componentMultiply(mMeshTranslation, mScale);
}
void Actor::updateCollisionObjectPosition()
{
std::scoped_lock lock(mPositionMutex);
updateCollisionObjectPositionUnsafe();
}
void Actor::updateCollisionObjectPositionUnsafe()
{
mShape->setLocalScaling(Misc::Convert::toBullet(mScale));
osg::Vec3f newPosition = getScaledMeshTranslation() + mPosition;
auto& trans = mCollisionObject->getWorldTransform();
trans.setOrigin(Misc::Convert::toBullet(newPosition));
trans.setRotation(Misc::Convert::toBullet(mRotation));
mCollisionObject->setWorldTransform(trans);
}
osg::Vec3f Actor::getCollisionObjectPosition() const
{
std::scoped_lock lock(mPositionMutex);
return getScaledMeshTranslation() + mPosition;
}
bool Actor::setPosition(const osg::Vec3f& position)
{
std::scoped_lock lock(mPositionMutex);
const bool worldPositionChanged = mPositionOffset.length2() != 0;
applyOffsetChange();
if (worldPositionChanged || mSkipSimulation)
return true;
mPreviousPosition = mPosition;
mPosition = position;
return mPreviousPosition != mPosition;
}
void Actor::adjustPosition(const osg::Vec3f& offset)
{
std::scoped_lock lock(mPositionMutex);
mPositionOffset += offset;
}
osg::Vec3f Actor::applyOffsetChange()
{
if (mPositionOffset.length2() != 0)
{
mPosition += mPositionOffset;
mPreviousPosition += mPositionOffset;
mSimulationPosition += mPositionOffset;
mPositionOffset = osg::Vec3f();
}
return mPosition;
}
void Actor::setRotation(osg::Quat quat)
{
std::scoped_lock lock(mPositionMutex);
mRotation = quat;
}
bool Actor::isRotationallyInvariant() const
{
return mRotationallyInvariant;
}
void Actor::updateScale()
{
std::scoped_lock lock(mPositionMutex);
updateScaleUnsafe();
}
void Actor::updateScaleUnsafe()
{
float scale = mPtr.getCellRef().getScale();
osg::Vec3f scaleVec(scale, scale, scale);
mPtr.getClass().adjustScale(mPtr, scaleVec, false);
mScale = scaleVec;
mHalfExtents = osg::componentMultiply(mOriginalHalfExtents, scaleVec);
scaleVec = osg::Vec3f(scale, scale, scale);
mPtr.getClass().adjustScale(mPtr, scaleVec, true);
mRenderingHalfExtents = osg::componentMultiply(mOriginalHalfExtents, scaleVec);
}
osg::Vec3f Actor::getHalfExtents() const
{
return mHalfExtents;
}
osg::Vec3f Actor::getOriginalHalfExtents() const
{
return mOriginalHalfExtents;
}
osg::Vec3f Actor::getRenderingHalfExtents() const
{
return mRenderingHalfExtents;
}
void Actor::setInertialForce(const osg::Vec3f& force)
{
mForce = force;
}
void Actor::setOnGround(bool grounded)
{
mOnGround = grounded;
}
void Actor::setOnSlope(bool slope)
{
mOnSlope = slope;
}
bool Actor::isWalkingOnWater() const
{
return mWalkingOnWater;
}
void Actor::setWalkingOnWater(bool walkingOnWater)
{
mWalkingOnWater = walkingOnWater;
}
void Actor::setCanWaterWalk(bool waterWalk)
{
if (waterWalk != mCanWaterWalk)
{
mCanWaterWalk = waterWalk;
updateCollisionMask();
}
}
MWWorld::Ptr Actor::getStandingOnPtr() const
{
std::scoped_lock lock(mPositionMutex);
return mStandingOnPtr;
}
void Actor::setStandingOnPtr(const MWWorld::Ptr& ptr)
{
std::scoped_lock lock(mPositionMutex);
mStandingOnPtr = ptr;
}
bool Actor::canMoveToWaterSurface(float waterlevel, const btCollisionWorld* world) const
{
const float halfZ = getHalfExtents().z();
const osg::Vec3f actorPosition = getPosition();
const osg::Vec3f startingPosition(actorPosition.x(), actorPosition.y(), actorPosition.z() + halfZ);
const osg::Vec3f destinationPosition(actorPosition.x(), actorPosition.y(), waterlevel + halfZ);
MWPhysics::ActorTracer tracer;
tracer.doTrace(getCollisionObject(), startingPosition, destinationPosition, world);
return (tracer.mFraction >= 1.0f);
}
void Actor::setMantleState(const Mantle::State& state)
{
if (state.mActive && (!mMantle.mActive || state.mElapsed < mMantle.mElapsed))
mMantleStarted = state.mHeight;
if (mMantle.mActive && !state.mActive)
mMantleFinished = true;
mMantle = state;
}
float Actor::takeMantleStarted()
{
const float height = mMantleStarted;
mMantleStarted = -1.f;
return height;
}
bool Actor::takeMantleFinished()
{
const bool finished = mMantleFinished;
mMantleFinished = false;
return finished;
}
}