Collision detection not working

Empaler

11-04-2010 18:23:57

Hi

As a final part of my project is detecting collisions between a player and the map walls and between a player and some boxes.
The problem is that its not working, the program crashes after initializing.

This is my ContactCallback code:

#pragma once

#include <OgreNewt.h>

#define OBJ_TYPE 3
#define PLAYER_TYPE 2
#define MAP_TYPE 1

class bodyCallback : public OgreNewt::ContactCallback
{
public:
bodyCallback(void);
~bodyCallback(void);

int onAABBOverlap( OgreNewt::Body* body0, OgreNewt::Body* body1, int threadIndex );

void contactsProcess( OgreNewt::ContactJoint &contactJoint, Ogre::Real timeStep, int threadIndex );

private:
};



bodyCallback.h

#include "bodyCallback.h"
#include "OgreNewtonFrameListener.h"
#include <windows.h>
#include <mmsystem.h>

bodyCallback::bodyCallback(void) : OgreNewt::ContactCallback()
{
}

bodyCallback::~bodyCallback(void)
{
}

int bodyCallback::onAABBOverlap( OgreNewt::Body* body0, OgreNewt::Body* body1, int threadIndex )
{
// if none of the bodies is the belt, return 0 to remove contact
if( (body0->getType() == MAP_TYPE && body1->getType() == PLAYER_TYPE) || (body0->getType() == OBJ_TYPE && body1->getType() == PLAYER_TYPE) ||
(body0->getType() == PLAYER_TYPE && body1->getType() == MAP_TYPE) || (body0->getType() == PLAYER_TYPE && body1->getType() == OBJ_TYPE) )
return 1;

return 0;
}

void bodyCallback::contactsProcess( OgreNewt::ContactJoint &contactJoint, Ogre::Real timeStep, int threadIndex )
{
OgreNewt::Body* body0 = contactJoint.getBody0();
OgreNewt::Body* body1 = contactJoint.getBody1();
// first, find which body represents the conveyor belt!

if ((body0->getType() == MAP_TYPE && body1->getType() == PLAYER_TYPE) || (body0->getType() == PLAYER_TYPE && body1->getType() == MAP_TYPE))
{
char soundfile[] = "..\\..\\media\\sounds\\tada.wav";
PlaySound(soundfile,NULL, SND_ALIAS | SND_ASYNC);
}

if ((body0->getType() == OBJ_TYPE && body1->getType() == PLAYER_TYPE) || (body0->getType() == PLAYER_TYPE && body1->getType() == OBJ_TYPE))
{
char soundfile[] = "..\\..\\media\\sounds\\tada.wav";
PlaySound(soundfile,NULL, SND_ALIAS | SND_ASYNC);
}
}


bodyCallback.cpp

And my Application code

#pragma once


#include <ExampleApplication.h>
#include <OgreNewt.h>
#include "bodyCallback.h"
// CEGUI has issues with the Ogre memory manager, this seems to fix it for now.
//#include <OgreNoMemoryMacros.h>
#include <CEGUI/CEGUI.h>
//#include <OgreMemoryMacros.h>


#include <OgreCEGUIRenderer.h>
#include <OgreCEGUIResourceProvider.h>

class OgreNewtonApplication :
public ExampleApplication
{
public:
OgreNewtonApplication(void);
~OgreNewtonApplication(void);

protected:
void createFrameListener();
void createScene();
void createMaterials();
void destroyScene();

// our custom function to simplify making simpe dynamics rigid body boxes.
bool readcaixafile(void);
void criacaixas();
OgreNewt::Body* makeSimpleBox( Ogre::Vector3 size, Ogre::Vector3 pos, Ogre::Quaternion orient, const char* materialName );

void makePlayer();

private:

OgreNewt::World* m_World;
OgreNewt::PlayerController *mPlayer;
Ogre::Vector3 ArrayCaixas[50];// ArrayBox[50][3];
Ogre::Timer* mTimer;
int TotCaixas;

OgreNewt::MaterialPair* mPair1;
OgreNewt::MaterialPair* mPair2;
OgreNewt::MaterialID* BoxID;
OgreNewt::MaterialID* PlayerID;
OgreNewt::MaterialID* MapID;


Ogre::FrameListener* mNewtonListener;

int mEntityCount;

CEGUI::OgreCEGUIRenderer* mGUIRenderer;
};



