1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-02-19 12:09:42 +00:00

Merge branch 'justbenormal' into 'master'

Unify collision shape transformations

See merge request OpenMW/openmw!2808
This commit is contained in:
psi29a 2023-03-10 07:51:01 +00:00
commit ff7a098e74
3 changed files with 26 additions and 47 deletions

View file

@ -270,7 +270,7 @@ namespace
dst.pos = osg::Vec3f(src.getOrigin().x(), src.getOrigin().y(), src.getOrigin().z()); dst.pos = osg::Vec3f(src.getOrigin().x(), src.getOrigin().y(), src.getOrigin().z());
for (int row = 0; row < 3; ++row) for (int row = 0; row < 3; ++row)
for (int column = 0; column < 3; ++column) for (int column = 0; column < 3; ++column)
dst.rotation.mValues[column][row] = src.getBasis().getRow(row)[column]; dst.rotation.mValues[row][column] = src.getBasis().getRow(row)[column];
} }
struct TestBulletNifLoader : Test struct TestBulletNifLoader : Test
@ -292,12 +292,9 @@ namespace
Nif::NiStringExtraData mNiStringExtraData2; Nif::NiStringExtraData mNiStringExtraData2;
Nif::Controller mController; Nif::Controller mController;
btTransform mTransform{ btMatrix3x3(btQuaternion(btVector3(1, 0, 0), 0.5f)), btVector3(1, 2, 3) }; btTransform mTransform{ btMatrix3x3(btQuaternion(btVector3(1, 0, 0), 0.5f)), btVector3(1, 2, 3) };
btTransform mResultTransform{ btMatrix3x3(1, 0, 0, 0, 0.82417738437652587890625, 0.56633174419403076171875, 0, btTransform mTransformScale2{ btMatrix3x3(btQuaternion(btVector3(1, 0, 0), 0.5f)), btVector3(2, 4, 6) };
-0.56633174419403076171875, 0.82417738437652587890625), btTransform mTransformScale3{ btMatrix3x3(btQuaternion(btVector3(1, 0, 0), 0.5f)), btVector3(3, 6, 9) };
btVector3(1, 2, 3) }; btTransform mTransformScale4{ btMatrix3x3(btQuaternion(btVector3(1, 0, 0), 0.5f)), btVector3(4, 8, 12) };
btTransform mResultTransform2{ btMatrix3x3(1, 0, 0, 0, 0.7951543331146240234375, 0.606407105922698974609375, 0,
-0.606407105922698974609375, 0.7951543331146240234375),
btVector3(4, 8, 12) };
const std::string mHash = "hash"; const std::string mHash = "hash";
TestBulletNifLoader() TestBulletNifLoader()
@ -746,7 +743,7 @@ namespace
std::unique_ptr<Resource::TriangleMeshShape> mesh(new Resource::TriangleMeshShape(triangles.release(), true)); std::unique_ptr<Resource::TriangleMeshShape> mesh(new Resource::TriangleMeshShape(triangles.release(), true));
mesh->setLocalScaling(btVector3(3, 3, 3)); mesh->setLocalScaling(btVector3(3, 3, 3));
std::unique_ptr<btCompoundShape> shape(new btCompoundShape); std::unique_ptr<btCompoundShape> shape(new btCompoundShape);
shape->addChildShape(mResultTransform, mesh.release()); shape->addChildShape(mTransform, mesh.release());
Resource::BulletShape expected; Resource::BulletShape expected;
expected.mCollisionShape.reset(shape.release()); expected.mCollisionShape.reset(shape.release());
expected.mAnimatedShapes = { { -1, 0 } }; expected.mAnimatedShapes = { { -1, 0 } };
@ -773,7 +770,7 @@ namespace
std::unique_ptr<Resource::TriangleMeshShape> mesh(new Resource::TriangleMeshShape(triangles.release(), true)); std::unique_ptr<Resource::TriangleMeshShape> mesh(new Resource::TriangleMeshShape(triangles.release(), true));
mesh->setLocalScaling(btVector3(12, 12, 12)); mesh->setLocalScaling(btVector3(12, 12, 12));
std::unique_ptr<btCompoundShape> shape(new btCompoundShape); std::unique_ptr<btCompoundShape> shape(new btCompoundShape);
shape->addChildShape(mResultTransform2, mesh.release()); shape->addChildShape(mTransformScale4, mesh.release());
Resource::BulletShape expected; Resource::BulletShape expected;
expected.mCollisionShape.reset(shape.release()); expected.mCollisionShape.reset(shape.release());
expected.mAnimatedShapes = { { -1, 0 } }; expected.mAnimatedShapes = { { -1, 0 } };
@ -814,8 +811,8 @@ namespace
mesh2->setLocalScaling(btVector3(3, 3, 3)); mesh2->setLocalScaling(btVector3(3, 3, 3));
std::unique_ptr<btCompoundShape> shape(new btCompoundShape); std::unique_ptr<btCompoundShape> shape(new btCompoundShape);
shape->addChildShape(mResultTransform, mesh.release()); shape->addChildShape(mTransform, mesh.release());
shape->addChildShape(mResultTransform, mesh2.release()); shape->addChildShape(mTransform, mesh2.release());
Resource::BulletShape expected; Resource::BulletShape expected;
expected.mCollisionShape.reset(shape.release()); expected.mCollisionShape.reset(shape.release());
expected.mAnimatedShapes = { { -1, 0 } }; expected.mAnimatedShapes = { { -1, 0 } };
@ -845,7 +842,7 @@ namespace
std::unique_ptr<Resource::TriangleMeshShape> mesh(new Resource::TriangleMeshShape(triangles.release(), true)); std::unique_ptr<Resource::TriangleMeshShape> mesh(new Resource::TriangleMeshShape(triangles.release(), true));
mesh->setLocalScaling(btVector3(12, 12, 12)); mesh->setLocalScaling(btVector3(12, 12, 12));
std::unique_ptr<btCompoundShape> shape(new btCompoundShape); std::unique_ptr<btCompoundShape> shape(new btCompoundShape);
shape->addChildShape(mResultTransform2, mesh.release()); shape->addChildShape(mTransformScale4, mesh.release());
Resource::BulletShape expected; Resource::BulletShape expected;
expected.mCollisionShape.reset(shape.release()); expected.mCollisionShape.reset(shape.release());
expected.mAnimatedShapes = { { -1, 0 } }; expected.mAnimatedShapes = { { -1, 0 } };
@ -887,8 +884,8 @@ namespace
mesh2->setLocalScaling(btVector3(12, 12, 12)); mesh2->setLocalScaling(btVector3(12, 12, 12));
std::unique_ptr<btCompoundShape> shape(new btCompoundShape); std::unique_ptr<btCompoundShape> shape(new btCompoundShape);
shape->addChildShape(mResultTransform2, mesh.release()); shape->addChildShape(mTransformScale4, mesh.release());
shape->addChildShape(mResultTransform2, mesh2.release()); shape->addChildShape(mTransformScale4, mesh2.release());
Resource::BulletShape expected; Resource::BulletShape expected;
expected.mCollisionShape.reset(shape.release()); expected.mCollisionShape.reset(shape.release());
expected.mAnimatedShapes = { { -1, 1 } }; expected.mAnimatedShapes = { { -1, 1 } };
@ -1348,18 +1345,8 @@ namespace
std::unique_ptr<Resource::TriangleMeshShape> mesh2(new Resource::TriangleMeshShape(triangles2.release(), true)); std::unique_ptr<Resource::TriangleMeshShape> mesh2(new Resource::TriangleMeshShape(triangles2.release(), true));
mesh2->setLocalScaling(btVector3(12, 12, 12)); mesh2->setLocalScaling(btVector3(12, 12, 12));
std::unique_ptr<btCompoundShape> shape(new btCompoundShape); std::unique_ptr<btCompoundShape> shape(new btCompoundShape);
const btTransform transform1{ shape->addChildShape(mTransformScale2, mesh1.release());
btMatrix3x3(1, 0, 0, 0, 0.8004512795493964327775415767973754555, 0.59939782204119995689950428641168400645, shape->addChildShape(mTransformScale3, mesh2.release());
0, -0.59939782204119995689950428641168400645, 0.8004512795493964327775415767973754555),
btVector3(2, 4, 6)
};
const btTransform transform2{
btMatrix3x3(1, 0, 0, 0, 0.79515431915808965079861536651151254773, 0.60640713116208888600056070572463795543,
0, -0.60640713116208888600056070572463795543, 0.79515431915808965079861536651151254773),
btVector3(3, 6, 9)
};
shape->addChildShape(transform1, mesh1.release());
shape->addChildShape(transform2, mesh2.release());
Resource::BulletShape expected; Resource::BulletShape expected;
expected.mCollisionShape.reset(shape.release()); expected.mCollisionShape.reset(shape.release());
expected.mAnimatedShapes = { { -1, 0 } }; expected.mAnimatedShapes = { { -1, 0 } };

