1 #include "movementsolver.hpp"
2 
3 #include <BulletCollision/CollisionDispatch/btCollisionObject.h>
4 #include <BulletCollision/CollisionDispatch/btCollisionWorld.h>
5 #include <BulletCollision/CollisionShapes/btCollisionShape.h>
6 
7 #include <components/esm/loadgmst.hpp>
8 #include <components/misc/convert.hpp>
9 
10 #include "../mwbase/world.hpp"
11 #include "../mwbase/environment.hpp"
12 
13 #include "../mwworld/class.hpp"
14 #include "../mwworld/esmstore.hpp"
15 #include "../mwworld/refdata.hpp"
16 
17 #include "actor.hpp"
18 #include "collisiontype.hpp"
19 #include "constants.hpp"
20 #include "contacttestwrapper.h"
21 #include "physicssystem.hpp"
22 #include "stepper.hpp"
23 #include "trace.h"
24 
25 #include <cmath>
26 
27 namespace MWPhysics
28 {
isActor(const btCollisionObject * obj)29     static bool isActor(const btCollisionObject *obj)
30     {
31         assert(obj);
32         return obj->getBroadphaseHandle()->m_collisionFilterGroup == CollisionType_Actor;
33     }
34 
35     class ContactCollectionCallback : public btCollisionWorld::ContactResultCallback
36     {
37     public:
ContactCollectionCallback(const btCollisionObject * me,osg::Vec3f velocity)38         ContactCollectionCallback(const btCollisionObject * me, osg::Vec3f velocity) : mMe(me)
39         {
40             m_collisionFilterGroup = me->getBroadphaseHandle()->m_collisionFilterGroup;
41             m_collisionFilterMask = me->getBroadphaseHandle()->m_collisionFilterMask & ~CollisionType_Projectile;
42             mVelocity = Misc::Convert::toBullet(velocity);
43         }
addSingleResult(btManifoldPoint & contact,const btCollisionObjectWrapper * colObj0Wrap,int partId0,int index0,const btCollisionObjectWrapper * colObj1Wrap,int partId1,int index1)44         btScalar addSingleResult(btManifoldPoint & contact, const btCollisionObjectWrapper * colObj0Wrap, int partId0, int index0, const btCollisionObjectWrapper * colObj1Wrap, int partId1, int index1) override
45         {
46             if (isActor(colObj0Wrap->getCollisionObject()) && isActor(colObj1Wrap->getCollisionObject()))
47                 return 0.0;
48             // ignore overlap if we're moving in the same direction as it would push us out (don't change this to >=, that would break detection when not moving)
49             if (contact.m_normalWorldOnB.dot(mVelocity) > 0.0)
50                 return 0.0;
51             auto delta = contact.m_normalWorldOnB * -contact.m_distance1;
52             mContactSum += delta;
53             mMaxX = std::max(std::abs(delta.x()), mMaxX);
54             mMaxY = std::max(std::abs(delta.y()), mMaxY);
55             mMaxZ = std::max(std::abs(delta.z()), mMaxZ);
56             if (contact.m_distance1 < mDistance)
57             {
58                 mDistance = contact.m_distance1;
59                 mNormal = contact.m_normalWorldOnB;
60                 mDelta = delta;
61                 return mDistance;
62             }
63             else
64             {
65                 return 0.0;
66             }
67         }
68         btScalar mMaxX = 0.0;
69         btScalar mMaxY = 0.0;
70         btScalar mMaxZ = 0.0;
71         btVector3 mContactSum{0.0, 0.0, 0.0};
72         btVector3 mNormal{0.0, 0.0, 0.0}; // points towards "me"
73         btVector3 mDelta{0.0, 0.0, 0.0}; // points towards "me"
74         btScalar mDistance = 0.0; // negative or zero
75     protected:
76         btVector3 mVelocity;
77         const btCollisionObject * mMe;
78     };
79 
traceDown(const MWWorld::Ptr & ptr,const osg::Vec3f & position,Actor * actor,btCollisionWorld * collisionWorld,float maxHeight)80     osg::Vec3f MovementSolver::traceDown(const MWWorld::Ptr &ptr, const osg::Vec3f& position, Actor* actor, btCollisionWorld* collisionWorld, float maxHeight)
81     {
82         osg::Vec3f offset = actor->getCollisionObjectPosition() - ptr.getRefData().getPosition().asVec3();
83 
84         ActorTracer tracer;
85         tracer.findGround(actor, position + offset, position + offset - osg::Vec3f(0,0,maxHeight), collisionWorld);
86         if (tracer.mFraction >= 1.0f)
87         {
88             actor->setOnGround(false);
89             return position;
90         }
91 
92         actor->setOnGround(true);
93 
94         // Check if we actually found a valid spawn point (use an infinitely thin ray this time).
95         // Required for some broken door destinations in Morrowind.esm, where the spawn point
96         // intersects with other geometry if the actor's base is taken into account
97         btVector3 from = Misc::Convert::toBullet(position);
98         btVector3 to = from - btVector3(0,0,maxHeight);
99 
100         btCollisionWorld::ClosestRayResultCallback resultCallback1(from, to);
101         resultCallback1.m_collisionFilterGroup = 0xff;
102         resultCallback1.m_collisionFilterMask = CollisionType_World|CollisionType_HeightMap;
103 
104         collisionWorld->rayTest(from, to, resultCallback1);
105 
106         if (resultCallback1.hasHit() && ((Misc::Convert::toOsg(resultCallback1.m_hitPointWorld) - tracer.mEndPos + offset).length2() > 35*35
107             || !isWalkableSlope(tracer.mPlaneNormal)))
108         {
109             actor->setOnSlope(!isWalkableSlope(resultCallback1.m_hitNormalWorld));
110             return Misc::Convert::toOsg(resultCallback1.m_hitPointWorld) + osg::Vec3f(0.f, 0.f, sGroundOffset);
111         }
112 
113         actor->setOnSlope(!isWalkableSlope(tracer.mPlaneNormal));
114 
115         return tracer.mEndPos-offset + osg::Vec3f(0.f, 0.f, sGroundOffset);
116     }
117 
move(ActorFrameData & actor,float time,const btCollisionWorld * collisionWorld,WorldFrameData & worldData)118     void MovementSolver::move(ActorFrameData& actor, float time, const btCollisionWorld* collisionWorld,
119                                            WorldFrameData& worldData)
120     {
121         auto* physicActor = actor.mActorRaw;
122         const ESM::Position& refpos = actor.mRefpos;
123         // Early-out for totally static creatures
124         // (Not sure if gravity should still apply?)
125         {
126             const auto ptr = physicActor->getPtr();
127             if (!ptr.getClass().isMobile(ptr))
128                 return;
129         }
130 
131         // Reset per-frame data
132         physicActor->setWalkingOnWater(false);
133         // Anything to collide with?
134         if(!physicActor->getCollisionMode() || actor.mSkipCollisionDetection)
135         {
136             actor.mPosition += (osg::Quat(refpos.rot[0], osg::Vec3f(-1, 0, 0)) *
137                                 osg::Quat(refpos.rot[2], osg::Vec3f(0, 0, -1))
138                                 ) * actor.mMovement * time;
139             return;
140         }
141 
142         const btCollisionObject *colobj = physicActor->getCollisionObject();
143 
144         // Adjust for collision mesh offset relative to actor's "location"
145         // (doTrace doesn't take local/interior collision shape translation into account, so we have to do it on our own)
146         // for compatibility with vanilla assets, we have to derive this from the vertical half extent instead of from internal hull translation
147         // if not for this hack, the "correct" collision hull position would be physicActor->getScaledMeshTranslation()
148         osg::Vec3f halfExtents = physicActor->getHalfExtents();
149         actor.mPosition.z() += halfExtents.z(); // vanilla-accurate
150 
151         static const float fSwimHeightScale = MWBase::Environment::get().getWorld()->getStore().get<ESM::GameSetting>().find("fSwimHeightScale")->mValue.getFloat();
152         float swimlevel = actor.mWaterlevel + halfExtents.z() - (physicActor->getRenderingHalfExtents().z() * 2 * fSwimHeightScale);
153 
154         ActorTracer tracer;
155 
156         osg::Vec3f inertia = physicActor->getInertialForce();
157         osg::Vec3f velocity;
158 
159         if (actor.mPosition.z() < swimlevel || actor.mFlying)
160         {
161             velocity = (osg::Quat(refpos.rot[0], osg::Vec3f(-1, 0, 0)) * osg::Quat(refpos.rot[2], osg::Vec3f(0, 0, -1))) * actor.mMovement;
162         }
163         else
164         {
165             velocity = (osg::Quat(refpos.rot[2], osg::Vec3f(0, 0, -1))) * actor.mMovement;
166 
167             if ((velocity.z() > 0.f && physicActor->getOnGround() && !physicActor->getOnSlope())
168             || (velocity.z() > 0.f && velocity.z() + inertia.z() <= -velocity.z() && physicActor->getOnSlope()))
169                 inertia = velocity;
170             else if (!physicActor->getOnGround() || physicActor->getOnSlope())
171                 velocity = velocity + inertia;
172         }
173 
174         // Dead and paralyzed actors underwater will float to the surface,
175         // if the CharacterController tells us to do so
176         if (actor.mMovement.z() > 0 && actor.mFloatToSurface && actor.mPosition.z() < swimlevel)
177             velocity = osg::Vec3f(0,0,1) * 25;
178 
179         if (actor.mWantJump)
180             actor.mDidJump = true;
181 
182         // Now that we have the effective movement vector, apply wind forces to it
183         if (worldData.mIsInStorm)
184         {
185             osg::Vec3f stormDirection = worldData.mStormDirection;
186             float angleDegrees = osg::RadiansToDegrees(std::acos(stormDirection * velocity / (stormDirection.length() * velocity.length())));
187             static const float fStromWalkMult = MWBase::Environment::get().getWorld()->getStore().get<ESM::GameSetting>().find("fStromWalkMult")->mValue.getFloat();
188             velocity *= 1.f-(fStromWalkMult * (angleDegrees/180.f));
189         }
190 
191         Stepper stepper(collisionWorld, colobj);
192         osg::Vec3f origVelocity = velocity;
193         osg::Vec3f newPosition = actor.mPosition;
194         /*
195          * A loop to find newPosition using tracer, if successful different from the starting position.
196          * nextpos is the local variable used to find potential newPosition, using velocity and remainingTime
197          * The initial velocity was set earlier (see above).
198         */
199         float remainingTime = time;
200         bool seenGround = physicActor->getOnGround() && !physicActor->getOnSlope() && !actor.mFlying;
201 
202         int numTimesSlid = 0;
203         osg::Vec3f lastSlideNormal(0,0,1);
204         osg::Vec3f lastSlideNormalFallback(0,0,1);
205         bool forceGroundTest = false;
206 
207         for (int iterations = 0; iterations < sMaxIterations && remainingTime > 0.0001f; ++iterations)
208         {
209             osg::Vec3f nextpos = newPosition + velocity * remainingTime;
210 
211             // If not able to fly, don't allow to swim up into the air
212             if(!actor.mFlying && nextpos.z() > swimlevel && newPosition.z() < swimlevel)
213             {
214                 const osg::Vec3f down(0,0,-1);
215                 velocity = reject(velocity, down);
216                 // NOTE: remainingTime is unchanged before the loop continues
217                 continue; // velocity updated, calculate nextpos again
218             }
219 
220             if((newPosition - nextpos).length2() > 0.0001)
221             {
222                 // trace to where character would go if there were no obstructions
223                 tracer.doTrace(colobj, newPosition, nextpos, collisionWorld);
224 
225                 // check for obstructions
226                 if(tracer.mFraction >= 1.0f)
227                 {
228                     newPosition = tracer.mEndPos; // ok to move, so set newPosition
229                     break;
230                 }
231             }
232             else
233             {
234                 // The current position and next position are nearly the same, so just exit.
235                 // Note: Bullet can trigger an assert in debug modes if the positions
236                 // are the same, since that causes it to attempt to normalize a zero
237                 // length vector (which can also happen with nearly identical vectors, since
238                 // precision can be lost due to any math Bullet does internally). Since we
239                 // aren't performing any collision detection, we want to reject the next
240                 // position, so that we don't slowly move inside another object.
241                 break;
242             }
243 
244             if (isWalkableSlope(tracer.mPlaneNormal) && !actor.mFlying && newPosition.z() >= swimlevel)
245                 seenGround = true;
246 
247             // We hit something. Check if we can step up.
248             float hitHeight = tracer.mHitPoint.z() - tracer.mEndPos.z() + halfExtents.z();
249             osg::Vec3f oldPosition = newPosition;
250             bool usedStepLogic = false;
251             if (hitHeight < sStepSizeUp && !isActor(tracer.mHitObject))
252             {
253                 // Try to step up onto it.
254                 // NOTE: this modifies newPosition and velocity on its own if successful
255                 usedStepLogic = stepper.step(newPosition, velocity, remainingTime, seenGround, iterations == 0);
256             }
257             if (usedStepLogic)
258             {
259                 // don't let pure water creatures move out of water after stepMove
260                 const auto ptr = physicActor->getPtr();
261                 if (ptr.getClass().isPureWaterCreature(ptr) && newPosition.z() + halfExtents.z() > actor.mWaterlevel)
262                     newPosition = oldPosition;
263                 else if(!actor.mFlying && actor.mPosition.z() >= swimlevel)
264                     forceGroundTest = true;
265             }
266             else
267             {
268                 // Can't step up, so slide against what we ran into
269                 remainingTime *= (1.0f-tracer.mFraction);
270 
271                 auto planeNormal = tracer.mPlaneNormal;
272 
273                 // If we touched the ground this frame, and whatever we ran into is a wall of some sort,
274                 // pretend that its collision normal is pointing horizontally
275                 // (fixes snagging on slightly downward-facing walls, and crawling up the bases of very steep walls because of the collision margin)
276                 if (seenGround && !isWalkableSlope(planeNormal) && planeNormal.z() != 0)
277                 {
278                     planeNormal.z() = 0;
279                     planeNormal.normalize();
280                 }
281 
282                 // Move up to what we ran into (with a bit of a collision margin)
283                 if ((newPosition-tracer.mEndPos).length2() > sCollisionMargin*sCollisionMargin)
284                 {
285                     auto direction = velocity;
286                     direction.normalize();
287                     newPosition = tracer.mEndPos;
288                     newPosition -= direction*sCollisionMargin;
289                 }
290 
291                 osg::Vec3f newVelocity = (velocity * planeNormal <= 0.0) ? reject(velocity, planeNormal) : velocity;
292                 bool usedSeamLogic = false;
293 
294                 // check for the current and previous collision planes forming an acute angle; slide along the seam if they do
295                 if(numTimesSlid > 0)
296                 {
297                     auto dotA = lastSlideNormal * planeNormal;
298                     auto dotB = lastSlideNormalFallback * planeNormal;
299                     if(numTimesSlid <= 1) // ignore fallback normal if this is only the first or second slide
300                         dotB = 1.0;
301                     if(dotA <= 0.0 || dotB <= 0.0)
302                     {
303                         osg::Vec3f bestNormal = lastSlideNormal;
304                         // use previous-to-previous collision plane if it's acute with current plane but actual previous plane isn't
305                         if(dotB < dotA)
306                         {
307                             bestNormal = lastSlideNormalFallback;
308                             lastSlideNormal = lastSlideNormalFallback;
309                         }
310 
311                         auto constraintVector = bestNormal ^ planeNormal; // cross product
312                         if(constraintVector.length2() > 0) // only if it's not zero length
313                         {
314                             constraintVector.normalize();
315                             newVelocity = project(velocity, constraintVector);
316 
317                             // version of surface rejection for acute crevices/seams
318                             auto averageNormal = bestNormal + planeNormal;
319                             averageNormal.normalize();
320                             tracer.doTrace(colobj, newPosition, newPosition + averageNormal*(sCollisionMargin*2.0), collisionWorld);
321                             newPosition = (newPosition + tracer.mEndPos)/2.0;
322 
323                             usedSeamLogic = true;
324                         }
325                     }
326                 }
327                 // otherwise just keep the normal vector rejection
328 
329                 // if this isn't the first iteration, or if the first iteration is also the last iteration,
330                 // move away from the collision plane slightly, if possible
331                 // this reduces getting stuck in some concave geometry, like the gaps above the railings in some ald'ruhn buildings
332                 // this is different from the normal collision margin, because the normal collision margin is along the movement path,
333                 // but this is along the collision normal
334                 if(!usedSeamLogic && (iterations > 0 || remainingTime < 0.01f))
335                 {
336                     tracer.doTrace(colobj, newPosition, newPosition + planeNormal*(sCollisionMargin*2.0), collisionWorld);
337                     newPosition = (newPosition + tracer.mEndPos)/2.0;
338                 }
339 
340                 // Do not allow sliding up steep slopes if there is gravity.
341                 if (newPosition.z() >= swimlevel && !actor.mFlying && !isWalkableSlope(planeNormal))
342                     newVelocity.z() = std::min(newVelocity.z(), velocity.z());
343 
344                 if (newVelocity * origVelocity <= 0.0f)
345                     break;
346 
347                 numTimesSlid += 1;
348                 lastSlideNormalFallback = lastSlideNormal;
349                 lastSlideNormal = planeNormal;
350                 velocity = newVelocity;
351             }
352         }
353 
354         bool isOnGround = false;
355         bool isOnSlope = false;
356         if (forceGroundTest || (inertia.z() <= 0.f && newPosition.z() >= swimlevel))
357         {
358             osg::Vec3f from = newPosition;
359             auto dropDistance = 2*sGroundOffset + (physicActor->getOnGround() ? sStepSizeDown : 0);
360             osg::Vec3f to = newPosition - osg::Vec3f(0,0,dropDistance);
361             tracer.doTrace(colobj, from, to, collisionWorld);
362             if(tracer.mFraction < 1.0f)
363             {
364                 if (!isActor(tracer.mHitObject))
365                 {
366                     isOnGround = true;
367                     isOnSlope = !isWalkableSlope(tracer.mPlaneNormal);
368 
369                     const btCollisionObject* standingOn = tracer.mHitObject;
370                     PtrHolder* ptrHolder = static_cast<PtrHolder*>(standingOn->getUserPointer());
371                     if (ptrHolder)
372                         actor.mStandingOn = ptrHolder->getPtr();
373 
374                     if (standingOn->getBroadphaseHandle()->m_collisionFilterGroup == CollisionType_Water)
375                         physicActor->setWalkingOnWater(true);
376                     if (!actor.mFlying && !isOnSlope)
377                     {
378                         if (tracer.mFraction*dropDistance > sGroundOffset)
379                             newPosition.z() = tracer.mEndPos.z() + sGroundOffset;
380                         else
381                         {
382                             newPosition.z() = tracer.mEndPos.z();
383                             tracer.doTrace(colobj, newPosition, newPosition + osg::Vec3f(0, 0, 2*sGroundOffset), collisionWorld);
384                             newPosition = (newPosition+tracer.mEndPos)/2.0;
385                         }
386                     }
387                 }
388                 else
389                 {
390                     // Vanilla allows actors to float on top of other actors. Do not push them off.
391                     if (!actor.mFlying && isWalkableSlope(tracer.mPlaneNormal) && tracer.mEndPos.z()+sGroundOffset <= newPosition.z())
392                         newPosition.z() = tracer.mEndPos.z() + sGroundOffset;
393 
394                     isOnGround = false;
395                 }
396             }
397             // forcibly treat stuck actors as if they're on flat ground because buggy collisions when inside of things can/will break ground detection
398             if(physicActor->getStuckFrames() > 0)
399             {
400                 isOnGround = true;
401                 isOnSlope = false;
402             }
403         }
404 
405         if((isOnGround && !isOnSlope) || newPosition.z() < swimlevel || actor.mFlying)
406             physicActor->setInertialForce(osg::Vec3f(0.f, 0.f, 0.f));
407         else
408         {
409             inertia.z() -= time * Constants::GravityConst * Constants::UnitsPerMeter;
410             if (inertia.z() < 0)
411                 inertia.z() *= actor.mSlowFall;
412             if (actor.mSlowFall < 1.f) {
413                 inertia.x() *= actor.mSlowFall;
414                 inertia.y() *= actor.mSlowFall;
415             }
416             physicActor->setInertialForce(inertia);
417         }
418         physicActor->setOnGround(isOnGround);
419         physicActor->setOnSlope(isOnSlope);
420 
421         actor.mPosition = newPosition;
422         // remove what was added earlier in compensating for doTrace not taking interior transformation into account
423         actor.mPosition.z() -= halfExtents.z(); // vanilla-accurate
424     }
425 
addMarginToDelta(btVector3 delta)426     btVector3 addMarginToDelta(btVector3 delta)
427     {
428         if(delta.length2() == 0.0)
429             return delta;
430         return delta + delta.normalized() * sCollisionMargin;
431     }
432 
unstuck(ActorFrameData & actor,const btCollisionWorld * collisionWorld)433     void MovementSolver::unstuck(ActorFrameData& actor, const btCollisionWorld* collisionWorld)
434     {
435         const auto& ptr = actor.mActorRaw->getPtr();
436         if (!ptr.getClass().isMobile(ptr))
437             return;
438 
439         auto* physicActor = actor.mActorRaw;
440         if(!physicActor->getCollisionMode() || actor.mSkipCollisionDetection) // noclipping/tcl
441             return;
442 
443         auto* collisionObject = physicActor->getCollisionObject();
444         auto tempPosition = actor.mPosition;
445 
446         if(physicActor->getStuckFrames() >= 10)
447         {
448             if((physicActor->getLastStuckPosition() - actor.mPosition).length2() < 100)
449                 return;
450             else
451             {
452                 physicActor->setStuckFrames(0);
453                 physicActor->setLastStuckPosition({0, 0, 0});
454             }
455         }
456 
457         // use vanilla-accurate collision hull position hack (do same hitbox offset hack as movement solver)
458         // if vanilla compatibility didn't matter, the "correct" collision hull position would be physicActor->getScaledMeshTranslation()
459         const auto verticalHalfExtent = osg::Vec3f(0.0, 0.0, physicActor->getHalfExtents().z());
460 
461         // use a 3d approximation of the movement vector to better judge player intent
462         auto velocity = (osg::Quat(actor.mRefpos.rot[0], osg::Vec3f(-1, 0, 0)) * osg::Quat(actor.mRefpos.rot[2], osg::Vec3f(0, 0, -1))) * actor.mMovement;
463         // try to pop outside of the world before doing anything else if we're inside of it
464         if (!physicActor->getOnGround() || physicActor->getOnSlope())
465                 velocity += physicActor->getInertialForce();
466 
467         // because of the internal collision box offset hack, and the fact that we're moving the collision box manually,
468         // we need to replicate part of the collision box's transform process from scratch
469         osg::Vec3f refPosition = tempPosition + verticalHalfExtent;
470         osg::Vec3f goodPosition = refPosition;
471         const btTransform oldTransform = collisionObject->getWorldTransform();
472         btTransform newTransform = oldTransform;
473 
474         auto gatherContacts = [&](btVector3 newOffset) -> ContactCollectionCallback
475         {
476             goodPosition = refPosition + Misc::Convert::toOsg(addMarginToDelta(newOffset));
477             newTransform.setOrigin(Misc::Convert::toBullet(goodPosition));
478             collisionObject->setWorldTransform(newTransform);
479 
480             ContactCollectionCallback callback{collisionObject, velocity};
481             ContactTestWrapper::contactTest(const_cast<btCollisionWorld*>(collisionWorld), collisionObject, callback);
482             return callback;
483         };
484 
485         // check whether we're inside the world with our collision box with manually-derived offset
486         auto contactCallback = gatherContacts({0.0, 0.0, 0.0});
487         if(contactCallback.mDistance < -sAllowedPenetration)
488         {
489             physicActor->setStuckFrames(physicActor->getStuckFrames() + 1);
490             physicActor->setLastStuckPosition(actor.mPosition);
491             // we are; try moving it out of the world
492             auto positionDelta = contactCallback.mContactSum;
493             // limit rejection delta to the largest known individual rejections
494             if(std::abs(positionDelta.x()) > contactCallback.mMaxX)
495                 positionDelta *= contactCallback.mMaxX / std::abs(positionDelta.x());
496             if(std::abs(positionDelta.y()) > contactCallback.mMaxY)
497                 positionDelta *= contactCallback.mMaxY / std::abs(positionDelta.y());
498             if(std::abs(positionDelta.z()) > contactCallback.mMaxZ)
499                 positionDelta *= contactCallback.mMaxZ / std::abs(positionDelta.z());
500 
501             auto contactCallback2 = gatherContacts(positionDelta);
502             // successfully moved further out from contact (does not have to be in open space, just less inside of things)
503             if(contactCallback2.mDistance > contactCallback.mDistance)
504                 tempPosition = goodPosition - verticalHalfExtent;
505             // try again but only upwards (fixes some bad coc floors)
506             else
507             {
508                 // upwards-only offset
509                 auto contactCallback3 = gatherContacts({0.0, 0.0, std::abs(positionDelta.z())});
510                 // success
511                 if(contactCallback3.mDistance > contactCallback.mDistance)
512                     tempPosition = goodPosition - verticalHalfExtent;
513                 else
514                 // try again but fixed distance up
515                 {
516                     auto contactCallback4 = gatherContacts({0.0, 0.0, 10.0});
517                     // success
518                     if(contactCallback4.mDistance > contactCallback.mDistance)
519                         tempPosition = goodPosition - verticalHalfExtent;
520                 }
521             }
522         }
523         else
524         {
525             physicActor->setStuckFrames(0);
526             physicActor->setLastStuckPosition({0, 0, 0});
527         }
528 
529         collisionObject->setWorldTransform(oldTransform);
530         actor.mPosition = tempPosition;
531     }
532 }
533