Collision Detection Problem

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
patrickniceboy
Posts: 23
Joined: Fri Apr 27, 2007 3:22 pm
Location: Brazil

Collision Detection Problem

Post by patrickniceboy »

My Problem:
When i put a object X 50, Y -175, Z 89, collision works fine, but if i use X < -100 it doesnt work.

for example the same object X -150, Y -175, Z 89

it's so strange because the collision works with terrain normaly all the cenario

I'm using this code:

Code: Select all

void ISceneNode_assignTriangleSelector(scene::ISceneNode* node, scene::ISceneManager* smgr, io::IFileSystem* ifs)
{
  if (!node || !smgr || !ifs)
    return;

  if (node->getType() == scene::ESNT_OCT_TREE)
  {
    
    io::IAttributes* attribs = ifs->createEmptyAttributes();
    if (attribs)
    {
      node->serializeAttributes(attribs);

      // get the mesh name out
      core::stringc name = attribs->getAttributeAsString("chao");
	  
      attribs->drop();

      // get the animated mesh for the object
      scene::IAnimatedMesh* mesh = smgr->getMesh(name.c_str());
      if (mesh)
      {
        scene::ITriangleSelector* selector =
        smgr->createOctTreeTriangleSelector(mesh->getMesh(0), node, 128);
        node->setTriangleSelector(selector);
        selector->drop();
      }
    }
  }
  else if (node->getType() == scene::ESNT_MESH)
  {
   if (node->getID() == -1)
   {
    scene::IMeshSceneNode* msn = (scene::IMeshSceneNode*)node;
    
    if (msn->getMesh())
    {
      scene::ITriangleSelector* selector =
      smgr->createTriangleSelector(msn->getMesh(), msn);
      msn->setTriangleSelector(selector);
      selector->drop();
	  
     // get objects from irr file
	 // swprintf(test,200,L" X %0.0f Y %0.0f Z %0.0f ",msn->getPosition().X, msn->getPosition().Y, msn->getPosition().Z );
	 
	  
    }
    
   }
   
  }else if (node->getType() == scene::ESNT_ANIMATED_MESH)
  {
    scene::IAnimatedMeshSceneNode* msn = (scene::IAnimatedMeshSceneNode*)node;

    scene::IAnimatedMesh* am = msn->getMesh();
    if (am)
    {
      scene::ITriangleSelector* selector =
        smgr->createTriangleSelector(am->getMesh(0), msn);
      msn->setTriangleSelector(selector);
      selector->drop();
    }
  }
  else if (node->getType() == scene::ESNT_TERRAIN)
  {
    
    scene::ITerrainSceneNode* tsn = (scene::ITerrainSceneNode*)node;

    scene::ITriangleSelector* selector =
      smgr->createTerrainTriangleSelector(tsn, 2); // probably don't want lod 0 all the time...
    tsn->setTriangleSelector(selector);
    selector->drop();
  }
  else if (node->getType() == scene::ESNT_CUBE)
  {

    scene::ITriangleSelector* selector =
      smgr->createTriangleSelectorFromBoundingBox(node);
    node->setTriangleSelector(selector);
    selector->drop();   
  }
  else
  {
      
    // not something we want to collide with
  }
}

void ISceneNode_assignTriangleSelectors(scene::ISceneNode* node, scene::ISceneManager* smgr, io::IFileSystem* ifs)
{
  // assign a selector for this node
  ISceneNode_assignTriangleSelector(node, smgr, ifs);

  // now recurse on children...
  core::list<scene::ISceneNode*>::Iterator begin = node->getChildren().begin();
  core::list<scene::ISceneNode*>::Iterator end   = node->getChildren().end();

  for (; begin != end; ++begin)
    ISceneNode_assignTriangleSelectors(*begin, smgr, ifs);
}


void ISceneManager_assignTriangleSelectors(scene::ISceneManager* smgr, io::IFileSystem* ifs)
{
  ISceneNode_assignTriangleSelectors(smgr->getRootSceneNode(), smgr, ifs);
} 


void ISceneNode_gatherTriangleSelectors(scene::ISceneNode* node, scene::IMetaTriangleSelector* meta, const core::aabbox3df& box)
{
  scene::ITriangleSelector* selector = node->getTriangleSelector();
  if (selector && node->getTransformedBoundingBox().intersectsWithBox(box))
    meta->addTriangleSelector(selector);

  // now recurse on children...
  core::list<scene::ISceneNode*>::Iterator begin = node->getChildren().begin();
  core::list<scene::ISceneNode*>::Iterator end   = node->getChildren().end();

  for (; begin != end; ++begin)
    ISceneNode_gatherTriangleSelectors(*begin, meta, box);
} 


