1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-16 20:19:57 +00:00

Separate adjustRigidBody function

This commit is contained in:
Jason Hooks 2012-08-14 18:04:58 -04:00
parent e7329d5f8b
commit 1f5bc229e0
7 changed files with 43 additions and 30 deletions

View file

@ -193,8 +193,8 @@ namespace MWWorld
Ogre::Quaternion pitchQuat = pitchNode->getOrientation(); Ogre::Quaternion pitchQuat = pitchNode->getOrientation();
//playerphysics->ps.snappingImplemented = false;
playerphysics->ps.viewangles.x = pitchQuat.getPitch().valueDegrees(); playerphysics->ps.viewangles.x = pitchQuat.getPitch().valueDegrees();
@ -280,19 +280,20 @@ namespace MWWorld
mEngine->deleteRigidBody(handle); 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)) if (OEngine::Physic::RigidBody* body = mEngine->getRigidBody(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 // start positions others than 0, 0, 0
btTransform tr = body->getWorldTransform(); btTransform tr = body->getWorldTransform();
Ogre::Vector3 position = node->getPosition();
tr.setOrigin(btVector3(position.x,position.y,position.z)); tr.setOrigin(btVector3(position.x,position.y,position.z));
body->setWorldTransform(tr); body->setWorldTransform(tr);
} }
if (OEngine::Physic::PhysicActor* act = mEngine->getCharacter(handle)) 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 // start positions others than 0, 0, 0
if (handle == "player") if (handle == "player")
{ {
@ -301,31 +302,32 @@ namespace MWWorld
else else
{ {
act->setPosition(btVector3(position.x,position.y,position.z)); 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)); act->setRotation(btQuaternion(rotation.x, rotation.y, rotation.z, rotation.w));
} }*/
if (OEngine::Physic::RigidBody* body = mEngine->getRigidBody(handle)) 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)); 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()) if(handleToMesh.find(handle) != handleToMesh.end())
{ {
btTransform transform = mEngine->getRigidBody(handle)->getWorldTransform();
removeObject(handle); removeObject(handle);
Ogre::Quaternion quat = Ogre::Quaternion(transform.getRotation().getW(), transform.getRotation().getX(), transform.getRotation().getY(), transform.getRotation().getZ()); float scale = node->getScale().x;
Ogre::Vector3 vec = Ogre::Vector3(transform.getOrigin().getX(), transform.getOrigin().getY(), transform.getOrigin().getZ()); Ogre::Quaternion quat = node->getOrientation();
Ogre::Vector3 vec = node->getPosition();
addObject(handle, handleToMesh[handle], quat, scale, vec); addObject(handle, handleToMesh[handle], quat, scale, vec);
} }
} }

View file

@ -34,11 +34,11 @@ namespace MWWorld
void removeObject (const std::string& handle); 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(); bool toggleCollisionMode();

View file

@ -585,7 +585,7 @@ namespace MWWorld
{ {
moveObjectImp(ptr, x, y, z); 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) void World::scaleObject (const Ptr& ptr, float scale)
@ -595,7 +595,7 @@ namespace MWWorld
ptr.getCellRef().scale = scale; ptr.getCellRef().scale = scale;
//scale = scale/ptr.getRefData().getBaseNode()->getScale().x; //scale = scale/ptr.getRefData().getBaseNode()->getScale().x;
ptr.getRefData().getBaseNode()->setScale(scale,scale,scale); 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) 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 roty(Ogre::Degree(-y),Ogre::Vector3::UNIT_Y);
Ogre::Quaternion rotz(Ogre::Degree(-z),Ogre::Vector3::UNIT_Z); Ogre::Quaternion rotz(Ogre::Degree(-z),Ogre::Vector3::UNIT_Z);
ptr.getRefData().getBaseNode()->setOrientation(rotx*roty*rotz); 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 void World::indexToPosition (int cellX, int cellY, float &x, float &y, bool centre) const

View file

@ -129,7 +129,7 @@ void ManualBulletShapeLoader::loadResource(Ogre::Resource *resource)
}; };
cShape->boxTranslation = boxTranslation; cShape->boxTranslation = boxTranslation;
if(mBoundingBox != NULL) if(mBoundingBox != NULL)
cShape->Shape = mBoundingBox; cShape->Shape = mBoundingBox;
else else
{ {

View file

@ -332,6 +332,20 @@ namespace Physic
mHeightFieldMap.erase(name); mHeightFieldMap.erase(name);
} }
void PhysicEngine::adjustRigidBody(BulletShapePtr shape, RigidBody* body, float scale, Ogre::Vector3 position, Ogre::Quaternion rotation){
btTransform tr;
btBoxShape* box = dynamic_cast<btBoxShape*>(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) 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(); std::string sid = (boost::format("%07.3f") % scale).str();
@ -355,17 +369,9 @@ namespace Physic
RigidBody* body = new RigidBody(CI,name); RigidBody* body = new RigidBody(CI,name);
body->collide = shape->collide; body->collide = shape->collide;
btTransform tr; //Pass in BulletShape, RigidBody, scale, position, rotation
btBoxShape* box = dynamic_cast<btBoxShape*>(body->getCollisionShape());
if(box != NULL){ adjustRigidBody(shape, body, scale, position, rotation);
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);
return body; return body;

View file

@ -148,6 +148,8 @@ namespace Physic
*/ */
RigidBody* createAndAdjustRigidBody(std::string mesh,std::string name,float scale, Ogre::Vector3 position, Ogre::Quaternion rotation); 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 * Add a HeightField to the simulation
*/ */

View file

@ -21,6 +21,7 @@ void newtrace(traceResults* const results, const Ogre::Vector3& start, const Ogr
//if (!traceobj->incellptr) //if (!traceobj->incellptr)
// return; // return;
const Ogre::Vector3 rayDir = end - start; 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 startReplace = Ogre::Vector3(650,950, 45);
//Ogre::Vector3 endReplace = startReplace; //Ogre::Vector3 endReplace = startReplace;
//endReplace.z -= .25; //endReplace.z -= .25;
const bool hasHit = NewPhysicsTrace<collisionWorldTrace>(&out, start, end, BBHalfExtents, Ogre::Vector3(0.0f, 0.0f,0.0f), isInterior, enginePass); const bool hasHit = NewPhysicsTrace<collisionWorldTrace>(&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 btVector3 btend(end.x, end.y, end.z);
const btQuaternion btrot(rotation.y, rotation.x, rotation.z); //y, x, 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 from(btrot, btstart);
const btTransform to(btrot, btend); const btTransform to(btrot, btend);