OgreNewt collision detect

St0rM3r

01-06-2008 14:13:26

Hi !

I am trying to detect collisions between objects and a ship using OgreNewt, I have read some tutorials, examples sources code and I have created a C++ class handling collisions. The problem is that that class doesn't work and I dont understand why (I set a breakpoint in userProcess method and it never call it), some help would be appreciated.

It used to work but only one object was colliding with the ship, now nothing works lol.

I call the method enableCollidable for objects and enableCollision (collisionCallback) for the ship.

The OgreNewt BasicFrameListener is active and when I press F3, I see the bounding boxes of objects and ship.

Header of the class :

class AFX_EXTMODEL_CLASS CollisionObject : public OgreNewt::ContactCallback
{
public:
CollisionObject();
int userBegin();
int userProcess();
void userEnd();
void enableCollidable(Ogre::SceneNode *node, Ogre::Entity *entity);
void enableCollision(Ogre::SceneNode *node, Ogre::Entity *entity);
virtual int collisionDetected(OgreNewt::Body *) {return 0;};
void updateBody(Ogre::SceneNode *node);

OgreNewt::Collision *col; /*!< */
OgreNewt::Body *body; /*!< */
};


Code of the class :



CollisionObject::CollisionObject()
{
body = 0;
col = 0;
}

void CollisionObject::updateBody(Ogre::SceneNode *node)
{
if (body && node)
{
Ogre::Vector3 pos = node->getPosition();
Ogre::Quaternion orient = node->getOrientation();
body->setPositionOrientation(pos, orient);
}
}

void CollisionObject::enableCollidable(Ogre::SceneNode *node, Ogre::Entity *entity)
{
if (!soc::World)
soc::World = new OgreNewt::World();
if (node && entity)
{
entity->setNormaliseNormals(true);
Ogre::Vector3 vSize = entity->getBoundingBox().getSize() * node->getScale();
/*col = new OgreNewt::CollisionPrimitives::TreeCollision(soc::World, node, true);*/
/*col = new OgreNewt::CollisionPrimitives::ConvexHull(soc::World, node);*/
col = new OgreNewt::CollisionPrimitives::Box(soc::World, vSize);
body = new OgreNewt::Body(soc::World, col);
delete col;
Ogre::Vector3 inertia = OgreNewt::MomentOfInertia::CalcBoxSolid(vSize.length(), vSize);
body->setMassMatrix(vSize.length(), inertia);
updateBody(node);
}
}

void CollisionObject::enableCollision(Ogre::SceneNode *node, Ogre::Entity *entity)
{
const OgreNewt::MaterialID *worldMaterial = soc::World->getDefaultMaterialID();
const OgreNewt::MaterialID *collisionMaterial = new OgreNewt::MaterialID(soc::World);

OgreNewt::MaterialPair* collisionLink = new OgreNewt::MaterialPair(soc::World, worldMaterial, collisionMaterial);
collisionLink->setContactCallback(this);
collisionLink->setDefaultFriction(0.0, 0.0);

enableCollidable(node, entity);

body->setMaterialGroupID(collisionMaterial);
body->setUserData(this);
}

int CollisionObject::userProcess()
{
if (m_body0 == body)
{
return collisionDetected(m_body1);
}
else if (m_body1 == body)
{
return collisionDetected(m_body0);
}
return 0;
}

int CollisionObject::userBegin()
{
m_body0;
m_body1;
return 0;
}

void CollisionObject::userEnd()
{
m_body0;
m_body1;
}



Sorry for my english.

Thanks you guys !

St0rM3r

05-06-2008 17:02:06

Ok i have found the solution on this forum :

World->setWorldSize(Box);

I suggest you to add the use of this method in tutorials because it s not intuitive.

edit: if you want to use my class, dont forget to replace return 0; by return 1; in userBegin(), it doesn't trouble me for testing because I was using breakpoint on all user* methods to test if collision occured or not.