Hiding entities when they are not on screen

Blitz3D Forums/Blitz3D Programming/Hiding entities when they are not on screen

fall_x(Posted 2005) [#1]
Hi,

I have a lot of entities on a big playing area. I guess this makes the game slower not only because of the rendering (I setp up the camerarange so this is shouldn't be a problem... I guess) but because of the collision detection. So I was thinking I would hide all entities that are not on screen.
What's the best way of doing this? Looping trough all the entities (with types) and checking if they are on screen using EntityInView and if they're not, I hide them?
Is EntityInView fast enough for this? Or is there a better sollution?

Thanks.


_PJ_(Posted 2005) [#2]
It depends on what sort of a game, how many entities etc.

Many previous suggestions involve having your game split into areas, where all the entities for an area are parented to a pivot or something. At the change of area, this pivot (and thus all recursive children) is hidden.

Some of the following may help:

http://www.blitzbasic.com/codearcs/codearcs.php?code=1244

http://www.blitzbasic.com/Community/posts.php?topic=41049

http://www.blitzbasic.com/Community/posts.php?topic=41247

plus check out the code archives (maybe search them for 'Occlusion' as this is what you are trying to achieve.


fall_x(Posted 2005) [#3]
Thanks, I already figured I had to use a grid or something, but parenting them to a pivot is a good idea!
Suppose my areas are 10*10, I only need to divide my player's x-y coords by 10, round it, and I know which area is closest. Then I'll probably have to show that area and possibly it's surrounding areas as well.

Thanks!


jfk EO-11110(Posted 2005) [#4]
you don't have to hide them manually! Blitz will automatcly skip rendering things that are not in view, aka behind the camera. Tho they need to be seperate meshes. But if they once are seperate meshes, you don't have to care about hiding them. Hiding them would also skip collision detection, so if you walk backward, you could fall trough the earth right to china.

Just make sure your world is made of reasonably sized seperate meshes, so the camera will automaticly skip rendering things that are a) out of the camera range or b) not in view.

If you have a huge big one-mesh map, you may use the ClusterizeMesh Function from the codearchives.


fall_x(Posted 2005) [#5]
I think you're missing the point. I know they are not rendered when they're out of range, but still it runs slow because there's collision detection with way too many objects going on. If they're not on screen I don't want there to be any collision detection.

"Hiding them would also skip collision detection, so if you walk backward, you could fall trough the earth right to china."

You're assuming I'm making a first person game then. Which I'm not. :)


fall_x(Posted 2005) [#6]
Ok, this is giving me an FPS-increase of 10 (when there are lots of trees on the screen) to 50 (in less populates areas) :O
Great, it's running at 40-80fps now instead of 25-35fps. Quite an improvement! :)


_PJ_(Posted 2005) [#7]
just to note: Collision Detection can be maintained without rendering if objects are set to Alpha 0.

Perhaps, also, some optimisation may be done on your trees. If this is what seems to be causing the majority of the slowdown.

Are they meshes or sprites? can you texture them with less surfaces? can you replace meshes with sprites at greater distances (L.O.D stuff)?

This is kinda tricky stuff, and Im not the best to advise on it, but it may help a lot.


fall_x(Posted 2005) [#8]
just to note: Collision Detection can be maintained without rendering if objects are set to Alpha 0.
Thanks, but I knew that already :)

Are they meshes or sprites?

meshes, about 50 polygons
can you texture them with less surfaces?

I guess they have 2 surfaces, the trunk (solid) and the leaves (transparant)... I tried grouping them in less meshes with addmesh(), add them to a mesh based on their location, but the 3D engine doesn't support zbuffer on transparant surfaces (or something like that, someone from blitz support explained it to me but I don't have the mail right here), which caused trees to pop up in front of eachother. And it didn't really increased the FPS by much.
can you replace meshes with sprites at greater distances (L.O.D stuff)?

My camera is top-down (well, tilted a bit, but from the top), which means you can't look very far anyway. There are typically 10 trees on screeen at once, and they are not far enough away to replace them with a sprite.

But I'm pretty sure most of the slow-down was caused by the engine checking the collision between my players and every single tree that was in the scene. Now it only has to check a few trees, and it's a LOT faster.
Thanks for the suggestions.


_PJ_(Posted 2005) [#9]
Great stuff! Good luck!


jfk EO-11110(Posted 2005) [#10]
BTW. to reduce the collision work with trees I usually don't set their collision on, but instead place an invisible box around the trunk (alpha zero).

Of course this requires that there ain't no birds or rc model airplanes expecting to collide with the branches.