UE4中PhysX BroadPhase(碰撞检测的粗略阶段)
PhysX的BroadPhase(碰撞检测的粗略阶段),具体是用AABB(轴向包围盒)来做碰撞检测
具体算法有两种:SAP(Sweep And Prune)和MBP(Multi Pruning Box,多个剪枝盒)
SAP(Sweep And Prune)
当场景中有大量的物体(大世界有百万级别)时,即使它们已按AABB的三个轴向xyz做了排序,一个动态物体在移动过程与它们进行AABB判断也是一个很耗时操作
TG_EndPhysics的卡顿与BpSAP.UpdateWork直接相关
MBP(Multi Pruning Box,多个剪枝盒)
为了解决与物体判断AABB的次数,可以使用MBP来将整个场景划分成多个块,这样动态物体只用与当前所在块中的物体进行AABB判断,大大减少BroadPhase(碰撞检测的粗略阶段)的耗时
全局Default Broadphase Settings
注1:勾上Use MBPOn Client,并设置MBPBounds和MBPNum Subdivs的数值
注2:MBPBounds的Min、Max缺省为0,表示区域为整个场景
注3:MBPNum Subdivs为2,即将MBPBounds划分成2x2,一共4个块
该配置会保存在DefaultEngine.ini中
[/Script/Engine.PhysicsSettings] DefaultBroadphaseSettings=(bUseMBPOnClient=True,bUseMBPOnServer=False,bUseMBPOuterBounds=False,MBPBounds=(Min=(X=0.000000,Y=0.000000,Z=0.000000),Max=(X=0.000000,Y=0.000000,Z=0.000000),IsValid=0),MBPOuterBounds=(Min=(X=0.000000,Y=0.000000,Z=0.000000),Max=(X=0.000000,Y=0.000000,Z=0.000000),IsValid=0),MBPNumSubdivs=2)
在地图中可以覆写全局Default Broadphase Settings
具体代码详见:UnrealEngine\Engine\Source\Runtime\Engine\Private\PhysicsEngine\PhysScene_PhysX.cpp
void FPhysScene_PhysX::InitPhysScene(const AWorldSettings* Settings)
相关CVar变量
p.ForceMbpClient | Forces all created scenes to use MBP on client builds |
p.ForceMbpServer | Forces all created scenes to use MBP on server builds |
p.ForceNoKKPairs | Disables kinematic-kinematic pairs. This is required when using APEX destruction to correctly generate chunk pairs - when not using destruction this speeds up the broadphase by early rejecting KK pairs. |
p.ForceNoKSPairs | Disables kinematic-static pairs. This makes converting from static to dynamic a little slower - but provides better broadphase performance because we early reject those pairs. |
p.OverrideMbpNumSubdivisionsClient | Override for number of subdivisions to perform when building MBP regions on a client, note regions are only generated when a scene is created - this will not update the scene if it's already running (0 = No override, 1>16 - Override number) |
p.OverrideMbpNumSubdivisionsServer | Override for number of subdivisions to perform when building MBP regions on a server, note regions are only generated when a scene is created - this will not update the scene if it's already running (0 = No override, 1>16 - Override number) |
参考
Dive Into PhysX Broad Phase In UE4