Move btCollisionObject creation for MWPhysics::Object into components

pull/3174/head
elsid 4 years ago
parent d6613d3677
commit 405e814190
No known key found for this signature in database
GPG Key ID: B845CB9FEE18AB40

@ -6,6 +6,7 @@
#include <components/resource/bulletshape.hpp>
#include <components/sceneutil/positionattitudetransform.hpp>
#include <components/misc/convert.hpp>
#include <components/bullethelpers/collisionobject.hpp>
#include <BulletCollision/CollisionShapes/btCompoundShape.h>
#include <BulletCollision/CollisionDispatch/btCollisionObject.h>
@ -15,22 +16,18 @@
namespace MWPhysics
{
Object::Object(const MWWorld::Ptr& ptr, osg::ref_ptr<Resource::BulletShapeInstance> shapeInstance, osg::Quat rotation, int collisionType, PhysicsTaskScheduler* scheduler)
: mShapeInstance(shapeInstance)
: mShapeInstance(std::move(shapeInstance))
, mSolid(true)
, mScale(ptr.getCellRef().getScale(), ptr.getCellRef().getScale(), ptr.getCellRef().getScale())
, mPosition(ptr.getRefData().getPosition().asVec3())
, mRotation(rotation)
, mTaskScheduler(scheduler)
{
mPtr = ptr;
mCollisionObject = std::make_unique<btCollisionObject>();
mCollisionObject->setCollisionShape(shapeInstance->getCollisionShape());
mCollisionObject = BulletHelpers::makeCollisionObject(mShapeInstance->getCollisionShape(),
Misc::Convert::toBullet(mPosition), Misc::Convert::toBullet(rotation));
mCollisionObject->setUserPointer(this);
setScale(ptr.getCellRef().getScale());
setRotation(rotation);
updatePosition();
commitPositionChange();
mShapeInstance->setLocalScaling(mScale);
mTaskScheduler->addCollisionObject(mCollisionObject.get(), collisionType, CollisionType_Actor|CollisionType_HeightMap|CollisionType_Projectile);
}

@ -65,23 +65,12 @@ namespace
* osg::Quat(zr, osg::Vec3(0, 0, -1));
}
osg::Quat makeObjectOsgQuat(const ESM::Position& position)
{
const float xr = position.rot[0];
const float yr = position.rot[1];
const float zr = position.rot[2];
return osg::Quat(zr, osg::Vec3(0, 0, -1))
* osg::Quat(yr, osg::Vec3(0, -1, 0))
* osg::Quat(xr, osg::Vec3(-1, 0, 0));
}
osg::Quat makeNodeRotation(const MWWorld::Ptr& ptr, RotationOrder order)
{
const auto pos = ptr.getRefData().getPosition();
const auto rot = ptr.getClass().isActor() ? makeActorOsgQuat(pos)
: (order == RotationOrder::inverse ? makeInversedOrderObjectOsgQuat(pos) : makeObjectOsgQuat(pos));
: (order == RotationOrder::inverse ? makeInversedOrderObjectOsgQuat(pos) : Misc::Convert::makeOsgQuat(pos));
return rot;
}
@ -155,7 +144,7 @@ namespace
const auto transform = object->getTransform();
const btTransform closedDoorTransform(
Misc::Convert::toBullet(makeObjectOsgQuat(ptr.getCellRef().getPosition())),
Misc::Convert::makeBulletQuaternion(ptr.getCellRef().getPosition()),
transform.getOrigin()
);

@ -0,0 +1,24 @@
#ifndef OPENMW_COMPONENTS_BULLETHELPERS_COLLISIONOBJECT_H
#define OPENMW_COMPONENTS_BULLETHELPERS_COLLISIONOBJECT_H
#include <components/misc/convert.hpp>
#include <BulletCollision/CollisionDispatch/btCollisionObject.h>
#include <LinearMath/btQuaternion.h>
#include <LinearMath/btTransform.h>
#include <memory>
namespace BulletHelpers
{
inline std::unique_ptr<btCollisionObject> makeCollisionObject(btCollisionShape* shape,
const btVector3& position, const btQuaternion& rotation)
{
std::unique_ptr<btCollisionObject> result = std::make_unique<btCollisionObject>();
result->setCollisionShape(shape);
result->setWorldTransform(btTransform(rotation, position));
return result;
}
}
#endif

@ -1,6 +1,7 @@
#ifndef OPENMW_COMPONENTS_MISC_CONVERT_H
#define OPENMW_COMPONENTS_MISC_CONVERT_H
#include <components/esm/defs.hpp>
#include <components/esm/loadpgrd.hpp>
#include <LinearMath/btTransform.h>
@ -47,6 +48,30 @@ namespace Convert
{
return osg::Quat(quat.x(), quat.y(), quat.z(), quat.w());
}
inline osg::Quat makeOsgQuat(const float (&rotation)[3])
{
return osg::Quat(rotation[2], osg::Vec3f(0, 0, -1))
* osg::Quat(rotation[1], osg::Vec3f(0, -1, 0))
* osg::Quat(rotation[0], osg::Vec3f(-1, 0, 0));
}
inline osg::Quat makeOsgQuat(const ESM::Position& position)
{
return makeOsgQuat(position.rot);
}
inline btQuaternion makeBulletQuaternion(const float (&rotation)[3])
{
return btQuaternion(btVector3(0, 0, -1), rotation[2])
* btQuaternion(btVector3(0, -1, 0), rotation[1])
* btQuaternion(btVector3(-1, 0, 0), rotation[0]);
}
inline btQuaternion makeBulletQuaternion(const ESM::Position& position)
{
return makeBulletQuaternion(position.rot);
}
}
}

Loading…
Cancel
Save