Merge handleFall() and updateMechanics() into updateActor(). It remove gratuitious differences between sync and async cases. Also, it will make the after simulation update logic easier to follow when projectiles move into the simulation thread.

pull/3152/head
Frederic Chardon 3 years ago committed by fredzio
parent 0d1d3e67bc
commit 499b161e7b

@ -54,29 +54,6 @@ namespace
return actorData.mPosition.z() < actorData.mSwimLevel;
}
void handleFall(MWPhysics::ActorFrameData& actorData, bool simulationPerformed)
{
const float heightDiff = actorData.mPosition.z() - actorData.mOldHeight;
const bool isStillOnGround = (simulationPerformed && actorData.mWasOnGround && actorData.mIsOnGround);
if (isStillOnGround || actorData.mFlying || isUnderWater(actorData) || actorData.mSlowFall < 1)
actorData.mNeedLand = true;
else if (heightDiff < 0)
actorData.mFallHeight += heightDiff;
}
void updateMechanics(MWPhysics::Actor& actor, MWPhysics::ActorFrameData& actorData)
{
auto ptr = actor.getPtr();
MWMechanics::CreatureStats& stats = ptr.getClass().getCreatureStats(ptr);
if (actorData.mNeedLand)
stats.land(ptr == MWMechanics::getPlayer() && (actorData.mFlying || isUnderWater(actorData)));
else if (actorData.mFallHeight < 0)
stats.addToFallHeight(-actorData.mFallHeight);
}
osg::Vec3f interpolateMovements(MWPhysics::Actor& actor, MWPhysics::ActorFrameData& actorData, float timeAccum, float physicsDt)
{
const float interpolationFactor = std::clamp(timeAccum / physicsDt, 0.0f, 1.0f);
@ -221,10 +198,8 @@ namespace MWPhysics
if (mNumThreads != 0)
{
for (size_t i = 0; i < mActors.size(); ++i)
{
updateMechanics(*mActors[i], mActorsFrameData[i]);
updateActor(*mActors[i], mActorsFrameData[i], mAdvanceSimulation, mTimeAccum, mPhysicsDt);
}
if(mAdvanceSimulation)
mAsyncBudget.update(mTimer->delta_s(mAsyncStartTime, mTimeEnd), mPrevStepCount, mBudgetCursor);
updateStats(frameStart, frameNumber, stats);
@ -449,11 +424,6 @@ namespace MWPhysics
if (!mRemainingSteps)
{
while ((job = mNextJob.fetch_add(1, std::memory_order_relaxed)) < mNumJobs)
{
handleFall(mActorsFrameData[job], mAdvanceSimulation);
}
refreshLOSCache();
mPostSimBarrier->wait([this] { afterPostSim(); });
}
@ -476,6 +446,17 @@ namespace MWPhysics
void PhysicsTaskScheduler::updateActor(Actor& actor, ActorFrameData& actorData, bool simulationPerformed, float timeAccum, float dt) const
{
auto ptr = actor.getPtr();
MWMechanics::CreatureStats& stats = ptr.getClass().getCreatureStats(ptr);
const float heightDiff = actorData.mPosition.z() - actorData.mOldHeight;
const bool isStillOnGround = (simulationPerformed && actorData.mWasOnGround && actorData.mIsOnGround);
if (isStillOnGround || actorData.mFlying || isUnderWater(actorData) || actorData.mSlowFall < 1)
stats.land(ptr == MWMechanics::getPlayer() && (actorData.mFlying || isUnderWater(actorData)));
else if (heightDiff < 0)
stats.addToFallHeight(-heightDiff);
actor.setSimulationPosition(interpolateMovements(actor, actorData, timeAccum, dt));
actor.setLastStuckPosition(actorData.mLastStuckPosition);
actor.setStuckFrames(actorData.mStuckFrames);
@ -524,11 +505,8 @@ namespace MWPhysics
}
for (size_t i = 0; i < mActors.size(); ++i)
{
handleFall(mActorsFrameData[i], mAdvanceSimulation);
updateMechanics(*mActors[i], mActorsFrameData[i]);
updateActor(*mActors[i], mActorsFrameData[i], mAdvanceSimulation, mTimeAccum, mPhysicsDt);
}
refreshLOSCache();
}

@ -978,14 +978,12 @@ namespace MWPhysics
, mWaterlevel(waterlevel)
, mHalfExtentsZ(actor.getHalfExtents().z())
, mOldHeight(0)
, mFallHeight(0)
, mStuckFrames(0)
, mFlying(MWBase::Environment::get().getWorld()->isFlying(actor.getPtr()))
, mWasOnGround(actor.getOnGround())
, mIsAquatic(actor.getPtr().getClass().isPureWaterCreature(actor.getPtr()))
, mWaterCollision(waterCollision)
, mSkipCollisionDetection(actor.skipCollisions() || !actor.getCollisionMode())
, mNeedLand(false)
{
}

@ -97,14 +97,12 @@ namespace MWPhysics
const float mWaterlevel;
const float mHalfExtentsZ;
float mOldHeight;
float mFallHeight;
unsigned int mStuckFrames;
const bool mFlying;
const bool mWasOnGround;
const bool mIsAquatic;
const bool mWaterCollision;
const bool mSkipCollisionDetection;
bool mNeedLand;
};
struct WorldFrameData

Loading…
Cancel
Save