Optimizing collision detection code

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
TJBaldy
Posts: 16
Joined: Fri Dec 02, 2011 3:06 pm

Optimizing collision detection code

Post 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 :)
RdR
Competition winner
Posts: 273
Joined: Tue Mar 29, 2011 2:58 pm
Contact:

Re: Optimizing collision detection code

Post 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)
TJBaldy
Posts: 16
Joined: Fri Dec 02, 2011 3:06 pm

Re: Optimizing collision detection code

Post 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).
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Re: Optimizing collision detection code

Post 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.
Working on game: Marrbles (Currently stopped).
CuteAlien
Admin
Posts: 9690
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Optimizing collision detection code

Post 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.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Post Reply