OgreNewtonApplication.h

/*
OgreNewt library - connecting Ogre and Newton!

Demo09_PlayerController
*/
#include "OgreNewtonApplication.h"
#include "OgreNewtonFrameListener.h"

#include <OgreNewt.h>
#include <OgreNewt_BasicFrameListener.h>


OgreNewtonApplication::OgreNewtonApplication(void)
{
// create OgreNewt world.
m_World = new OgreNewt::World();
mPlayer = NULL;
mTimer = new Timer();
mTimer->reset();

mEntityCount = 0;

}

OgreNewtonApplication::~OgreNewtonApplication(void)
{
// destroy world.
if( mPlayer )
delete mPlayer;
delete m_World;
}



void OgreNewtonApplication::createScene()
{

// setup CEGUI
mGUIRenderer = new CEGUI::OgreCEGUIRenderer( mWindow, Ogre::RENDER_QUEUE_OVERLAY, false, 3000, mSceneMgr );
new CEGUI::System( mGUIRenderer );

// load up CEGUI stuff...
try
{
using namespace CEGUI;
CEGUI::Logger::getSingleton().setLoggingLevel( CEGUI::Informative );

CEGUI::SchemeManager::getSingleton().loadScheme((CEGUI::utf8*)"TaharezLookSkin.scheme");
CEGUI::System::getSingleton().setDefaultMouseCursor((CEGUI::utf8*)"TaharezLook", (CEGUI::utf8*)"MouseArrow");
CEGUI::System::getSingleton().setDefaultFont((CEGUI::utf8*)"BlueHighway-10");

CEGUI::Window* sheet = CEGUI::WindowManager::getSingleton().createWindow( (CEGUI::utf8*)"DefaultWindow", (CEGUI::utf8*)"root_wnd" );
CEGUI::System::getSingleton().setGUISheet( sheet );

//makeGUI();
//setupGUI();

}
catch (CEGUI::Exception)
{}


// sky box.
mSceneMgr->setSkyBox(true, "Examples/CloudyNoonSkyBox");

// shadows on!
mSceneMgr->setShadowTechnique( Ogre::SHADOWTYPE_STENCIL_ADDITIVE );

createMaterials();

// floor object!
Entity* floor;
SceneNode* floornode;
ConfigFile cf;
cf.load("ogremazeconfig.cfg");
Ogre::String MapaName = cf.getSetting("MeshMapName");
char MapName[255];
sprintf( MapName, "%s", MapaName.c_str());

// const char *levelName = "simple_terrain.mesh";
// const char *levelName = "castle.mesh";
const char *levelName = MapName;
// const char *levelName = "playground.mesh";

floor = mSceneMgr->createEntity("Floor", levelName );

floornode = mSceneMgr->getRootSceneNode()->createChildSceneNode( "FloorNode" );
floornode->attachObject( floor );
floor->setCastShadows( false );


//Ogre::Vector3 siz(100.0, 10.0, 100.0);
char serializeCollisionName[256];
strcpy (serializeCollisionName, levelName);
strtok (serializeCollisionName, ".");
strcat (serializeCollisionName, ".col");

FILE* file = fopen (serializeCollisionName, "rb");
if (!file) {
// serialize the mesh so that next time loads faster
OgreNewt::CollisionSerializer saveLevelCollision;
OgreNewt::CollisionPtr tmpSphape = OgreNewt::CollisionPtr(new OgreNewt::CollisionPrimitives::TreeCollision( m_World, floor, true, 0 ));
saveLevelCollision.exportCollision(tmpSphape, serializeCollisionName);
file = fopen (serializeCollisionName, "rb");
}

// load level collision from serialized file
FileHandleDataStream streamFile (file);
OgreNewt::CollisionSerializer loadLevelCollision;
OgreNewt::CollisionPtr col = loadLevelCollision.importCollision(streamFile, m_World);
fclose (file);

OgreNewt::Body* bod = new OgreNewt::Body( m_World, col );
#ifdef OGRENEWT_NO_COLLISION_SHAREDPTR
delete col;
#endif

//floornode->setScale( siz );
bod->attachNode( floornode );
bod->setPositionOrientation( Ogre::Vector3(0.0f, 0.0f, 0.0f), Ogre::Quaternion::IDENTITY );
bod->setType(MAP_TYPE);

MapID = new OgreNewt::MaterialID(m_World);
bod->setMaterialGroupID(MapID);

makePlayer();

criacaixas();

// position camera
mCamera->setPosition(0.0f, 1.0f, 0.0f);

// set the near and far clip plane
mCamera->setNearClipDistance(0.2f);
mCamera->setFarClipDistance(1000.0f);

//make a light
Ogre::Light* light;

light = mSceneMgr->createLight( "Light1" );
light->setType( Ogre::Light::LT_POINT );
light->setPosition( Ogre::Vector3(0.0, 100.0, 100.0) );
}


