使用PhysX作场景查询
PhysX Series: Scene Query
PhysX Series
这个系列主要记录一些最近在游戏中使用Nvdia PhysX 3.4物理引擎的一些经验。本文主要介绍场景查询的一些内部机制和使用方法。
Prerequisites
官方资料:
PhysX 3.4 source code
Download PhysXGuide.chm
A Scene
in PhysX engine is a container of objects in a hierachical manner.
There are only position and rotation in GlobalPose
and LocalPose
, no "scale". Scale only reflects on geometry's actual size.
PhysX Scene Query
Three kinds of scene query:
raycast
sweep
overlap
In general, each SceneQuery
traverses a culling structure containing the scene objects, performs a precise test using the GeometryQuery
functions, and accumulates the results.
You can customize filtering logic via prefilter
and postfilter
.
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
Pre-filtering happens before midphase and narrow phase and allows shapes to be efficiently discarded before the potentially expensive exact collision test.
Post-filtering happens after the narrow phase test and can therefore use the results of the test (such as PxRaycastHit.position) to determine whether a hit should be discarded or not.
More on traversal
traversal
A scene uses two query structures, one for "static" objects (PxRigidStatic
), one for "dynamic" objects (PxRigidBody
). Each structure can use different culling algorithms, see PxPruningStructureType
.
eNone
Based on grid. Full rebuild when changed.
eDYNAMIC_AABB_TREE
Based on tree. Full rebuild when changed. Only choose this if all static actors in your scene are not modified after initialization.
eSTATIC_AABB_TREE
Based on grid and tree. Incremental rebuild when changed, unless by force. Choose this if frequently add/remove geometry, at the cost of higher memory
More on prefilter
and postfilter
prefilter
and postfilter
To make prefilter
works, there are 3 steps.
first attach data (
PxFilterData
) for on shape. It has four 32bit words to hold custom data, e.g., useword0
as layer of this shape.
Here is an example:
second define callback function for
prefilter
. SeePxQueryFilterCallback
. The logic is totally depend on yourself, just returnPxQueryHitType
to tell if this shape can pass.
eNONE
Shall not pass.
eTOUCH
Pass, but does not stop the raycast or sweep.
eBLOCK
Pass, but stop here.
Here is an realworld example:
third step is to add
PxQueryFilterData
when query
PxQueryFilterData
has two fields:
PxQueryFlags
Supported flags are in PxQueryFlag::Enums
, e.g. raise ePREFILTER
means all shapes need to pass prefilter
you defined.
PxFilterData
Has four 32bit words for you, e.g. use word0
as the "layermask" of the query.
Here is an realworld example of raycast (return multiple objects):
Last updated