Page 1 of 1

Optimizing collision detection code

Posted: Fri Jan 06, 2012 1:20 pm
by TJBaldy
Hey,

So I've implemented some collision code into my game but my FPS drops a fair bit and I'm guessing it's the way I've implemented it. I'm currently doing it the same way the terrain tutorial does the terrain itself, for each individual object (e.g. 4 different fences). So at the moment I'm duplicating the same code which I'm guessing is the problem. At the moment I'm doing it like this...

Code: Select all

 
scene::ITriangleSelector* FenceCollisionNorth = 0;
scene::ITriangleSelector* FenceCollisionWest = 0;
scene::ITriangleSelector* FenceCollisionEast = 0;
scene::ITriangleSelector* FenceCollisionSouth = 0;
then...

Code: Select all

 
//Collision detection for FENCE's
 FenceCollisionNorth = smgr->createOctreeTriangleSelector(metalFenceNodeNorth->getMesh(), metalFenceNodeNorth, 20);
metalFenceNodeNorth->setTriangleSelector(FenceCollisionNorth);
 
FenceCollisionEast = smgr->createOctreeTriangleSelector(metalFenceNodeEast->getMesh(), metalFenceNodeEast, 20);
metalFenceNodeEast->setTriangleSelector(FenceCollisionEast);
                                        
FenceCollisionWest = smgr->createOctreeTriangleSelector(metalFenceNodeWest->getMesh(), metalFenceNodeWest, 20);
metalFenceNodeWest->setTriangleSelector(FenceCollisionWest);
                                        
FenceCollisionSouth = smgr->createOctreeTriangleSelector(metalFenceNodeSouth->getMesh(), metalFenceNodeSouth, 20);
metalFenceNodeSouth->setTriangleSelector(FenceCollisionSouth);
and finally... (I've repeated the next piece of code 4 times for each fence, but to save room and not clutter this post I'm only posting an example of one).

Code: Select all

 
//Create collision on camera for Fence's...
                        scene::ISceneNodeAnimator* coll1 = smgr->createCollisionResponseAnimator(FenceCollisionNorth, fps_camera, core::vector3df(80,50,40),
                                core::vector3df(0,0,0),
                                core::vector3df(0,0,0));
                        FenceCollisionNorth->drop();
                        fps_camera->addAnimator(coll1);
                        coll1->drop();
I know people will reply with answers saying "use OOP properly", but that isn't important right now. I just want to implement the code in the best way possible to aid the FPS without doing it OOP.

Thanks in advance guys :)

Re: Optimizing collision detection code

Posted: Fri Jan 06, 2012 1:28 pm
by RdR
You can batch those 4 meshes into 1 mesh using the BatchingMesh (http://irrext.svn.sourceforge.net/viewv ... chingMesh/)
And create a single octree triangle selector.

Unless you have a different collision response for each fench you can still use 1 triangle selector using the MetaTriangleSelector (http://irrlicht.sourceforge.net/docu/cl ... ector.html)

Re: Optimizing collision detection code

Posted: Fri Jan 06, 2012 1:51 pm
by TJBaldy
RdR wrote:You can batch those 4 meshes into 1 mesh using the BatchingMesh (http://irrext.svn.sourceforge.net/viewv ... chingMesh/)
And create a single octree triangle selector.

Unless you have a different collision response for each fench you can still use 1 triangle selector using the MetaTriangleSelector (http://irrlicht.sourceforge.net/docu/cl ... ector.html)
I'm already creating all the fences from 1 mesh. Is this not the same thing? I just looked at a different forum post about BatchingMesh's and apparently they don't improve performance anyway :S.

When you say different collision response, do you mean the way the camera reacts to the collison, i.e each fence in my game? If so, I have the same response for each fence (you can't walk through it, simple).

Re: Optimizing collision detection code

Posted: Fri Jan 06, 2012 2:24 pm
by serengeor
If you really want to improve the performance of physics, use a real physics engine like bullet, havok, tokamak, physx, ode, ... . They should give you a lot better performance and most probably a lot more features.
And if you have a lot of separate static meshes you should really use mesh batcher to batch those, it should reduce draw calls and also probably ease up the work for physics engine.

You don't even really have to use the whole engines if all you need is collision detection. I'm sure at least bullet can be used to only do the collision detection.

Re: Optimizing collision detection code

Posted: Sat Jan 07, 2012 11:51 am
by CuteAlien
If you are not afraid of recompiling the engine you can also experiment with the Octree defines in Octree.h. OCTREE_USE_HARDWARE true and OCTREE_BOX_BASED false are some many level-layouts the better option and can noticably increase the FPS.

Also the first check should be - do you need all those polygons for collision detection? You rarely use the real geometry which is used for rendering also for collision detection. Instead use invisible simplified geometry which just covers the necessary space.