void OgreNewtonApplication::createFrameListener()
{
// It is important that the Physics update is updated before anything
mNewtonListener = new OgreNewt::BasicFrameListener( mWindow, m_World, 100 );
mRoot->addFrameListener(mNewtonListener);

mFrameListener = new OgreNewtonFrameListener( mWindow, mCamera, mSceneMgr, m_World, mPlayer, TotCaixas, mTimer);
mRoot->addFrameListener(mFrameListener);

}

void OgreNewtonApplication::destroyScene()
{
CEGUI::System* sys = CEGUI::System::getSingletonPtr();
delete sys;

// CEGUI Cleanup
delete mGUIRenderer;
}

void OgreNewtonApplication::makePlayer()
{
Entity* ellipsoid;
SceneNode* node;

ellipsoid = mSceneMgr->createEntity( "PlayerControllerEntity", "ellipsoid.mesh");
node = mSceneMgr->getRootSceneNode()->createChildSceneNode();
node->attachObject( ellipsoid );
Ogre::Vector3 size (1.0f, 2.5f, 2.0f);
node->setScale(size);

OgreNewt::ConvexCollisionPtr col = OgreNewt::ConvexCollisionPtr(new OgreNewt::CollisionPrimitives::Ellipsoid( m_World, size * 0.5f, 0 ));
OgreNewt::Body* bod = new OgreNewt::Body( m_World, col );
Ogre::Vector3 inertia, offset;
col->calculateInertialMatrix(inertia, offset);
#ifdef OGRENEWT_NO_COLLISION_SHAREDPTR
delete col;
#endif
bod->attachNode(node);
bod->setMassMatrix( 50, 50*inertia );
bod->setCenterOfMass(offset);
bod->setStandardForceCallback();
bod->setType(PLAYER_TYPE);

PlayerID = new OgreNewt::MaterialID(m_World);
bod->setMaterialGroupID(PlayerID);

ellipsoid->setMaterialName("Simple/dirt01");
ellipsoid->setVisible(false);

bod->setPositionOrientation(Ogre::Vector3(0.0f, 1.0f, 0.0f), Ogre::Quaternion::IDENTITY);

// set teh play to alway be actibe
bod->setAutoSleep(0);
mPlayer = new OgreNewt::PlayerController(bod, 0.4);
}

bool OgreNewtonApplication::readcaixafile()
{
char str [80] = "teste.txt";
FILE *file;
int p, cont=0;
Ogre::Vector3 tempvec;

if((file = fopen(str,"r"))== NULL)
{
printf("Erro na abertura do ficheiro:%s ",str);
return false;
}

p=fscanf(file,"%d x:%f y:%f z:%f\n", TotCaixas, tempvec.x, tempvec.y, tempvec.z);
ArrayCaixas[cont]=tempvec;
cont++;
while (p!=EOF)
{
p=fscanf(file,"%d x:%f y:%f z:%f\n", TotCaixas, tempvec.x, tempvec.y, tempvec.z);
ArrayCaixas[cont]=tempvec;
cont++;
}

return true;
}

