#include "recastmeshobject.hpp" #include #include #include namespace DetourNavigator { namespace { bool updateCompoundObject(const btCompoundShape& shape, const AreaType areaType, std::vector& children) { assert(static_cast(shape.getNumChildShapes()) == children.size()); bool result = false; for (int i = 0, num = shape.getNumChildShapes(); i < num; ++i) { assert(shape.getChildShape(i) == std::addressof(children[static_cast(i)].getShape())); result = children[static_cast(i)].update(shape.getChildTransform(i), areaType) || result; } return result; } std::vector makeChildrenObjects(const osg::ref_ptr& holder, const btCompoundShape& shape, const AreaType areaType) { std::vector result; for (int i = 0, num = shape.getNumChildShapes(); i < num; ++i) { const CollisionShape collisionShape {holder, *shape.getChildShape(i)}; result.emplace_back(collisionShape, shape.getChildTransform(i), areaType); } return result; } std::vector makeChildrenObjects(const osg::ref_ptr& holder, const btCollisionShape& shape, const AreaType areaType) { if (shape.isCompound()) return makeChildrenObjects(holder, static_cast(shape), areaType); return std::vector(); } } RecastMeshObject::RecastMeshObject(const CollisionShape& shape, const btTransform& transform, const AreaType areaType) : mHolder(shape.getHolder()) , mShape(shape.getShape()) , mTransform(transform) , mAreaType(areaType) , mLocalScaling(mShape.get().getLocalScaling()) , mChildren(makeChildrenObjects(mHolder, mShape.get(), mAreaType)) { } bool RecastMeshObject::update(const btTransform& transform, const AreaType areaType) { bool result = false; if (!(mTransform == transform)) { mTransform = transform; result = true; } if (mAreaType != areaType) { mAreaType = areaType; result = true; } if (!(mLocalScaling == mShape.get().getLocalScaling())) { mLocalScaling = mShape.get().getLocalScaling(); result = true; } if (mShape.get().isCompound()) result = updateCompoundObject(static_cast(mShape.get()), mAreaType, mChildren) || result; return result; } }