Problem with materials and collision callbacks (SOLVED)

hoffhoff

06-08-2006 20:39:54

Hello,
I have ogrenewt working fine, the movement of the player and so on. Now I´m trying to start with materials and the collision callbacks. For now, all I want to do is to close the program (exit(0)) when two objects collides.

So, my contact callback is like this:

#include "CContactCallback.h"

CContactCallback::CContactCallback(int ID) : OgreNewt::ContactCallback()
{
}

CContactCallback::~CContactCallback(void)
{
}

int CContactCallback::userProcess()
{
exit(0);
return 1;
}


then I created another class to hold all the materials informations:


//.h:
#include "CContactCallback.h"

#define MAT_DEFAULT 0
#define MAT_WOOD 1
#define MAT_METAL 2
#define MAT_GRASS 3
#define MAT_HUMAN 4

#define TOTAL_MATERIALS 5 // Useful while working with defines.

class CMatCollision
{
private:
OgreNewt::World* myWorld;
const OgreNewt::MaterialID *mat_vector[TOTAL_MATERIALS], *matdefault;
OgreNewt::MaterialPair* mat_pair_human_metal;
CContactCallback *cont;

public:
CMatCollision(OgreNewt::World* nworld);
~CMatCollision(void);
void CreateMaterials();
const OgreNewt::MaterialID* getMaterialID(int matnum);
void startPairs();
};


//.cpp

#include "CMatCollision.h"


CMatCollision::CMatCollision(OgreNewt::World* nworld)
{
myWorld = nworld;
CreateMaterials();
}

CMatCollision::~CMatCollision(void)
{
}

void CMatCollision::CreateMaterials()
{
int i;
for (i=0; i < TOTAL_MATERIALS; i++)
{
mat_vector[i] = new OgreNewt::MaterialID( myWorld);
}
matdefault = myWorld->getDefaultMaterialID();
}

const OgreNewt::MaterialID* CMatCollision::getMaterialID(int matnum)
{
return mat_vector[matnum];
}

void CMatCollision::startPairs()
{
cont = new CContactCallback(1);
mat_pair_human_metal = new OgreNewt::MaterialPair( myWorld, mat_vector[MAT_HUMAN], mat_vector[MAT_METAL] );
mat_pair_default_default->setContactCallback(cont);
mat_pair_human_metal->setDefaultElasticity(1000000);
mat_pair_human_metal->setContactCallback(cont);
}



Ok. Then, in the main code:

hoffhoff

06-08-2006 20:41:45


CApplication::CApplication(void)
{
mWorld = new OgreNewt::World();
pMatCollision = new CMatCollision(mWorld);
pMatCollision->CreateMaterials();
pMatCollision->startPairs();
}

//...
void CApplication::createScene(void)
{
...
pCharacter = new CCharacter("ninja.mesh", "ninja1", mRoot, mWindow, mSceneMgr, mWorld, Ogre::Vector3(00, 10, 00), Ogre::Vector3(0.1, 0.1, 0.1), pMatCollision);
pCharacter->setMaterial(MAT_HUMAN);

CCharacter * pRobot;
pRobot = new CCharacter("robot.mesh", "robot", mRoot, mWindow, mSceneMgr, mWorld, Ogre::Vector3(00, 10, 10), Ogre::Vector3(0.2, 0.2, 0.2), pMatCollision);
pRobot->setMaterial(MAT_METAL);
pRobot->init();
pCharacter->init();

hoffhoff

06-08-2006 20:42:13

And the CCharacter looks like this:

bool CCharacter::init()
{
//Variables

Ogre::Entity* ent;
//Ogre::Light* light;
OgreNewt::ConvexCollision* conv;


//Ogre::Real mass;
//mass = 80.0;
Vector3 inertia;
Vector3 offset;


std::string entityname = nodename + "entity";
std::string scenenodename = nodename + "scenenode";
//std::string lightname = nodename + "light";


//Create our entity
ent = pSceneManager->createEntity(entityname, meshfilename);
//ent->setCastShadows(true);


//Create a new scenenode
pSceneNode = pSceneManager->getRootSceneNode()->createChildSceneNode(nodename);
pSceneNode->setScale(scale);
pSceneNode->attachObject(ent);


//Now create the newton body
conv = new OgreNewt::CollisionPrimitives::ConvexHull(pWorld, pSceneNode, pSceneNode->getOrientation(), pSceneNode->getPosition());
pBody= new OgreNewt::Body(pWorld, conv);
pBody->setMaterialGroupID(pMatCollision->getMaterialID(this->Material));

pBody->attachToNode( pSceneNode );
pBody->setPositionOrientation( pos, pSceneNode->getOrientation() );
pBody->setUserData(this);
pBody->setAutoFreeze(0);

this->setStateToFloating();


//Now calculate the Intertial
conv->calculateInertialMatrix(inertia, offset);
//We dont need conv anymore
delete conv;
//Set the mass matix
pBody->setMassMatrix(mass, inertia);
//Set force callback
pBody->setCustomForceAndTorqueCallback(CCharacter::playerForceandTorqueCallback);


return true;
}

/// on .h:
void setMaterial(int mat){this->Material = mat;}

//constuctor:

CCharacter::CCharacter(const std::string newmeshfilename, const std::string newnodename, Ogre::Root* newpRoot, Ogre::RenderWindow* newpWin, Ogre::SceneManager* newpCSceneManager, OgreNewt::World* newpWorld, Ogre::Vector3 newstartpos, Ogre::Vector3 newscale, CMatCollision *matcol)
{
pMatCollision = matcol;
meshfilename = newmeshfilename;
nodename = newnodename;
pRoot = newpRoot;
pSceneManager = newpCSceneManager;
pWorld = newpWorld;
pWin = newpWin;
pos = newstartpos;
scale = newscale;
Material = MAT_DEFAULT;

velocity = Ogre::Vector3(0, 0, 0);
movement = Ogre::Vector3(0, 0, 0);
spin = Ogre::Vector3(0, 0, 0);

pBody = NULL;
pSceneNode = NULL;
pLightSceneNode = NULL;

mass = 800; //default
SpeedSpin = 100; //default
SpeedMove = 30; //default

State = ISFLOATING;
}



But the collision callback does not work. Also, there is no difference if I set the value of elasticity to 0 or 10000.
Do someone knows where is the error?

Thanks!

hoffhoff

06-08-2006 21:09:45

Nevermind, I found out my error :oops:

I corrected the code above.
It might be useful for someone =]