void OgreNewtonApplication::criacaixas()
{
char MapName[255];
Ogre::String MapaName;
ConfigFile cf;
cf.load("ogremazeconfig.cfg");
Ogre::Vector3 size (1.0f, 3.5f, 2.0f);

//char tests [80] = "testtesttest.txt";
//FILE *testf;
//Ogre::Vector3 testv;
//testf = fopen(tests,"w");

if (cf.getSetting("IfUsingObjects")=="yes" || cf.getSetting("IfUsingObjects")=="Yes")
{
MapaName = cf.getSetting("ObjectFileName");
sprintf( MapName, "%s", MapaName.c_str());

FILE *file;
int p, cont=0;
Ogre::Vector3 tempvec;

file = fopen(MapName,"r");

p=fscanf(file,"%d x:%f y:%f z:%f\n", &TotCaixas, &tempvec.x, &tempvec.y, &tempvec.z);
/*fprintf(testf,"%d x:%f y:%f z:%f\n", TotCaixas, tempvec.x, tempvec.y, tempvec.z);*/
ArrayCaixas[cont]=tempvec;
cont++;
while (p!=EOF)
{
p=fscanf(file,"%d x:%f y:%f z:%f\n", &TotCaixas, &tempvec.x, &tempvec.y, &tempvec.z);
ArrayCaixas[cont]=tempvec;
cont++;
}

Ogre::Vector3 pyramidBlock (0.75f, 0.35f, 0.5f);
Ogre::Quaternion orient (Ogre::Radian (0.5f * 3.1416f), Ogre::Vector3 (0.0f, 1.0f, 0.0f));

for (int i = 0; i < TotCaixas; i ++)
{
OgreNewt::Body* box;
box = makeSimpleBox (size, ArrayCaixas[i], orient, "Simple/NewtonLogo");
}
fclose(file);
}

}

OgreNewt::Body* OgreNewtonApplication::makeSimpleBox( Ogre::Vector3 size, Ogre::Vector3 pos, Ogre::Quaternion orient, const char* materialName )
{
Entity* box1;
SceneNode* box1node;

box1 = mSceneMgr->createEntity( "Entity"+Ogre::StringConverter::toString(mEntityCount++), "box.mesh" );
box1node = mSceneMgr->getRootSceneNode()->createChildSceneNode();
box1node->attachObject( box1 );
box1node->setScale( size );

OgreNewt::ConvexCollisionPtr col (new OgreNewt::CollisionPrimitives::Box( m_World, size, 0 ));
OgreNewt::Body* bod = new OgreNewt::Body( m_World, col );

bod->setPositionOrientation( pos, orient );

// base mass on the size of the object.
Ogre::Real mass = size.x * size.y * size.z * 2.5;
// calculate the inertia based on box formula and mass
Ogre::Vector3 inertia, offset;
col->calculateInertialMatrix(inertia, offset);

bod->attachNode( box1node );
bod->setMassMatrix( mass, mass*inertia );
bod->setCenterOfMass(offset);
bod->setStandardForceCallback();
bod->setType(OBJ_TYPE);

BoxID = new OgreNewt::MaterialID(m_World);
bod->setMaterialGroupID(BoxID);

box1->setMaterialName( materialName );

return bod;
}

void OgreNewtonApplication::createMaterials()
{
bodyCallback* mbodyCallback = new bodyCallback( );
//mPair1 = new OgreNewt::MaterialPair( m_World, PlayerID, MapID );
mPair2 = new OgreNewt::MaterialPair( m_World, BoxID, PlayerID );
//mPair1->setContactCallback( mbodyCallback );
mPair2->setContactCallback( mbodyCallback );
}


OgreNewtonApplication.cpp

I need to get this done asap so any help is very welcome =)

Thanks

Empaler

12-04-2010 11:40:05

Now im getting confused ....
class bodyCallback : public OgreNewt::ContactCallback
{
public:
bodyCallback(void);
~bodyCallback(void);

int onAABBOverlap( OgreNewt::Body* body0, OgreNewt::Body* body1, int threadIndex );

void contactsProcess( OgreNewt::ContactJoint &contactJoint, Ogre::Real timeStep, int threadIndex );

private:
};


In conveyor example its shown a class like this... but in the tutorials it says this is mogre and in ogrenewt uses UserBegin() UserProcess() and UserEnd() ...
Which shall i use ??? :S

Pls anyone help

Empaler

19-04-2010 12:09:47

Anyone??? ;/

kallaspriit

24-04-2010 18:25:39

The conveyor demo is correct, the start-process-end was changed to overlap-process in NGD 2.

Empaler

02-05-2010 18:39:23

Works now =)

Thanks