Mesh always in Y direction

majc

01-01-2009 21:58:30

Im having a strange problem, a weapon is always in Y direction even when i run into it, it lays down in the ground and return to Y direction(it seems that took Viagra lol).

Here is a picture:



This is my Physics Interface Class:

#include "cPhysicsInterface.h"

cPhysicsInterface *cPhysicsInterface::instance = NULL;

cPhysicsInterface::cPhysicsInterface()
{
// create OgreNewt world.
mWorld = new OgreNewt::World();
mWorld->setWorldSize(Vector3(-50000,-50000,-50000),Vector3(50000,50000,50000));
gravity = Ogre::Vector3(0,-98,0);
}

cPhysicsInterface *cPhysicsInterface::getInstance()
{
if (instance == NULL)
return instance = new cPhysicsInterface();
else
return instance;
}


void cPhysicsInterface::setCustomForceCallback(Body *body)
{
}

Real cPhysicsInterface::getHeightFromGround()
{
return 0;
}

Ogre::Vector3 cPhysicsInterface::getPosition(Body *body)
{
Ogre::Vector3 pos = Ogre::Vector3::ZERO;
Ogre::Quaternion orient = Quaternion::IDENTITY ;
body->getPositionOrientation(pos,orient);
return pos;
}

Ogre::Quaternion cPhysicsInterface::getOrientation(Body *body)
{
Ogre::Vector3 pos;
Ogre::Quaternion orient;
body->getPositionOrientation(pos,orient);
return orient;
}

void cPhysicsInterface::addLocalForce(Body *body, const Ogre::Vector3 &force, const Ogre::Vector3 &position)
{
body->addLocalForce(force,position);
}

void cPhysicsInterface::addTorque(Body *body, const Ogre::Vector3 &torque)
{
body->addTorque(torque);
}

void cPhysicsInterface::setBodyBehavior(Body *body , int damage, bool mode)
{
}

Body *cPhysicsInterface::createPhysicalBody(Body *body, SceneNode *node, Ogre::Vector3 position, Quaternion orientation)
{

return mBody;
}

Ogre::Vector3 cPhysicsInterface::createInertia(Real m, Ogre::Vector3 &size)
{
return Vector3::ZERO;
}

World *cPhysicsInterface::getNewtWorld()
{
return mWorld;
}

Ogre::Vector3 cPhysicsInterface::getGravity()
{
return gravity;
}

Body *cPhysicsInterface::createNewtBody(Collision *collision)
{
return new Body(mWorld,collision);;
}

void cPhysicsInterface::assignBodyToNode(Body* body, SceneNode *node)
{
body->attachToNode(node);
}

void cPhysicsInterface::setMassMatrix(Body* body, Real mass, Ogre::Vector3 inertia)
{
body->setMassMatrix( mass, inertia );
}

void cPhysicsInterface::setAutoFreeze(Body* body, int active)
{
body->setAutoFreeze(active);
}

void cPhysicsInterface::setPositionOrientation(Body *body, Ogre::Vector3 position, Quaternion orientation)
{
body->setPositionOrientation(position, orientation);
}

void cPhysicsInterface::setType(Body* body, int type)
{
body->setType(type);
}

const MaterialID *cPhysicsInterface::createMaterialDefault()
{
const MaterialID* mMatDefault = mWorld->getDefaultMaterialID();
return mMatDefault;
}

const MaterialID *cPhysicsInterface::createMaterialDefault2()
{
const MaterialID* mMatDefault2 = new MaterialID( mWorld );
return mMatDefault2;
}


MaterialPair *cPhysicsInterface::createMaterialPair(const MaterialID *materialDefault, const MaterialID *materialDefault2)
{
MaterialPair *mMatPairDefault = new MaterialPair(mWorld,materialDefault,materialDefault2);
return mMatPairDefault;
}

void cPhysicsInterface::setDefaultFriction(MaterialPair *materialPairDefault, Real stat, Real kinetic)
{
materialPairDefault->setDefaultFriction(stat,kinetic);
}

void cPhysicsInterface::setMaterialGroupID(Body* body, const MaterialID *materialDefault2)
{
body->setMaterialGroupID(materialDefault2);
}

CollisionPrimitives::TreeCollisionSceneParser* cPhysicsInterface::createTreeCollisionSceneParser()
{
CollisionPrimitives::TreeCollisionSceneParser* stat_col = new CollisionPrimitives::TreeCollisionSceneParser( mWorld );
return stat_col;
}

Collision *cPhysicsInterface::createCollisionBox(const Vector3 &size)
{
return new CollisionPrimitives::Box( mWorld, size );
}

Collision *cPhysicsInterface::createCollisionEllipsoid(const Vector3 &size)
{
return new CollisionPrimitives::Ellipsoid( mWorld, size );
}
Collision *cPhysicsInterface::createCollisionConvexHull(SceneNode *node)
{
return new CollisionPrimitives::ConvexHull( mWorld, node );
}

void cPhysicsInterface::setLinearDamping(Body* body, Real damp)
{
body->setLinearDamping ( 0.01f );
}

void cPhysicsInterface::setAngularDamping(Body* body, Vector3 &damp)
{
body->setAngularDamping(damp);
}

void cPhysicsInterface::setUserData(Body* body, void *data)
{
body->setUserData(data);
}

void cPhysicsInterface::setDefaultSoftness(MaterialPair *materialPairDefault, Real softness)
{
materialPairDefault->setDefaultSoftness(softness);
}

void cPhysicsInterface::setDefaultElasticity(MaterialPair *materialPairDefault, Real elasticity)
{
materialPairDefault->setDefaultElasticity(elasticity);
}

