diff --git a/apps/openmw/mwphysics/mtphysics.cpp b/apps/openmw/mwphysics/mtphysics.cpp index 9a4ec2cba2..86760a67c6 100644 --- a/apps/openmw/mwphysics/mtphysics.cpp +++ b/apps/openmw/mwphysics/mtphysics.cpp @@ -208,23 +208,16 @@ namespace { osg::Vec3f movement = osg::Vec3f(); auto it = actor.movement().begin(); - while (it != actor.movement().end()) - { - if (it->jump) - { - // Adjusting inertia is instant and should not be performed over time like other movement is. - it++; - continue; - } - - float start = std::max(it->simulationTimeStart, startTime); - float stop = std::min(it->simulationTimeStop, endTime); - movement += it->velocity * (stop - start); - if (std::abs(stop - it->simulationTimeStop) < 0.0001f) - it = actor.movement().erase(it); - else - it++; - } + std::erase_if(actor.movement(), [&](MWPhysics::Movement& v) { + if (v.mJump) + return false; + float start = std::max(v.mSimulationTimeStart, startTime); + float stop = std::min(v.mSimulationTimeStop, endTime); + movement += v.mVelocity * (stop - start); + if (std::abs(stop - v.mSimulationTimeStop) < 0.0001f) + return true; + return false; + }); return movement; } @@ -232,17 +225,14 @@ namespace std::optional takeInertia(MWPhysics::PtrHolder& actor, float startTime) const { std::optional inertia = std::nullopt; - auto it = actor.movement().begin(); - while (it != actor.movement().end()) - { - if (it->jump && it->simulationTimeStart >= startTime) + std::erase_if(actor.movement(), [&](MWPhysics::Movement& v) { + if (v.mJump && v.mSimulationTimeStart >= startTime) { - inertia = it->velocity; - it = actor.movement().erase(it); + inertia = v.mVelocity; + return true; } - else - it++; - } + return false; + }); return inertia; } diff --git a/apps/openmw/mwphysics/ptrholder.hpp b/apps/openmw/mwphysics/ptrholder.hpp index d7a6f887bb..ecc626b44c 100644 --- a/apps/openmw/mwphysics/ptrholder.hpp +++ b/apps/openmw/mwphysics/ptrholder.hpp @@ -1,7 +1,7 @@ #ifndef OPENMW_MWPHYSICS_PTRHOLDER_H #define OPENMW_MWPHYSICS_PTRHOLDER_H -#include +#include #include #include #include @@ -16,10 +16,10 @@ namespace MWPhysics { struct Movement { - osg::Vec3f velocity = osg::Vec3f(); - float simulationTimeStart = 0.f; // The time at which this movement begun - float simulationTimeStop = 0.f; // The time at which this movement finished - bool jump = false; + osg::Vec3f mVelocity = osg::Vec3f(); + float mSimulationTimeStart = 0.f; // The time at which this movement begun + float mSimulationTimeStop = 0.f; // The time at which this movement finished + bool mJump = false; }; class PtrHolder @@ -47,7 +47,7 @@ namespace MWPhysics mMovement.push_back(Movement{ velocity, simulationTimeStart, simulationTimeStop, jump }); } - std::deque& movement() { return mMovement; } + std::list& movement() { return mMovement; } void setSimulationPosition(const osg::Vec3f& position) { mSimulationPosition = position; } @@ -66,7 +66,7 @@ namespace MWPhysics protected: MWWorld::Ptr mPtr; std::unique_ptr mCollisionObject; - std::deque mMovement; + std::list mMovement; osg::Vec3f mSimulationPosition; osg::Vec3d mPosition; osg::Vec3d mPreviousPosition;