对象:rayCast()

类型 函数
对象 粒子系统
physics.*
返回值 数组
修订 版本 2024.3703
关键词 rayCast,物理,LiquidFun
另请参见 physics.newParticleSystem()
对象:queryRegion()

概述

此函数用于查找与直线碰撞的粒子。它返回一个描述每次碰撞的表格数组

注意事项

如果起始位置在粒子内部,则不会记录该粒子的碰撞。

语法

ParticleSystem:rayCast( from_x, from_y, to_x, to_y, behavior )
from_x (必填)

数字 射线的起始 **x** 位置。

from_y (必填)

数字 射线的起始 **y** 位置。

to_x (必填)

数字 射线的结束 **x** 位置。

to_y (必填)

数字 射线的结束 **y** 位置。

behavior (可选)

字符串 碰撞测试行为,按性能成本递增排序

  • "any" — 返回一个结果,但不一定是最近的一个。
  • "closest" — 仅返回距起点最近的碰撞(如果有)。这是默认行为。
  • "unsorted" — 返回所有结果,无特定顺序。
  • "sorted" — 返回所有结果,按从最近到最远排序。

结果属性

hits 将是一个包含以下属性的元素数组

示例

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