OT: C++ loop Question?

I know this is off topic, but it has to do with my Rendering Loop. Currenlty I am checking each of my Items/NPC to see if there in a zone i can see. I do this with a for loop.

for(a=0; a<NumItems; a++)
if(Item[a].InViewableRoom = true)

My question is this, how long does it take a compiler to check to see if an if is valid?? I do this for quite a few objects each time the scene is drawn. I want to draw 60ish frames per second. I thought it might be better to create a linked list, knowing what objects were in what rooms, but they can change rooms at any time, if they move. So a linked list is not realy feasible, without rewriting my current setup. So mainly i want to know, are using large if chains slow? or are they run through pretty quickly??


“if” statements are usualy not the problem when considering performance.
You should be probably more worried about how many “for” loops you use, specially if there one inside others.
The performance will also depend on the complexity of the item you draw with you DrawItem() function.



for loops and if statements aren’t necessarily a bad thing, however in the context of what you are using a tree of some sort would be better (hint: search for BSP trees, quad-trees, oct-trees and/or portals)

With a tree you can hierarchically group objects into rooms and test to see if you can see a room, if you can then only test the objects in that room. If not don’t bother.

You may want to have a look at http://www.gametutorials.com/Tutorials/OpenGL/OpenGL_Pg5.htm for their octree tutorials and http://nate.scuzzy.net/programming/ for the BSP demo. I’m sure there are a million and one other tutorials and references you can find with google…

If you decide to use one of these methods, research them all to find out which one is most suitable for your game.

[This message has been edited by Rob The Bloke (edited 08-13-2002).]

Well, i am doing something similar to that now. I am however NOT using BSP. I have simply broke my world up into zones, and each zone has a PVS. I simply check which zone im currently in (few sqrts each time i move, so no biggie) then i simply check out of the PVS, what is visable (with a box in frustum check, once again a few sqrts, so no biggie) Then i run through my items, and NPC, (because, like i said earlier, they can move at any time). I simply check to see if there listed in a room that is curretnly drawn, and if they are I run them through a bounding sphere in zone frustum check, to once again see if there visable.

THEN if there visable after ALL that, i draw them. I think this is a very very agressive method of dumping objects that arent drawn anyways. BUT, i have to run through them ‘all’ because i have to move them anyways.

Take for example, a box, if I shoot a box off a shelf, and turn around before it hits the floor. If is was using a tree, and determined that the box was not visable, it would never update the box while NOT visable, so the box would freeze in mid air. So i ‘have’(maybe not have to, but i think i should) to run through all the objects anyhow to test for movement. So i think this present setup is ok. I was just curious about if the IFS, and FORS were going to cause some issues.

Thanks for the help, let me know what you think about my ideas for culling objects, and rooms. I think its a pretty good algorithm. But i ALWAYS like people to tell me where they might see a fault. Thanks