Author Topic: Some questions about octrees & collision detection  (Read 955 times)

Offline Raswr

  • byte
  • *
  • Posts: 13
    • View Profile
Some questions about octrees & collision detection
« on: February 27, 2013, 04:12:03 pm »
Hello again  :)

I've been playing with collision detection octrees lately, they are quite interesting structures. This is going to be more of a learning thread, not a technical question. I have a big terrain (2500 triangles) and asigned a 10 depth octree to it (collision mode only). I was amazed at how fast it tests collision using sphere collision on my character. When I get close to that terrain, fps only fall from 57/58 to around 54 or 56 with such a big structure, especially on a phone!. It would be nice to know how this works a bit more (maybe not a completely jpct-ae related question, if it doesn't belong here, i'm sorry, just delete it).

I've been reading about octrees and they recursively subdivide a bigger space in the shape of 8 cubes/bounding boxes. But how can the collision detection system calculate this so fast? I mean, a 10depth octree can get really complex and testing against 8 nodes at every tree level should damage performance by a lot. I guess there are some optimizations but still, I can't believe that these sphere vs triangle collisions are calculating so fast on such a big mesh. Also, given a certain mesh, is there a method to know the optimal depth/max polys for the octree?  I would appreciate any insight on this topic, I'm really curious at how this works :o
« Last Edit: February 27, 2013, 05:57:39 pm by Raswr »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11777
    • View Profile
    • http://www.jpct.net
Re: Some questions about octrees & collision detection
« Reply #1 on: February 28, 2013, 07:26:24 am »
Testing against the nodes is almost trivial. It's a simple primitive (sphere, ellipsoid, ray...) against axis aligned bounding box test, which is pretty fast. It's not much slower than testing against a single polygon, so even at a depth of 10, it's not such a big deal compared to the alternative (testing agains all polygons in the mesh). BTW: In jPCT, the nodes are overlapping if needed to avoid polygon splitting.
There's no method to tell the optimal depth/polygon count for a tree/node. It's a matter of trial and error to find that out for your particular mesh. I tend to keep the polygon count in each node at around 500-1000. Anything lower usually doesn't help performance wise because (as you've mentioned), there is an overhead in traversing the tree and if it becomes too high, it might eat up the benefits of the tree.
Using the tree for rendering is another story, because it increases the number of draw calls and that will eat up the performance gain pretty quickly.

Offline Raswr

  • byte
  • *
  • Posts: 13
    • View Profile
Re: Some questions about octrees & collision detection
« Reply #2 on: March 06, 2013, 06:29:24 pm »
Thanks Egon, I think I finally found good values for my mesh, by trial and error as you said, it's really really fast.

Thanks again :)