2015-11-16 22:30:10 +00:00
|
|
|
#include "bulletshape.hpp"
|
|
|
|
|
|
|
|
#include <stdexcept>
|
2016-01-25 13:13:16 +00:00
|
|
|
#include <string>
|
2015-11-16 22:30:10 +00:00
|
|
|
|
|
|
|
#include <BulletCollision/CollisionShapes/btBoxShape.h>
|
|
|
|
#include <BulletCollision/CollisionShapes/btScaledBvhTriangleMeshShape.h>
|
|
|
|
#include <BulletCollision/CollisionShapes/btCompoundShape.h>
|
2021-08-01 00:13:55 +00:00
|
|
|
#include <BulletCollision/CollisionShapes/btHeightfieldTerrainShape.h>
|
2015-11-16 22:30:10 +00:00
|
|
|
|
|
|
|
namespace Resource
|
|
|
|
{
|
2021-10-30 00:58:40 +00:00
|
|
|
namespace
|
|
|
|
{
|
2021-10-30 01:06:22 +00:00
|
|
|
CollisionShapePtr duplicateCollisionShape(const btCollisionShape *shape)
|
2021-10-30 00:58:40 +00:00
|
|
|
{
|
|
|
|
if (shape == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
if (shape->isCompound())
|
|
|
|
{
|
|
|
|
const btCompoundShape *comp = static_cast<const btCompoundShape*>(shape);
|
2021-10-30 01:06:22 +00:00
|
|
|
std::unique_ptr<btCompoundShape, DeleteCollisionShape> newShape(new btCompoundShape);
|
2021-10-30 00:58:40 +00:00
|
|
|
|
|
|
|
for (int i = 0, n = comp->getNumChildShapes(); i < n; ++i)
|
|
|
|
{
|
2021-10-30 01:06:22 +00:00
|
|
|
auto child = duplicateCollisionShape(comp->getChildShape(i));
|
2021-10-30 00:58:40 +00:00
|
|
|
const btTransform& trans = comp->getChildTransform(i);
|
2021-10-30 01:06:22 +00:00
|
|
|
newShape->addChildShape(trans, child.release());
|
2021-10-30 00:58:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return newShape;
|
|
|
|
}
|
|
|
|
|
2021-10-30 01:04:54 +00:00
|
|
|
if (shape->getShapeType() == TRIANGLE_MESH_SHAPE_PROXYTYPE)
|
|
|
|
{
|
|
|
|
const btBvhTriangleMeshShape* trishape = static_cast<const btBvhTriangleMeshShape*>(shape);
|
2021-10-30 01:06:22 +00:00
|
|
|
return CollisionShapePtr(new btScaledBvhTriangleMeshShape(const_cast<btBvhTriangleMeshShape*>(trishape), btVector3(1.f, 1.f, 1.f)));
|
2021-10-30 01:04:54 +00:00
|
|
|
}
|
2021-10-30 00:58:40 +00:00
|
|
|
|
2021-10-30 01:04:54 +00:00
|
|
|
if (shape->getShapeType() == BOX_SHAPE_PROXYTYPE)
|
|
|
|
{
|
|
|
|
const btBoxShape* boxshape = static_cast<const btBoxShape*>(shape);
|
2021-10-30 01:06:22 +00:00
|
|
|
return CollisionShapePtr(new btBoxShape(*boxshape));
|
2021-10-30 01:04:54 +00:00
|
|
|
}
|
2021-10-30 00:58:40 +00:00
|
|
|
|
|
|
|
if (shape->getShapeType() == TERRAIN_SHAPE_PROXYTYPE)
|
2021-10-30 01:06:22 +00:00
|
|
|
return CollisionShapePtr(new btHeightfieldTerrainShape(static_cast<const btHeightfieldTerrainShape&>(*shape)));
|
2021-10-30 00:58:40 +00:00
|
|
|
|
|
|
|
throw std::logic_error(std::string("Unhandled Bullet shape duplication: ") + shape->getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
void deleteShape(btCollisionShape* shape)
|
|
|
|
{
|
|
|
|
if (shape->isCompound())
|
|
|
|
{
|
|
|
|
btCompoundShape* compound = static_cast<btCompoundShape*>(shape);
|
|
|
|
for (int i = 0, n = compound->getNumChildShapes(); i < n; i++)
|
|
|
|
if (btCollisionShape* child = compound->getChildShape(i))
|
|
|
|
deleteShape(child);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete shape;
|
|
|
|
}
|
|
|
|
}
|
2015-11-16 22:30:10 +00:00
|
|
|
|
2021-10-30 01:06:22 +00:00
|
|
|
void DeleteCollisionShape::operator()(btCollisionShape* shape) const
|
2015-11-16 22:30:10 +00:00
|
|
|
{
|
2021-10-30 01:06:22 +00:00
|
|
|
deleteShape(shape);
|
2015-11-16 22:30:10 +00:00
|
|
|
}
|
|
|
|
|
2016-02-05 22:59:37 +00:00
|
|
|
BulletShape::BulletShape(const BulletShape ©, const osg::CopyOp ©op)
|
2021-10-30 01:06:22 +00:00
|
|
|
: mCollisionShape(duplicateCollisionShape(copy.mCollisionShape.get()))
|
|
|
|
, mAvoidCollisionShape(duplicateCollisionShape(copy.mAvoidCollisionShape.get()))
|
2020-11-16 16:37:30 +00:00
|
|
|
, mCollisionBox(copy.mCollisionBox)
|
2016-02-05 22:59:37 +00:00
|
|
|
, mAnimatedShapes(copy.mAnimatedShapes)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-07-12 08:44:11 +00:00
|
|
|
btCollisionShape *BulletShape::getCollisionShape() const
|
2015-11-16 22:30:10 +00:00
|
|
|
{
|
2021-10-30 01:06:22 +00:00
|
|
|
return mCollisionShape.get();
|
2015-11-16 22:30:10 +00:00
|
|
|
}
|
|
|
|
|
2018-07-12 08:44:11 +00:00
|
|
|
btCollisionShape *BulletShape::getAvoidCollisionShape() const
|
2018-07-10 22:05:19 +00:00
|
|
|
{
|
2021-10-30 01:06:22 +00:00
|
|
|
return mAvoidCollisionShape.get();
|
2018-07-10 22:05:19 +00:00
|
|
|
}
|
|
|
|
|
2018-07-12 08:44:11 +00:00
|
|
|
void BulletShape::setLocalScaling(const btVector3& scale)
|
|
|
|
{
|
|
|
|
mCollisionShape->setLocalScaling(scale);
|
|
|
|
if (mAvoidCollisionShape)
|
|
|
|
mAvoidCollisionShape->setLocalScaling(scale);
|
|
|
|
}
|
|
|
|
|
2021-02-05 13:55:42 +00:00
|
|
|
bool BulletShape::isAnimated() const
|
|
|
|
{
|
|
|
|
return !mAnimatedShapes.empty();
|
|
|
|
}
|
|
|
|
|
2016-02-06 22:30:41 +00:00
|
|
|
osg::ref_ptr<BulletShapeInstance> BulletShape::makeInstance() const
|
2015-11-16 22:30:10 +00:00
|
|
|
{
|
|
|
|
osg::ref_ptr<BulletShapeInstance> instance (new BulletShapeInstance(this));
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2016-02-06 22:30:41 +00:00
|
|
|
BulletShapeInstance::BulletShapeInstance(osg::ref_ptr<const BulletShape> source)
|
2021-10-30 01:06:22 +00:00
|
|
|
: mSource(source)
|
2015-11-16 22:30:10 +00:00
|
|
|
{
|
2020-11-16 16:37:30 +00:00
|
|
|
mCollisionBox = source->mCollisionBox;
|
2015-11-16 22:30:10 +00:00
|
|
|
mAnimatedShapes = source->mAnimatedShapes;
|
2021-10-30 01:06:22 +00:00
|
|
|
mCollisionShape = duplicateCollisionShape(source->mCollisionShape.get());
|
|
|
|
mAvoidCollisionShape = duplicateCollisionShape(source->mAvoidCollisionShape.get());
|
2015-11-16 22:30:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|