void cPhysicsInterface::setContinuousCollisionMode(MaterialPair *materialPairDefault, int mode)
{
materialPairDefault->setContinuousCollisionMode(1);
}


cPhysicsInterface::~cPhysicsInterface()
{
if(mWorld)
delete mWorld;
}


This is the weapon body code:

void cWeaponCollisionHandler::createEntity(Ogre::String entityName, Ogre::String meshName, Ogre::String textureName, Ogre::String nodeName, Ogre::Vector3 pos, Ogre::Vector3 scale)
{
Quaternion q;
node = mWeaponGraph->createWeaponNode(entityName,meshName,textureName,nodeName,pos,scale);
Collision *col = cPhysicsInterface::getInstance()->createCollisionConvexHull(node);
mWeaponBody = cPhysicsInterface::getInstance()->createNewtBody(col);
createPhysicalBody(mWeaponBody, node, position,Quaternion::IDENTITY);
}

Body *cWeaponCollisionHandler::createPhysicalBody(Body *body, SceneNode *node, Ogre::Vector3 position, Quaternion orientation)
{
Ogre::Vector3 inertia = createInertia(mWeapon->getMass(), Ogre::Vector3(.1,5,.1));
cPhysicsInterface::getInstance()->setMassMatrix(body,mWeapon->getMass(),inertia);
body->setCustomForceAndTorqueCallback <cWeaponCollisionHandler>(&cWeaponCollisionHandler::setCustomForceCallback,this);
cPhysicsInterface::getInstance()->setAutoFreeze(body,false);
cPhysicsInterface::getInstance()->assignBodyToNode(body,node);
cPhysicsInterface::getInstance()->setType(body,3);
cPhysicsInterface::getInstance()->setUserData(body,this);
cPhysicsInterface::getInstance()->setPositionOrientation(body,position,orientation);
/*cPhysicsInterface::getInstance()->setAngularDamping(body,Ogre::Vector3(0.01f, 0.01f, 0.01f ));
const MaterialID *mMatDefault = cPhysicsInterface::getInstance()->createMaterialDefault();
const MaterialID *mMatDefault2 = cPhysicsInterface::getInstance()->createMaterialDefault2();
MaterialPair *mMatPairDefault = cPhysicsInterface::getInstance()->createMaterialPair(mMatDefault,mMatDefault2);
cPhysicsInterface::getInstance()->setDefaultFriction(mMatPairDefault,0,0);
cPhysicsInterface::getInstance()->setDefaultSoftness(mMatPairDefault,1);
cPhysicsInterface::getInstance()->setDefaultElasticity(mMatPairDefault,0.0);
cPhysicsInterface::getInstance()->setContinuousCollisionMode(mMatPairDefault,0);
cPhysicsInterface::getInstance()->setMaterialGroupID(body,mMatDefault2);*/
return body;
}


This is the ground's body code:

void cLevelObjectsCollisionHandler::createEntity(Ogre::String entityName, Ogre::String meshName, String textureName, Ogre::String nodeName, bool castShadows, bool normaliseNormals, Ogre::Vector3 position, Ogre::Vector3 scale, Real mass, int bodyType)
{
SceneNode *node = mObjectGraph->createObjectNode(entityName,meshName,textureName,nodeName,castShadows,normaliseNormals,position,scale);
CollisionPrimitives::TreeCollisionSceneParser *col = cPhysicsInterface::getInstance()->createTreeCollisionSceneParser();
col->parseScene(node,true);
mLevelObjectBody = cPhysicsInterface::getInstance()->createNewtBody(col);
createPhysicalBody(mLevelObjectBody,node,Vector3(0,0,0),Quaternion::IDENTITY);
}

Body *cLevelObjectsCollisionHandler::createPhysicalBody(Body *body, SceneNode *node, Vector3 position, Quaternion orientation)
{
Ogre::Vector3 inertia = cPhysicsInterface::getInstance()->createInertia(mObjects->getMass(),Ogre::Vector3(10,10,10));
cPhysicsInterface::getInstance()->setMassMatrix(body,mObjects->getMass(),inertia);
body->setCustomForceAndTorqueCallback <cLevelObjectsCollisionHandler>(&cLevelObjectsCollisionHandler::setCustomForceCallback,this);
cPhysicsInterface::getInstance()->assignBodyToNode(body,node);
cPhysicsInterface::getInstance()->setUserData(body,this);
cPhysicsInterface::getInstance()->setAutoFreeze(body,false);
cPhysicsInterface::getInstance()->setPositionOrientation(body,position,orientation);
body->attachToNode(node);
return body;
}


Here is where i call the ground:

mEntityCollision->createEntity("Ground","Plane01.mesh","Examples/Rockwall","GroundNode",false,true,Ogre::Vector3(0,0,0),Ogre::Vector3(0.1,0.1,0.1),ground->getMass(),ground->getType());

Here is where i call the weapon:

weaponCollision->createEntity("Weapon2","m4a1.mesh","weapon","Weapon2",Ogre::Vector3(0.0,300.0,0.0),Ogre::Vector3(0.1,0.1,0.1));

As you can see i dont have nay UPVector in the weapon's body, why this is happening?

Thanks in advance!

nullsquared

01-01-2009 22:03:13

Check your weapon model's pivot point (its center). Most of the time, what you described occurs because a body's center of mass is at the mesh's pivot point/center, and the pivot point/center of the mesh is not actually in the center, but at some arbitrary point, like the bottom.

majc

01-01-2009 22:56:28

How can i check that? Sorry my ignorance.

nullsquared

02-01-2009 15:47:09

Best way would be to check in in a modeling application, but you can also use MeshMagick (http://www.ogre3d.org/forums/viewtopic. ... meshmagick) to center it, check the -yalign option in the thread.