Collision Detection

supermael

23-10-2010 19:26:39

Hi !
I've a problem with the collision detection between two bodies. I need to know if two bodies are colliding with each other, like a function :
bool AreColliding(Critter::Body* body1, Critter::Body* body2)
{
if(body1 is colliding with body2)
return true;
else return false;
}

I don't have only two bodies, so the function has to work with the given bodies. Is there a way to have a result like this ?

Thank you !
Supermael

TechnoBulldog

24-10-2010 02:57:29

Does this help? I haven't tried it, and I know it isn't exactly what you wanted, but will this help any?

If you do find out a way to do exactly what you said, let me know. It might be worth setting up a tutorial for it. Sorry I can't help much :(

betajaen

24-10-2010 09:14:14

This is a repost from your other topic about callbacks:

/**

NxOgre Tutorials - 303 - Collision Reporting

Copyright (c) 2009 Robin Southern, http://www.nxogre.org

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

*/

#include "OGRE/SdkSample.h"
#include "OGRE/SamplePlugin.h"
#include "NxOgre.h"
#include "critter.h"

using namespace Ogre;
using namespace OgreBites;
using namespace NxOgre;
using namespace Critter;

class _OgreSampleClassExport NxOgre303 : public SdkSample, public Callback // <<<< Notice we inherit from Callback.
{

public:

NxOgre::World* mWorld;
NxOgre::Scene* mScene;
Critter::RenderSystem* mRenderSystem;

Body* mBodyA;
Body* mBodyB;


void setupPhysics()
{

createWorldSceneEtc();

mBodyA = makeBox(Vec3(0,3,0));
mBodyA->setContactCallback(this);

mBodyB = makeBox(Vec3(0,7,0));
mBodyB->setContactCallback(this);

mScene->setActorFlags(mBodyA, mBodyB, NxOgre::Enums::ContactPairFlags_All);

}

void onContact(const ContactPair& pair)
{
// mBodyA->addForce(NxOgre::Vec3(0,600,0));
std::cout << pair.mEvents << std::endl;
}

Body* makeBox(const Matrix44& globalPose, const Vec3& initialVelocity = Vec3::ZERO)
{

Critter::BodyDescription bodyDescription;
bodyDescription.mMass = 40.0f;
bodyDescription.mLinearVelocity = initialVelocity;

BoxDescription box_desc;
box_desc.mSize.set(1,1,1);

Body* box = mRenderSystem->createBody(box_desc, globalPose, "cube.1m.mesh", bodyDescription);

return box;
}


void createWorldSceneEtc()
{
// The usual suspects.
mWorld = NxOgre::World::createWorld();
NxOgre::ResourceSystem::getSingleton()->openProtocol(new Critter::OgreResourceProtocol());
mWorld->getRemoteDebugger()->connect();
NxOgre::SceneDescription scene_description;
scene_description.mGravity = Constants::MEAN_EARTH_GRAVITY;
scene_description.mUseHardware = false;
mScene = mWorld->createScene(scene_description);
mScene->createSceneGeometry(NxOgre::PlaneGeometryDescription());
mRenderSystem = new Critter::RenderSystem(mScene, mSceneMgr);
mScene->getMaterial(0)->setAll(0.1, 0.5,0.5);
}

bool keyPressed(const OIS::KeyEvent& evt)
{
return SdkSample::keyPressed(evt);
}
void stopPhysics()
{
NxOgre::World::destroyWorld();
}

void setupContent()
{

ColourValue background = ColourValue(16.f/255.f, 16.f/255.f, 16.f/255.f);
mViewport->setBackgroundColour(background);
mSceneMgr->setFog(Ogre::FOG_EXP, background, 0.001, 800, 1000);

// set shadow properties
mSceneMgr->setShadowTechnique(SHADOWTYPE_TEXTURE_MODULATIVE);
mSceneMgr->setShadowColour(ColourValue(0.5, 0.5, 0.5));
mSceneMgr->setShadowTextureSize(1024);
mSceneMgr->setShadowTextureCount(1);

// create a floor mesh resource
Ogre::MeshManager::getSingleton().createPlane("floor", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
Plane(Vector3::UNIT_Y, 0), 1000, 1000, 1,1 , true, 1, 1, 1, Vector3::UNIT_Z);

// create a floor entity, give it a material, and place it at the origin
Entity* floor = mSceneMgr->createEntity("Floor", "floor");
floor->setMaterialName("ground-from-nxogre.org");
floor->setCastShadows(false);
mSceneMgr->getRootSceneNode()->attachObject(floor);

// use a small amount of ambient lighting
mSceneMgr->setAmbientLight(ColourValue(0.3, 0.3, 0.3));

// add a bright light above the scene
Light* light = mSceneMgr->createLight();
light->setType(Light::LT_POINT);
light->setPosition(-10, 40, 20);
light->setSpecularColour(ColourValue::White);

mCamera->setPosition(20,40,20);
mCamera->lookAt(0,15,0);
mCamera->setNearClipDistance(0.02f);
mCamera->setFarClipDistance(1000.0f);
mCameraMan->setTopSpeed(7.5);

setupPhysics();
}

void cleanupContent()
{
stopPhysics();
}


bool frameRenderingQueued(const FrameEvent& evt)
{

// Advance NxOgre.
mWorld->advance(evt.timeSinceLastFrame);

// Don't let the camera go underground.
if (mCamera->getPosition().y < 0.5f)
{
Ogre::Vector3 pos = mCamera->getPosition();
pos.y = 0.5f;
mCamera->setPosition(pos);
}

return SdkSample::frameRenderingQueued(evt);
}

NxOgre303()
{
mInfo["Title"] = "NxOgre 303";
mInfo["Description"] = "NxOgre 303 - Collision Reporting";
mInfo["Thumbnail"] = "thumb_skybox.png";
mInfo["Category"] = "Physics";
mInfo["Help"] = "Filtering collisions.";
}

};

SamplePlugin* sp;
Sample* s;

extern "C" _OgreSampleExport void dllStartPlugin()
{
s = new NxOgre303;
sp = OGRE_NEW OgreBites::SamplePlugin(s->getInfo()["Title"] + " Sample");
sp->addSample(s);
Ogre::Root::getSingleton().installPlugin(sp);
}

extern "C" _OgreSampleExport void dllStopPlugin()
{
Ogre::Root::getSingleton().uninstallPlugin(sp);
OGRE_DELETE sp;
delete s;
}


Basically you need to:

- Make a callback class (make a class and inherit from NxOgre Callback), and add the onContact function.
- Make an instance of the callback class (mCallback = new myCallback)
- For each body you want to watch collisions, tell the body which callback to call; body->setCallback(mCallback);
- Then tell PhysX you want to watch the collisions of these two bodies when they collide; ( mScene->setActorFlags(mBodyA, mBodyB, NxOgre::Enums::ContactPairFlags_All);)

Then fill out this function of what you want to do with them:

void onContact(const ContactPair& pair)
{
// pair contains pointers to A and B rigidBodies.
}

supermael

24-10-2010 11:21:42

I wanna be sur there isn't an other way to make it. Thank you for your reply.

betajaen

24-10-2010 12:07:03

Nope, that's the only way.

esset

01-11-2010 01:27:36

Verrrry helpful post betajaen, thanks.

Chitation

14-11-2010 06:07:11

How do you do collision detection for more than 2 objects? I checked the code in the example and it seems to only work for 2 set objects and I can't put any detection settings in for another object or a group of objects. Please let me know how I can do collision detection for more than 2 objects.

Thanks in advance!!