bepu / bepuphysics2

Pure C# 3D real time physics simulation library, now with a higher version number.
Apache License 2.0
2.35k stars 272 forks source link

General Questions #91

Closed CharlesWoodhill closed 4 years ago

CharlesWoodhill commented 4 years ago

hi, ive used many game engines in the past, not often used phys but ive some basic understanding of the pengines "apis". given that ive some questions. i know its not bepu-specific but i hope u can/want answer me anyway :) im posting here because the forum dont want my email adress.

1. game-engines normally give u the option to add a collider shape (box, mesh,....) so i think its that what pengines provide, but why isnt that "hirarchical" or at least dual? its obivous that a mesh collider is more expensive than a box. given a genine entity with a mesh and its (aa and/or not aa)bounding box, why am i need to use a mesh collider, cant i use 2 colliders where the first (box) is checked and only if its hit then check the 2nd (mesh)? or is this done "implicit" in the pengine? it seems unnecessary and/or loss of information(boundingbox) that the pengine then has to caluclate again, also it would be cumbersome to create custom broad/narrow phase implementations just to achive this...

2. i want to implement some sort of spatialchunking, its planned to let every chunk be maintained by one or more threads or even servers. gengines iirc only support 1 pengine.simulation and rely on some sort of grouping. but doesnt it cost performance because one has to check the groups? wouldnt it be better to use more simulations (ofc with some sort of shadow entities on the borders)? example: 1 pengine.simulation with 1000 objects in 3 groups still need a check of 1000x1000 to check the group flags, which is 1.000.000 loops over something. 3 engines .simulations with 333 each and lets say + 67 (shadowed) objects = 400 objects only need 3x400x400 (short pause) that is 480.000 loops but could be solved in 3 threads so 160.000 only per thread. it also has the bonus of beeing scaleable and so (given enuf horsepower) would support nearly infinite objects without a quadratic increase of pair checks. why group flags are used and not multiple simulations? is it wise to split into multiple simulations for space chunking on one machine?

  1. why (spatial)octrees arent used in penines (or gengines)?

i hope this questions are not too nooby, tnx in advance :)

RossNordby commented 4 years ago

game-engines normally give u the option to add a collider shape (box, mesh,....) so i think its that what pengines provide, but why isnt that "hirarchical" or at least dual?

Physics engines in general already use acceleration structures, like trees. So when you add a mesh, it is added to a broad phase acceleration structure which considers only its bounding box. Only objects with an overlapping bounding box will be tested in greater detail. Things like meshes or large compounds then have their own acceleration structures, so you don't have to test an incoming body against all 50000 triangles in a mesh either. That is all done automatically.

i want to implement some sort of spatialchunking

Splitting a simulation can be useful, but not in the way you are thinking. If you have 1000 objects in a simulation, it will not do a ~million loop iterations unless every object is overlapping every other object. "Collision groups" or "collision masks" usually refer to filtering options you can apply to broad phase detected overlaps to reject pairs or constrain their evaluation. The filter will not be used for pairs that do not have overlapping bounds.

Some engines, like bepuphysics, also already make use of multithreading and can scale up to many cores (limited usually by memory bandwidth in the solver). On consumer grade hardware it can easily handle thousands of active bodies interacting. Distributing the simulation across multiple servers can indeed help, but to be truly useful, the simulation would need to be relatively sparse (so that it's easier to partition) and have too many objects for a single computer to handle (otherwise the extra complexity is totally pointless).

why (spatial)octrees arent used in penines (or gengines)?

Spatial octrees in particular can be useful in cases where they fit the underlying data well, like sparse voxel grids. They usually underperform some alternatives when you have less guarantees about the data.

Bepuphysics uses a binary bounding volume hierarchy incrementally refined with a surface area heuristic subtree builder. The hierarchy tightly adheres to the actual bounds of objects in the simulation and doesn't assume anything about their distribution, so it tends to produce reasonably high performance trees no matter how things are laid out.

CharlesWoodhill commented 4 years ago

Thank u for this very nice explanations.