From fb073e5c1441e95f0b8ee3021ee9b64c28a53919 Mon Sep 17 00:00:00 2001 From: scrawl Date: Thu, 23 Feb 2017 22:34:42 +0100 Subject: [PATCH] Avoid unnecessary AABB update for rotationally invariant collision shapes --- apps/openmw/mwphysics/actor.cpp | 10 ++++++++++ apps/openmw/mwphysics/actor.hpp | 7 +++++++ apps/openmw/mwphysics/physicssystem.cpp | 7 +++++-- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/apps/openmw/mwphysics/actor.cpp b/apps/openmw/mwphysics/actor.cpp index e0edd515d..2730bf4f0 100644 --- a/apps/openmw/mwphysics/actor.cpp +++ b/apps/openmw/mwphysics/actor.cpp @@ -32,9 +32,14 @@ Actor::Actor(const MWWorld::Ptr& ptr, osg::ref_ptr if (std::abs(mHalfExtents.x()-mHalfExtents.y())= mHalfExtents.x()) { mShape.reset(new btCapsuleShapeZ(mHalfExtents.x(), 2*mHalfExtents.z() - 2*mHalfExtents.x())); + mRotationallyInvariant = true; } else + { mShape.reset(new btBoxShape(toBullet(mHalfExtents))); + mRotationallyInvariant = false; + } + mConvexShape = static_cast(mShape.get()); mCollisionObject.reset(new btCollisionObject); @@ -144,6 +149,11 @@ void Actor::updateRotation () updateCollisionObjectPosition(); } +bool Actor::isRotationallyInvariant() const +{ + return mRotationallyInvariant; +} + void Actor::updateScale() { float scale = mPtr.getCellRef().getScale(); diff --git a/apps/openmw/mwphysics/actor.hpp b/apps/openmw/mwphysics/actor.hpp index a640640e2..88f7f5537 100644 --- a/apps/openmw/mwphysics/actor.hpp +++ b/apps/openmw/mwphysics/actor.hpp @@ -72,6 +72,11 @@ namespace MWPhysics void updateScale(); void updateRotation(); + /** + * Return true if the collision shape looks the same no matter how its Z rotated. + */ + bool isRotationallyInvariant() const; + /** * Set mPosition and mPreviousPosition to the position in the Ptr's RefData. This should be used * when an object is "instantly" moved/teleported as opposed to being moved by the physics simulation. @@ -155,6 +160,8 @@ namespace MWPhysics bool mCanWaterWalk; bool mWalkingOnWater; + bool mRotationallyInvariant; + std::auto_ptr mShape; btConvexShape* mConvexShape; diff --git a/apps/openmw/mwphysics/physicssystem.cpp b/apps/openmw/mwphysics/physicssystem.cpp index 454dc611d..c3e2d7c1b 100644 --- a/apps/openmw/mwphysics/physicssystem.cpp +++ b/apps/openmw/mwphysics/physicssystem.cpp @@ -1294,8 +1294,11 @@ namespace MWPhysics ActorMap::iterator foundActor = mActors.find(ptr); if (foundActor != mActors.end()) { - foundActor->second->updateRotation(); - mCollisionWorld->updateSingleAabb(foundActor->second->getCollisionObject()); + if (!foundActor->second->isRotationallyInvariant()) + { + foundActor->second->updateRotation(); + mCollisionWorld->updateSingleAabb(foundActor->second->getCollisionObject()); + } return; } }