From c5b25ef70b9264c2a9036bcbb04da7fb50336b25 Mon Sep 17 00:00:00 2001 From: Jason Hooks Date: Mon, 10 Sep 2012 20:29:24 -0400 Subject: [PATCH] Very basic actor physics (no set scale/rotate functions) --- apps/openmw/mwrender/npcanimation.cpp | 1 + components/nifbullet/bullet_nif_loader.cpp | 1 + libs/openengine/bullet/physic.cpp | 46 +++++++++++----------- libs/openengine/bullet/physic.hpp | 7 ++-- 4 files changed, 28 insertions(+), 27 deletions(-) diff --git a/apps/openmw/mwrender/npcanimation.cpp b/apps/openmw/mwrender/npcanimation.cpp index e509bd170..e0304e06e 100644 --- a/apps/openmw/mwrender/npcanimation.cpp +++ b/apps/openmw/mwrender/npcanimation.cpp @@ -122,6 +122,7 @@ NpcAnimation::NpcAnimation(const MWWorld::Ptr& ptr, OEngine::Render::OgreRendere } + if(isFemale) mInsert->scale(race->data.height.female, race->data.height.female, race->data.height.female); else diff --git a/components/nifbullet/bullet_nif_loader.cpp b/components/nifbullet/bullet_nif_loader.cpp index 6fec6241f..1dc22a0d3 100644 --- a/components/nifbullet/bullet_nif_loader.cpp +++ b/components/nifbullet/bullet_nif_loader.cpp @@ -74,6 +74,7 @@ void ManualBulletShapeLoader::loadResource(Ogre::Resource *resource) cShape->collide = false; mBoundingBox = NULL; cShape->boxTranslation = Ogre::Vector3(0,0,0); + cShape->boxRotation = Ogre::Quaternion::IDENTITY; mTriMesh = new btTriangleMesh(); diff --git a/libs/openengine/bullet/physic.cpp b/libs/openengine/bullet/physic.cpp index f453915c8..8fa7681f5 100644 --- a/libs/openengine/bullet/physic.cpp +++ b/libs/openengine/bullet/physic.cpp @@ -27,13 +27,10 @@ namespace Physic }; PhysicActor::PhysicActor(std::string name, std::string mesh, PhysicEngine* engine, Ogre::Vector3 position, Ogre::Quaternion rotation, float scale): - mName(name), mEngine(engine), mMesh(mesh), mBoxTranslation(Ogre::Vector3::ZERO), mBoxRotation(Ogre::Quaternion::ZERO), mBody(0), collisionMode(false) + mName(name), mEngine(engine), mMesh(mesh), mBoxScaledTranslation(0,0,0), mBoxRotationInverse(0,0,0,0), mBody(0), collisionMode(false), mBoxRotation(0,0,0,0) { - Ogre::Vector3 test; - mBody = mEngine->createAndAdjustRigidBody(mesh, mName, scale, position, rotation, &test); - std::cout << "Test" << test << "\n"; + mBody = mEngine->createAndAdjustRigidBody(mesh, mName, scale, position, rotation, &mBoxScaledTranslation, &mBoxRotation, &mBoxRotationInverse); mEngine->addRigidBody(mBody, false); //Add rigid body to dynamics world, but do not add to object map - } PhysicActor::~PhysicActor() @@ -79,12 +76,17 @@ namespace Physic btVector3 PhysicActor::getPosition(void) { - return mBody->getWorldTransform().getOrigin();//return internalGhostObject->getWorldTransform().getOrigin() -mTranslation; + btVector3 vec = mBody->getWorldTransform().getOrigin(); + Ogre::Quaternion rotation = Ogre::Quaternion(mBody->getWorldTransform().getRotation().getW(), mBody->getWorldTransform().getRotation().getX(), + mBody->getWorldTransform().getRotation().getY(), mBody->getWorldTransform().getRotation().getZ()); + Ogre::Vector3 transrot = rotation * mBoxScaledTranslation; + btVector3 visualPosition = vec - btVector3(transrot.x, transrot.y, transrot.z); + return visualPosition; } btQuaternion PhysicActor::getRotation(void) { - return mBody->getWorldTransform().getRotation();//return btQuaternion::internalGhostObject->getWorldTransform().getRotation(); + return mBody->getWorldTransform().getRotation() * mBoxRotationInverse; } void PhysicActor::setPosition(const btVector3& pos) @@ -296,6 +298,7 @@ namespace Physic rotation = rotation * boxRotation; Ogre::Vector3 transrot = rotation * scaledBoxTranslation; Ogre::Vector3 newPosition = transrot + position; + tr.setOrigin(btVector3(newPosition.x, newPosition.y, newPosition.z)); tr.setRotation(btQuaternion(rotation.x,rotation.y,rotation.z,rotation.w)); body->setWorldTransform(tr); @@ -310,21 +313,14 @@ namespace Physic BulletShapeManager::getSingletonPtr()->load(outputstring,"General"); BulletShapePtr shape = BulletShapeManager::getSingleton().getByName(outputstring,"General"); - btBoxShape* box = dynamic_cast(shape->Shape); - if(box != NULL) - adjustRigidBody(body, position, rotation, shape->boxTranslation * scale, shape->boxRotation); - else - adjustRigidBody(body, position, rotation); + adjustRigidBody(body, position, rotation, shape->boxTranslation * scale, shape->boxRotation); } RigidBody* PhysicEngine::createAndAdjustRigidBody(std::string mesh,std::string name,float scale, Ogre::Vector3 position, Ogre::Quaternion rotation, - Ogre::Vector3* scaledBoxTranslation, Ogre::Quaternion* boxRotation) + Ogre::Vector3* scaledBoxTranslation, btQuaternion* boxRotation, btQuaternion* boxRotationInverse) { - if(scaledBoxTranslation != 0) - *scaledBoxTranslation = Ogre::Vector3(0, 5, 0); std::string sid = (boost::format("%07.3f") % scale).str(); std::string outputstring = mesh + sid; - //std::cout << "The string" << outputstring << "\n"; //get the shape from the .nif mShapeLoader->load(outputstring,"General"); @@ -332,9 +328,6 @@ namespace Physic BulletShapePtr shape = BulletShapeManager::getSingleton().getByName(outputstring,"General"); shape->Shape->setLocalScaling( btVector3(scale,scale,scale)); - - - // //create the motionState CMotionState* newMotionState = new CMotionState(this,name); @@ -344,11 +337,16 @@ namespace Physic RigidBody* body = new RigidBody(CI,name); body->collide = shape->collide; - btBoxShape* box = dynamic_cast(shape->Shape); - if(box != NULL) - adjustRigidBody(body, position, rotation, shape->boxTranslation * scale, shape->boxRotation); - else - adjustRigidBody(body, position, rotation); + if(scaledBoxTranslation != 0) + *scaledBoxTranslation = shape->boxTranslation * scale; + if(boxRotation != 0) + *boxRotation = btQuaternion(shape->boxRotation.x, shape->boxRotation.y, shape->boxRotation.z,shape->boxRotation.w); + if(boxRotationInverse != 0){ + Ogre::Quaternion inverse = shape->boxRotation.Inverse(); + *boxRotationInverse = btQuaternion(inverse.x,inverse.y,inverse.z,inverse.w); + } + + adjustRigidBody(body, position, rotation, shape->boxTranslation * scale, shape->boxRotation); return body; diff --git a/libs/openengine/bullet/physic.hpp b/libs/openengine/bullet/physic.hpp index f699b7d44..1ff706146 100644 --- a/libs/openengine/bullet/physic.hpp +++ b/libs/openengine/bullet/physic.hpp @@ -90,8 +90,9 @@ namespace Physic private: OEngine::Physic::RigidBody* mBody; - Ogre::Vector3 mBoxTranslation; - Ogre::Quaternion mBoxRotation; + Ogre::Vector3 mBoxScaledTranslation; + btQuaternion mBoxRotationInverse; + btQuaternion mBoxRotation; bool collisionMode; std::string mMesh; PhysicEngine* mEngine; @@ -143,7 +144,7 @@ namespace Physic * After created, the body is set to the correct rotation, position, and scale */ RigidBody* createAndAdjustRigidBody(std::string mesh,std::string name,float scale, Ogre::Vector3 position, Ogre::Quaternion rotation, - Ogre::Vector3* scaledBoxTranslation = 0, Ogre::Quaternion* boxRotation = 0); + Ogre::Vector3* scaledBoxTranslation = 0, btQuaternion* boxRotation = 0, btQuaternion* boxRotationInverse = 0); /** * Adjusts a rigid body to the right position and rotation