物理引擎的空间数据结构

来源:互联网 发布:他改变了中国淘宝评论 编辑:程序博客网 时间:2024/05/28 15:18

PhysX document: Scene queries are performed in three phases: broad phase, midphase and narrow phase.
Broad phase traverses the global scene spatial partitioning structure to find the candidates for mid and narrow phases.
midphase traverses the triangle mesh and heightfield internal culling structures, to find a smaller subset of the triangles in a mesh reported by the broad phase.
Narrow phase performs exact intersection tests (ray test for raycast() queries, and exact sweep shape tests or overlap tests for sweep() and overlap() queries).

GDC2013上Gino van den Bergen的分享:Physics for Game Programmers: Spatial Data Structures

内容:1)物理引擎中的碰撞检测(collision query);2)用于加速碰撞检测的常用空间数据结构

物理引擎使用空间数据结构的首要目的就是要加速query,包括离散碰撞检测(discrete collision detection)、连续碰撞检测(continuous collision detection)、空间查询(scene query)

离散碰撞检测对某一时刻场景中所有contact的检测;连续碰撞检测为了更鲁棒的处理快速运动物体,防止tunneling;空间查询最基本的是raycast,physx还提供了sweep和overlap查询

碰撞检测过程

常用物理引擎一般约束运动的物体是convex(box、sphere、capsule、convex),复杂的静态物体可使用triangle mesh;convex-mesh碰撞检测可分为三个阶段:

  1. Broad phase: 可能发生碰撞的独立moving object pair
  2. Mid phase: 可能发生碰撞的primitive pair
  3. Narrow phase: 真正碰撞的primitive pair

Mid phase中,检测convex和mesh包含大量的primitive之间的潜在碰撞,需要利用这些primitive的spatial coherence,即:primitive只存在于一个很小的局部,与为数不多的其他primitive可能发生碰撞,并且这些primitive可以按照空间位置进行排序

空间数据结构就是利用这些primitive的spatial coherence来加速mid phase的,本质是基于spatial的分治法:直接做space partition,或者根据object的空间信息做model partition

各种空间数据结构

space partition:uniform grid,spatial hashing,bsp,kd-tree。。。
model partition:bv tree,aabb tree。。。

uniform grid, spatial hashing

uniform grid将空间简单粗暴的分为同样大小的voxel;优点是position和voxel自带对应关系,O(1)查询复杂度;并能直接相邻voxel;注意voxel的存储方式直接影响相邻voxel的存储局部性(zigzag方式会很好)

spatial hashing将这些voxel再hash到bucket中,直接支持无限大小空间;不过hash会导致很远的voxel存储到同一个bucket中,spatial coherence的利用率不高

这两种方式对大小相近的大量primitive有极大加成,例如cloth、water的simulation

BSP tree, kd-tree

BSP tree(binary space partition)每一层将空间用超平面划分为in-out两面,position根据超平面的隐式方程的值(bool plane::above(position))决定in还是out;kd-tree只是在bsp的基础上,将超平面限定为axis-aligned

可将待查询物体shrink成一个点,查询时根据物体大小动态的移动超平面(。。。)

Bounding Volume Hierarchy

BV要尽量贴近模型,并且贴近模型的开销要尽量小一般可用sphere、aabb、obb、k-dops

Binary AABB Tree,在贴近模型和存储开销的trade off下,选择AABB构造BVH,并不要求树的平衡

AABB Tree

物理引擎中主要使用binary AABB Tree

ray cast:自顶向下的遍历aabb tree,优先访问距离ray的源头近的子树;访问树节点过程是line segment-box测试,使用SAT测试线段和box的三个主轴以及主轴的相互cross

shape cast(convex sweep):访问树节点的过程是对节点AABB和shape的box的闵可夫斯基和进行ray cast

构造AABB tree:基于所有shape的AABB进行构造,一般使用自顶向下构造(较自下而上构造快);构造大AABB作为根节点;选择一个平面划分空间,和平面有交叉的shape放在平面的domonant side,将所有shape的aabb分配到两个可能overlap的子空间中;对每个子空间中所有shape的aabb递归构造AABB tree;在这个过程中划分平面的选择可以使用一些启发式算法

更新AABB tree:自底向上更新;每个树节点根据所有子节点的AABB计算自己的AABB;所有对于显著变化的场景往往直接重新构造AABB tree

Compressed AABB Trees:…

BoxTree:…


原创粉丝点击