类型 函数 对象 粒子系统 库 physics.* 返回值 数组 修订 版本 2024.3703 关键词 rayCast,物理,LiquidFun 另请参见 physics.newParticleSystem() 对象:queryRegion()
此函数用于查找与直线碰撞的粒子。它返回一个描述每次碰撞的表格数组。
如果起始位置在粒子内部,则不会记录该粒子的碰撞。
ParticleSystem:rayCast( from_x, from_y, to_x, to_y, behavior )
数字。 射线的起始 **x** 位置。
数字。 射线的起始 **y** 位置。
数字。 射线的结束 **x** 位置。
数字。 射线的结束 **y** 位置。
字符串。 碰撞测试行为,按性能成本递增排序
"any"
— 返回一个结果,但不一定是最近的一个。"closest"
— 仅返回距起点最近的碰撞(如果有)。这是默认行为。"unsorted"
— 返回所有结果,无特定顺序。"sorted"
— 返回所有结果,按从最近到最远排序。hits
将是一个包含以下属性的元素数组
x
— 内容空间中的 **x** 碰撞位置。y
— 内容空间中的 **y** 碰撞位置。normal.x
— 局部空间中被碰撞表面法线的 **x** 分量。normal.y
— 局部空间中被碰撞表面法线的 **y** 分量。fraction
— 碰撞位于射线上的分数 (0
..1
)。0
是射线投射的起点,1
是终点。local hits = ParticleSystem:rayCast( 0, 0, 200, 300, "closest" ) if ( hits ) then -- There's at least one hit print( "Hit count: " .. tostring( #hits ) ) -- Output the results for i,v in ipairs( hits ) do print( "Hit: ", i, " Position: ", v.x, v.y, " Surface normal: ", v.normal.x, v.normal.y ) end print( "The first particle hit at position: ", hits[1].position.x, hits[1].position.y, " where the surface normal is: ", hits[1].normal.x, hits[1].normal.y, " and where the fraction along the ray is: ", hits[1].fraction ) else -- No hits on raycast end