From 49b1d5e1275bfebcd508afb910d4d55644184c20 Mon Sep 17 00:00:00 2001 From: greye Date: Wed, 25 Jul 2012 18:58:55 +0400 Subject: [PATCH] fix object placing --- apps/openmw/mwworld/physicssystem.cpp | 8 +++++--- apps/openmw/mwworld/physicssystem.hpp | 2 +- apps/openmw/mwworld/scene.cpp | 7 ++++++- libs/openengine/bullet/physic.cpp | 17 +++++++++++++---- libs/openengine/bullet/physic.hpp | 2 +- 5 files changed, 26 insertions(+), 10 deletions(-) diff --git a/apps/openmw/mwworld/physicssystem.cpp b/apps/openmw/mwworld/physicssystem.cpp index 2b3d4b0f44..e4a019b697 100644 --- a/apps/openmw/mwworld/physicssystem.cpp +++ b/apps/openmw/mwworld/physicssystem.cpp @@ -366,13 +366,15 @@ namespace MWWorld addActor (node->getName(), model, node->getPosition()); } - float PhysicsSystem::getObjectHeight(const MWWorld::Ptr &ptr) + bool PhysicsSystem::getObjectAABB(const MWWorld::Ptr &ptr, float *min, float *max) { std::string model = MWWorld::Class::get(ptr).getModel(ptr); if (model.empty()) { - return 0.0; + return false; } float scale = ptr.getRefData().getBaseNode()->getScale().x; - return mEngine->getObjectHeight(model, scale); + mEngine->getObjectAABB(model, scale, min, max); + + return true; } } diff --git a/apps/openmw/mwworld/physicssystem.hpp b/apps/openmw/mwworld/physicssystem.hpp index 61e2590bd1..f81bcc3736 100644 --- a/apps/openmw/mwworld/physicssystem.hpp +++ b/apps/openmw/mwworld/physicssystem.hpp @@ -65,7 +65,7 @@ namespace MWWorld void setCurrentWater(bool hasWater, int waterHeight); - float getObjectHeight(const MWWorld::Ptr &ptr); + bool getObjectAABB(const MWWorld::Ptr &ptr, float *min, float *max); private: OEngine::Render::OgreRenderer &mRender; diff --git a/apps/openmw/mwworld/scene.cpp b/apps/openmw/mwworld/scene.cpp index b78669ef5b..ce0acfe47b 100644 --- a/apps/openmw/mwworld/scene.cpp +++ b/apps/openmw/mwworld/scene.cpp @@ -337,7 +337,12 @@ namespace MWWorld mRendering.addObject (ptr); float *pos = ptr.getRefData().getPosition().pos; - pos[2] += mPhysics->getObjectHeight(ptr) / 2; + float min[3], max[3]; + if (mPhysics->getObjectAABB(ptr, min, max)) { + pos[0] -= (min[0] + max[0]) / 2; + pos[1] -= (min[1] + max[1]) / 2; + pos[2] -= min[2]; + } ptr.getRefData().getBaseNode()->setPosition(pos[0], pos[1], pos[2]); diff --git a/libs/openengine/bullet/physic.cpp b/libs/openengine/bullet/physic.cpp index 416d0c09df..611c87d9f5 100644 --- a/libs/openengine/bullet/physic.cpp +++ b/libs/openengine/bullet/physic.cpp @@ -12,6 +12,8 @@ #include +#include + #define BIT(x) (1<<(x)) namespace OEngine { @@ -569,7 +571,8 @@ namespace Physic return results2; } - float PhysicEngine::getObjectHeight(const std::string &mesh, float scale) { + void PhysicEngine::getObjectAABB(const std::string &mesh, float scale, float *min, float *max) + { char uniqueID[8]; sprintf( uniqueID, "%07.3f", scale ); std::string sid = uniqueID; @@ -582,11 +585,17 @@ namespace Physic btTransform trans; - btVector3 min, max; + btVector3 btmin, btmax; trans.setIdentity(); - shape->Shape->getAabb(trans, min, max); + shape->Shape->getAabb(trans, btmin, btmax); - return max.z() - min.z(); + min[0] = btmin.x(); + min[1] = btmin.y(); + min[2] = btmin.z(); + + max[0] = btmax.x(); + max[1] = btmax.y(); + max[2] = btmax.z(); } }}; diff --git a/libs/openengine/bullet/physic.hpp b/libs/openengine/bullet/physic.hpp index 22c5fa2748..8701adc849 100644 --- a/libs/openengine/bullet/physic.hpp +++ b/libs/openengine/bullet/physic.hpp @@ -221,7 +221,7 @@ namespace Physic bool toggleDebugRendering(); - float getObjectHeight(const std::string &mesh, float scale); + void getObjectAABB(const std::string &mesh, float scale, float *min, float *max); /** * Return the closest object hit by a ray. If there are no objects, it will return ("",-1).