Generate physics collisions for projectiles (bug #3372)
Remove redundant now mHit fieldpull/593/head
parent
c2933721c6
commit
dc7b48e92e
@ -0,0 +1,64 @@
|
||||
#include "projectile.hpp"
|
||||
|
||||
#include <BulletCollision/CollisionShapes/btSphereShape.h>
|
||||
#include <BulletCollision/CollisionDispatch/btCollisionWorld.h>
|
||||
|
||||
#include <components/sceneutil/positionattitudetransform.hpp>
|
||||
#include <components/resource/bulletshape.hpp>
|
||||
#include <components/debug/debuglog.hpp>
|
||||
#include <components/misc/convert.hpp>
|
||||
|
||||
#include "../mwworld/class.hpp"
|
||||
|
||||
#include "collisiontype.hpp"
|
||||
|
||||
namespace MWPhysics
|
||||
{
|
||||
Projectile::Projectile(int projectileId, const osg::Vec3f& position, btCollisionWorld* world)
|
||||
: mCollisionWorld(world)
|
||||
{
|
||||
mProjectileId = projectileId;
|
||||
|
||||
mShape.reset(new btSphereShape(1.f));
|
||||
mConvexShape = static_cast<btConvexShape*>(mShape.get());
|
||||
|
||||
mCollisionObject.reset(new btCollisionObject);
|
||||
mCollisionObject->setCollisionFlags(btCollisionObject::CF_KINEMATIC_OBJECT);
|
||||
mCollisionObject->setActivationState(DISABLE_DEACTIVATION);
|
||||
mCollisionObject->setCollisionShape(mShape.get());
|
||||
mCollisionObject->setUserPointer(this);
|
||||
|
||||
setPosition(position);
|
||||
|
||||
const int collisionMask = CollisionType_World | CollisionType_HeightMap |
|
||||
CollisionType_Actor | CollisionType_Door | CollisionType_Water;
|
||||
mCollisionWorld->addCollisionObject(mCollisionObject.get(), CollisionType_Projectile, collisionMask);
|
||||
}
|
||||
|
||||
Projectile::~Projectile()
|
||||
{
|
||||
if (mCollisionObject.get())
|
||||
mCollisionWorld->removeCollisionObject(mCollisionObject.get());
|
||||
}
|
||||
|
||||
void Projectile::updateCollisionObjectPosition()
|
||||
{
|
||||
btTransform tr = mCollisionObject->getWorldTransform();
|
||||
// osg::Vec3f scaledTranslation = mRotation * mMeshTranslation;
|
||||
// osg::Vec3f newPosition = scaledTranslation + mPosition;
|
||||
tr.setOrigin(Misc::Convert::toBullet(mPosition));
|
||||
mCollisionObject->setWorldTransform(tr);
|
||||
}
|
||||
|
||||
void Projectile::setPosition(const osg::Vec3f &position)
|
||||
{
|
||||
mPosition = position;
|
||||
updateCollisionObjectPosition();
|
||||
}
|
||||
|
||||
osg::Vec3f Projectile::getPosition() const
|
||||
{
|
||||
return mPosition;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
#ifndef OPENMW_MWPHYSICS_PROJECTILE_H
|
||||
#define OPENMW_MWPHYSICS_PROJECTILE_H
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "ptrholder.hpp"
|
||||
|
||||
#include <osg/Vec3f>
|
||||
#include <osg/Quat>
|
||||
#include <osg/ref_ptr>
|
||||
|
||||
class btCollisionWorld;
|
||||
class btCollisionShape;
|
||||
class btCollisionObject;
|
||||
class btConvexShape;
|
||||
|
||||
namespace Resource
|
||||
{
|
||||
class BulletShape;
|
||||
}
|
||||
|
||||
namespace MWPhysics
|
||||
{
|
||||
class Projectile : public PtrHolder
|
||||
{
|
||||
public:
|
||||
Projectile(const int projectileId, const osg::Vec3f& position, btCollisionWorld* world);
|
||||
~Projectile();
|
||||
|
||||
btConvexShape* getConvexShape() const { return mConvexShape; }
|
||||
|
||||
void updateCollisionObjectPosition();
|
||||
|
||||
void setPosition(const osg::Vec3f& position);
|
||||
|
||||
osg::Vec3f getPosition() const;
|
||||
|
||||
btCollisionObject* getCollisionObject() const
|
||||
{
|
||||
return mCollisionObject.get();
|
||||
}
|
||||
|
||||
int getProjectileId() const
|
||||
{
|
||||
return mProjectileId;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
std::unique_ptr<btCollisionShape> mShape;
|
||||
btConvexShape* mConvexShape;
|
||||
|
||||
std::unique_ptr<btCollisionObject> mCollisionObject;
|
||||
|
||||
osg::Vec3f mPosition;
|
||||
|
||||
btCollisionWorld* mCollisionWorld;
|
||||
|
||||
Projectile(const Projectile&);
|
||||
Projectile& operator=(const Projectile&);
|
||||
|
||||
int mProjectileId;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue