I’m having problems with Collision Detection using ODE in C++ and Panda3D.
My current code can check if any 2 objects have collided and then do an action.
But how can I make it such that I can do different actions for different types of objects?
e.g if the player collides with an item, the item disappears but if he collides with an enemy, the player will disappear instead.
Here is my current code for collision
void myNearCallback (void *data, dGeomID o1, dGeomID o2){
simpleWorldState* GameWorld;
GameWorld = reinterpret_cast<simpleWorldState*>(data);
const int N = 100;
dContactGeom contact[N];
int n;
n = dCollide(o1,o2,N,contact,sizeof(dContactGeom));
if (n > 0) { //collision!
std::cout << "Collision!!" << std::endl;
GameWorld->collided = true;
//Get the item Id
unsigned int* n = reinterpret_cast<unsigned int*>(dGeomGetData(o1));
if (!(*n)) {
n = reinterpret_cast<unsigned int*>(dGeomGetData(o2));
}
GameWorld->itemCollided = *n;
}
}
// In another update function
void updatePhysics(float dt,simpleWorldState& GameWorld){
GameWorld.collided = false;
dSpaceCollide(myCollSpace,reinterpret_cast<void*>(&GameWorld),myNearCallback);
//remove the item if there was collision
if (GameWorld.collided){
item* tItem = gameItems[GameWorld.itemCollided];
dGeomDestroy(tItem->ColHull);
gameItems.erase(GameWorld.itemCollided);
}
}