void ISceneManager_gatherTriangleSelectors(scene::ISceneManager* smgr, scene::IMetaTriangleSelector* meta, const core::aabbox3df& box)
{
  ISceneNode_gatherTriangleSelectors(smgr->getRootSceneNode(), meta, box);
} 


int main {
 ....
 ISceneManager_assignTriangleSelectors(smgr, device->getFileSystem());
  meta = smgr->createMetaTriangleSelector(); 

  anms = smgr->addAnimatedMeshSceneNode(smgr->getMesh("..."));
 
scene::ISceneNodeAnimator* anim2 = smgr->createCollisionResponseAnimator(meta, anms, core::vector3df(3,3,3), core::vector3df(0,-0.04,0), core::vector3df(0,2,0));

anms->addAnimator(anim2);  
 ...

}
patrickniceboy
Posts: 23
Joined: Fri Apr 27, 2007 3:22 pm
Location: Brazil

Post by patrickniceboy »

OK, no answers ....

so give me a light !

how do you guys implement collision? i mean when you are doing a game or something like that.. how do you load a scene and make the collisions ?

im loading a xml *.irr using a function loadscene and the function above together, i think it is the best (easy) way for to do it or do you implement (coding) every object one by one?

it works but it's not 100%

please give suggestions how to implement collisions!!
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

I have to be frank and say that the built-in collision detection in Irrlicht is poor, and that it appears unlikely to improve in the short to mid term. That's not intended a criticism of anyone, just a statement of my beliefs.

Until that situation changes, I think that you're most likely wasting your time trying to use Irrlicht collisions for anything other than toy projects, and that you should look into a third party solution like ODE or Bullet.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Regardless of how primitive the collision system is, it should work for most primitive cases. I'd be interested to have a look at your .irr file and source code.

Travis
patrickniceboy
Posts: 23
Joined: Fri Apr 27, 2007 3:22 pm
Location: Brazil

Post by patrickniceboy »

ok vitek here's my project http://sourceforge.net/projects/ziron3d/

data1.dat is a zip file

thank you guys
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

The problem is not with the Irrlicht collision system or my primitive triangle selector assignment/gathering code. The problem is in your code. The bounding box you use to gather selectors is not correct.
patrickniceboy
Posts: 23
Joined: Fri Apr 27, 2007 3:22 pm
Location: Brazil

Post by patrickniceboy »

ok, Travis can you help me please?

is it wrong ?

Code: Select all

 ..
 meta->removeAllTriangleSelectors(); 
 core::aabbox3df box(-1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f);
 camera->getAbsoluteTransformation().transformBoxEx(box);
 box = anms->getBoundingBox();
 ISceneManager_gatherTriangleSelectors(smgr, meta, box );
 ..
[/code]
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Yes, it is wrong. Why would you want to transform a box around the camera into world coordinates, and then overwrite that box with one that represents the character?

Consider that the bounding box is used to find triangle selectors near the colliding object [the player], don't you think you'd want a box around the character in world coordinates?

Travis
Last edited by vitek on Sat Dec 01, 2007 6:40 pm, edited 1 time in total.
patrickniceboy
Posts: 23
Joined: Fri Apr 27, 2007 3:22 pm
Location: Brazil

Post by patrickniceboy »

I changed my code for this:

Code: Select all

 meta->removeAllTriangleSelectors();
core::aabbox3df box(-20.f, -20.f, -20.f, 20.f, 20.f, 20.f);
camera->getAbsoluteTransformation().transformBoxEx(box);
ISceneManager_gatherTriangleSelectors(smgr, meta, box );
Now it works really fine !!!! :D

Thank you so much Travis !!
patrickniceboy
Posts: 23
Joined: Fri Apr 27, 2007 3:22 pm
Location: Brazil

Post by patrickniceboy »

how can i fix it??

now what's wrong?

Image
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

You modified the code that creates the triangle selector for the terrain. Now it uses a lower level-of-detail for the terrain collision mesh. As a side effect the characters feet sink into the ground.
patrickniceboy
Posts: 23
Joined: Fri Apr 27, 2007 3:22 pm
Location: Brazil

Post by patrickniceboy »

ohh sorry... i forgot it !! :?

Thank you! :D
patrickniceboy
Posts: 23
Joined: Fri Apr 27, 2007 3:22 pm
Location: Brazil

Post by patrickniceboy »

Here I am again :lol:

Ok I created a water tower in my game, it is about 2000 polygons... so when my game run i have about 50 fps, ok i walk near my water tower so i have 30 or less... :? but it is just when o collide with it.
The problem is because i have 2000 polygons, isn't it?
Is there anyway to get a better performace, or something like LOD?
or the only way is edit my model

what do you do guys? dont you use models with many polygons?

Thank you !
Post Reply