From 1f5bc229e0ef5c7e5d72b1f721bc0eb7c5e90e75 Mon Sep 17 00:00:00 2001 From: Jason Hooks Date: Tue, 14 Aug 2012 18:04:58 -0400 Subject: [PATCH] Separate adjustRigidBody function --- apps/openmw/mwworld/physicssystem.cpp | 24 ++++++++++--------- apps/openmw/mwworld/physicssystem.hpp | 6 ++--- apps/openmw/mwworld/worldimp.cpp | 6 ++--- components/nifbullet/bullet_nif_loader.cpp | 2 +- libs/openengine/bullet/physic.cpp | 28 +++++++++++++--------- libs/openengine/bullet/physic.hpp | 2 ++ libs/openengine/bullet/trace.cpp | 5 +++- 7 files changed, 43 insertions(+), 30 deletions(-) diff --git a/apps/openmw/mwworld/physicssystem.cpp b/apps/openmw/mwworld/physicssystem.cpp index 3bdf76bcd5..faacc5b060 100644 --- a/apps/openmw/mwworld/physicssystem.cpp +++ b/apps/openmw/mwworld/physicssystem.cpp @@ -193,8 +193,8 @@ namespace MWWorld Ogre::Quaternion pitchQuat = pitchNode->getOrientation(); + //playerphysics->ps.snappingImplemented = false; - playerphysics->ps.viewangles.x = pitchQuat.getPitch().valueDegrees(); @@ -280,19 +280,20 @@ namespace MWWorld mEngine->deleteRigidBody(handle); } - void PhysicsSystem::moveObject (const std::string& handle, const Ogre::Vector3& position) + void PhysicsSystem::moveObject (const std::string& handle, Ogre::SceneNode* node) { if (OEngine::Physic::RigidBody* body = mEngine->getRigidBody(handle)) { // TODO very dirty hack to avoid crash during setup -> needs cleaning up to allow // start positions others than 0, 0, 0 btTransform tr = body->getWorldTransform(); + Ogre::Vector3 position = node->getPosition(); tr.setOrigin(btVector3(position.x,position.y,position.z)); body->setWorldTransform(tr); } if (OEngine::Physic::PhysicActor* act = mEngine->getCharacter(handle)) { - // TODO very dirty hack to avoid crash during setup -> needs cleaning up to allow + /*// TODO very dirty hack to avoid crash during setup -> needs cleaning up to allow // start positions others than 0, 0, 0 if (handle == "player") { @@ -301,31 +302,32 @@ namespace MWWorld else { act->setPosition(btVector3(position.x,position.y,position.z)); - } + }*/ } } - void PhysicsSystem::rotateObject (const std::string& handle, const Ogre::Quaternion& rotation) + void PhysicsSystem::rotateObject (const std::string& handle, Ogre::SceneNode* node) { - if (OEngine::Physic::PhysicActor* act = mEngine->getCharacter(handle)) + /*if (OEngine::Physic::PhysicActor* act = mEngine->getCharacter(handle)) { act->setRotation(btQuaternion(rotation.x, rotation.y, rotation.z, rotation.w)); - } + }*/ if (OEngine::Physic::RigidBody* body = mEngine->getRigidBody(handle)) { + Ogre::Quaternion rotation = node->getOrientation(); body->getWorldTransform().setRotation(btQuaternion(rotation.x, rotation.y, rotation.z, rotation.w)); } } - void PhysicsSystem::scaleObject (const std::string& handle, float scale) + void PhysicsSystem::scaleObject (const std::string& handle, Ogre::SceneNode* node) { if(handleToMesh.find(handle) != handleToMesh.end()) { - btTransform transform = mEngine->getRigidBody(handle)->getWorldTransform(); removeObject(handle); - Ogre::Quaternion quat = Ogre::Quaternion(transform.getRotation().getW(), transform.getRotation().getX(), transform.getRotation().getY(), transform.getRotation().getZ()); - Ogre::Vector3 vec = Ogre::Vector3(transform.getOrigin().getX(), transform.getOrigin().getY(), transform.getOrigin().getZ()); + float scale = node->getScale().x; + Ogre::Quaternion quat = node->getOrientation(); + Ogre::Vector3 vec = node->getPosition(); addObject(handle, handleToMesh[handle], quat, scale, vec); } } diff --git a/apps/openmw/mwworld/physicssystem.hpp b/apps/openmw/mwworld/physicssystem.hpp index e42fa536b9..4ed8d59f60 100644 --- a/apps/openmw/mwworld/physicssystem.hpp +++ b/apps/openmw/mwworld/physicssystem.hpp @@ -34,11 +34,11 @@ namespace MWWorld void removeObject (const std::string& handle); - void moveObject (const std::string& handle, const Ogre::Vector3& position); + void moveObject (const std::string& handle, Ogre::SceneNode* node); - void rotateObject (const std::string& handle, const Ogre::Quaternion& rotation); + void rotateObject (const std::string& handle, Ogre::SceneNode* node); - void scaleObject (const std::string& handle, float scale); + void scaleObject (const std::string& handle, Ogre::SceneNode* node); bool toggleCollisionMode(); diff --git a/apps/openmw/mwworld/worldimp.cpp b/apps/openmw/mwworld/worldimp.cpp index 8fb7b1ba7d..d5046db2d4 100644 --- a/apps/openmw/mwworld/worldimp.cpp +++ b/apps/openmw/mwworld/worldimp.cpp @@ -585,7 +585,7 @@ namespace MWWorld { moveObjectImp(ptr, x, y, z); - mPhysics->moveObject (ptr.getRefData().getHandle(), Ogre::Vector3 (x, y, z)); + mPhysics->moveObject (ptr.getRefData().getHandle(), ptr.getRefData().getBaseNode()); } void World::scaleObject (const Ptr& ptr, float scale) @@ -595,7 +595,7 @@ namespace MWWorld ptr.getCellRef().scale = scale; //scale = scale/ptr.getRefData().getBaseNode()->getScale().x; ptr.getRefData().getBaseNode()->setScale(scale,scale,scale); - mPhysics->scaleObject( ptr.getRefData().getHandle(), scale ); + mPhysics->scaleObject( ptr.getRefData().getHandle(), ptr.getRefData().getBaseNode()); } void World::rotateObject (const Ptr& ptr,float x,float y,float z) @@ -610,7 +610,7 @@ namespace MWWorld Ogre::Quaternion roty(Ogre::Degree(-y),Ogre::Vector3::UNIT_Y); Ogre::Quaternion rotz(Ogre::Degree(-z),Ogre::Vector3::UNIT_Z); ptr.getRefData().getBaseNode()->setOrientation(rotx*roty*rotz); - mPhysics->rotateObject(ptr.getRefData().getHandle(),ptr.getRefData().getBaseNode()->getOrientation()); + mPhysics->rotateObject(ptr.getRefData().getHandle(),ptr.getRefData().getBaseNode()); } void World::indexToPosition (int cellX, int cellY, float &x, float &y, bool centre) const diff --git a/components/nifbullet/bullet_nif_loader.cpp b/components/nifbullet/bullet_nif_loader.cpp index 7af56d04c3..fc7fb5ec26 100644 --- a/components/nifbullet/bullet_nif_loader.cpp +++ b/components/nifbullet/bullet_nif_loader.cpp @@ -129,7 +129,7 @@ void ManualBulletShapeLoader::loadResource(Ogre::Resource *resource) }; cShape->boxTranslation = boxTranslation; if(mBoundingBox != NULL) - cShape->Shape = mBoundingBox; + cShape->Shape = mBoundingBox; else { diff --git a/libs/openengine/bullet/physic.cpp b/libs/openengine/bullet/physic.cpp index 9d0a290e9e..af73a42a6a 100644 --- a/libs/openengine/bullet/physic.cpp +++ b/libs/openengine/bullet/physic.cpp @@ -332,6 +332,20 @@ namespace Physic mHeightFieldMap.erase(name); } + void PhysicEngine::adjustRigidBody(BulletShapePtr shape, RigidBody* body, float scale, Ogre::Vector3 position, Ogre::Quaternion rotation){ + btTransform tr; + btBoxShape* box = dynamic_cast(body->getCollisionShape()); + if(box != NULL){ + Ogre::Vector3 transrot = rotation * (shape->boxTranslation * scale); + Ogre::Vector3 newPosition = transrot + position; + tr.setOrigin(btVector3(newPosition.x, newPosition.y, newPosition.z)); + } + else + tr.setOrigin(btVector3(position.x,position.y,position.z)); + tr.setRotation(btQuaternion(rotation.x,rotation.y,rotation.z,rotation.w)); + body->setWorldTransform(tr); + } + RigidBody* PhysicEngine::createAndAdjustRigidBody(std::string mesh,std::string name,float scale, Ogre::Vector3 position, Ogre::Quaternion rotation) { std::string sid = (boost::format("%07.3f") % scale).str(); @@ -355,17 +369,9 @@ namespace Physic RigidBody* body = new RigidBody(CI,name); body->collide = shape->collide; - btTransform tr; - btBoxShape* box = dynamic_cast(body->getCollisionShape()); - if(box != NULL){ - Ogre::Vector3 transrot = rotation * (shape->boxTranslation * scale); - Ogre::Vector3 newPosition = transrot + position; - tr.setOrigin(btVector3(newPosition.x, newPosition.y, newPosition.z)); - } - else - tr.setOrigin(btVector3(position.x,position.y,position.z)); - tr.setRotation(btQuaternion(rotation.x,rotation.y,rotation.z,rotation.w)); - body->setWorldTransform(tr); + //Pass in BulletShape, RigidBody, scale, position, rotation + + adjustRigidBody(shape, body, scale, position, rotation); return body; diff --git a/libs/openengine/bullet/physic.hpp b/libs/openengine/bullet/physic.hpp index 42f6487662..fc52ad127a 100644 --- a/libs/openengine/bullet/physic.hpp +++ b/libs/openengine/bullet/physic.hpp @@ -148,6 +148,8 @@ namespace Physic */ RigidBody* createAndAdjustRigidBody(std::string mesh,std::string name,float scale, Ogre::Vector3 position, Ogre::Quaternion rotation); + void adjustRigidBody(BulletShapePtr shape, RigidBody* body, float scale, Ogre::Vector3 position, Ogre::Quaternion rotation); + /** * Add a HeightField to the simulation */ diff --git a/libs/openengine/bullet/trace.cpp b/libs/openengine/bullet/trace.cpp index 6d2166baac..2329ed652c 100644 --- a/libs/openengine/bullet/trace.cpp +++ b/libs/openengine/bullet/trace.cpp @@ -21,6 +21,7 @@ void newtrace(traceResults* const results, const Ogre::Vector3& start, const Ogr //if (!traceobj->incellptr) // return; + const Ogre::Vector3 rayDir = end - start; @@ -34,6 +35,7 @@ void newtrace(traceResults* const results, const Ogre::Vector3& start, const Ogr //Ogre::Vector3 startReplace = Ogre::Vector3(650,950, 45); //Ogre::Vector3 endReplace = startReplace; //endReplace.z -= .25; + const bool hasHit = NewPhysicsTrace(&out, start, end, BBHalfExtents, Ogre::Vector3(0.0f, 0.0f,0.0f), isInterior, enginePass); @@ -104,7 +106,8 @@ const bool NewPhysicsTrace(NewPhysTraceResults* const out, const Ogre::Vector3& const btVector3 btend(end.x, end.y, end.z); const btQuaternion btrot(rotation.y, rotation.x, rotation.z); //y, x, z - const btBoxShape newshape(btVector3(BBHalfExtents.x, BBHalfExtents.y, BBHalfExtents.z)); + const btBoxShape newshape(btVector3(BBHalfExtents.x, BBHalfExtents.y, BBHalfExtents.z)); + //const btCapsuleShapeZ newshape(BBHalfExtents.x, BBHalfExtents.z * 2 - BBHalfExtents.x * 2); const btTransform from(btrot, btstart); const btTransform to(btrot, btend);