View file

@ -25,14 +25,6 @@
namespace namespace
{ {
osg::Matrixf getWorldTransform(const Nif::Node& node, const Nif::Parent* nodeParent)
{
osg::Matrixf result = node.trafo.toMatrix();
for (const Nif::Parent* parent = nodeParent; parent != nullptr; parent = parent->mParent)
result *= parent->mNiNode.trafo.toMatrix();
return result;
}
bool pathFileNameStartsWithX(const std::string& path) bool pathFileNameStartsWithX(const std::string& path)
{ {
const std::size_t slashpos = path.find_last_of("/\\"); const std::size_t slashpos = path.find_last_of("/\\");
@ -345,8 +337,7 @@ namespace NifBullet
&& (node.recType == Nif::RC_NiTriShape || node.recType == Nif::RC_NiTriStrips && (node.recType == Nif::RC_NiTriShape || node.recType == Nif::RC_NiTriStrips
|| node.recType == Nif::RC_BSLODTriShape)) || node.recType == Nif::RC_BSLODTriShape))
{ {
handleNiTriShape(static_cast<const Nif::NiGeometry&>(node), parent, getWorldTransform(node, parent), handleNiTriShape(static_cast<const Nif::NiGeometry&>(node), parent, isAnimated, avoid);
isAnimated, avoid);
} }
} }
@ -367,8 +358,8 @@ namespace NifBullet
} }
} }
void BulletNifLoader::handleNiTriShape(const Nif::NiGeometry& niGeometry, const Nif::Parent* nodeParent, void BulletNifLoader::handleNiTriShape(
const osg::Matrixf& transform, bool isAnimated, bool avoid) const Nif::NiGeometry& niGeometry, const Nif::Parent* nodeParent, bool isAnimated, bool avoid)
{ {
if (niGeometry.data.empty() || niGeometry.data->vertices.empty()) if (niGeometry.data.empty() || niGeometry.data->vertices.empty())
return; return;
@ -383,14 +374,17 @@ namespace NifBullet
auto childShape = std::make_unique<Resource::TriangleMeshShape>(childMesh.get(), true); auto childShape = std::make_unique<Resource::TriangleMeshShape>(childMesh.get(), true);
std::ignore = childMesh.release(); std::ignore = childMesh.release();
float scale = niGeometry.trafo.scale; osg::Matrixf transform = niGeometry.trafo.toMatrix();
for (const Nif::Parent* parent = nodeParent; parent != nullptr; parent = parent->mParent) for (const Nif::Parent* parent = nodeParent; parent != nullptr; parent = parent->mParent)
scale *= parent->mNiNode.trafo.scale; transform *= parent->mNiNode.trafo.toMatrix();
osg::Quat q = transform.getRotate(); childShape->setLocalScaling(Misc::Convert::toBullet(transform.getScale()));
osg::Vec3f v = transform.getTrans(); transform.orthoNormalize(transform);
childShape->setLocalScaling(btVector3(scale, scale, scale));
btTransform trans(btQuaternion(q.x(), q.y(), q.z(), q.w()), btVector3(v.x(), v.y(), v.z())); btTransform trans;
trans.setOrigin(Misc::Convert::toBullet(transform.getTrans()));
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j)
trans.getBasis()[i][j] = transform(j, i);
if (!avoid) if (!avoid)
{ {

View file

@ -7,7 +7,6 @@
#include <string> #include <string>
#include <osg/BoundingBox> #include <osg/BoundingBox>
#include <osg/Matrixf>
#include <osg/Referenced> #include <osg/Referenced>
#include <osg/ref_ptr> #include <osg/ref_ptr>
@ -60,8 +59,7 @@ namespace NifBullet
bool hasRootCollisionNode(const Nif::Node& rootNode) const; bool hasRootCollisionNode(const Nif::Node& rootNode) const;
bool collisionShapeIsEmpty(const Nif::Node& rootNode) const; bool collisionShapeIsEmpty(const Nif::Node& rootNode) const;
void handleNiTriShape(const Nif::NiGeometry& nifNode, const Nif::Parent* parent, const osg::Matrixf& transform, void handleNiTriShape(const Nif::NiGeometry& nifNode, const Nif::Parent* parent, bool isAnimated, bool avoid);
bool isAnimated, bool avoid);
std::unique_ptr<btCompoundShape, Resource::DeleteCollisionShape> mCompoundShape; std::unique_ptr<btCompoundShape, Resource::DeleteCollisionShape> mCompoundShape;
std::unique_ptr<btCompoundShape, Resource::DeleteCollisionShape> mAvoidCompoundShape; std::unique_ptr<btCompoundShape, Resource::DeleteCollisionShape> mAvoidCompoundShape;