Dota 2 Workshop Tools/Scripting/API/Global.TraceCollideable=zh: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
(Created page with "{{Note | This page was previously automatically generated. Any changes may be overwritten}} Category:Dota2Function Category:Global == Function Description == ''' b...")
 
No edit summary
Line 8: Line 8:
''' bool TraceCollideable(table ''parameters'') '''
''' bool TraceCollideable(table ''parameters'') '''


Does a raytrace against a single solid entity. An extra bounding box centered around the entity can be added with the ''mins'' and ''maxs'' parameters.
对单个实体执行光线跟踪。可以使用“mins”和“maxs”参数添加以实体为中心的额外边界框。




;Example
;Example
<source lang="lua">
<source lang="lua">
-- Traces a line 100 units forward from the postion of the player, trying to find an entity named "my_prop".
-- 沿轨迹从玩家的位置向前追踪100个单位,试图找到一个名为“my_prop”的实体。
-- If the trace hits, a line is drawn to the hit position, along with the surface normal.
-- 如果命中,将沿曲面法线在命中位置绘制一条线。
local playerEnt = Entities:FindByClassname(nil, "player")
local playerEnt = Entities:FindByClassname(nil, "player")
local startVector = playerEnt:GetOrigin() + Vector(0, 0, 32)
local startVector = playerEnt:GetOrigin() + Vector(0, 0, 32)
Line 21: Line 21:
startpos = startVector;
startpos = startVector;
-- Traces from the player position 100 units in front of the player
-- 玩家前方100个单位的玩家位置
endpos = startVector + RotatePosition(Vector(0,0,0), playerEnt:GetAngles(), Vector(100, 0, 0));
endpos = startVector + RotatePosition(Vector(0,0,0), playerEnt:GetAngles(), Vector(100, 0, 0));
ent = Entities:FindByName(nil, "my_prop"); -- Traces the entity named "my_prop"
ent = Entities:FindByName(nil, "my_prop"); -- Traces the entity named "my_prop"
-- Adds an extra 20 x 20 x 20 bounding box on the model.
-- 在模型上添加一个额外的20 x 20 x 20边界体。
mins = Vector(-10, -10, -10 );
mins = Vector(-10, -10, -10 );
maxs = Vector(10, 10, 10 )
maxs = Vector(10, 10, 10 )
Line 41: Line 41:
</source>
</source>


== Parameters ==
== 参数 ==
{| class="standard-table" style="width: 50%;"
{| class="standard-table" style="width: 50%;"
! Type
! 类型
! Name
! 名字
! Description
! 介绍
|-
|-
| table
|
| parameters
| 参数
| Table used for both input and output.
| 用于输入和输出的表。
|}
|}


== Returns ==
== 返回 ==


''bool'' - Whether the trace is successful. This only indicates that the trace was done, not whether it hit anything.
''bool'' - 追踪是否成功。这仅表示追踪已完成,而不是是否击中任何东西。


== Input Parameters ==
== 输入参数 ==
Passed to the ''parameters'' table to set up the trace.
传递到参数表以设置跟踪。


{| class="standard-table" style="width: 50%;"
{| class="standard-table" style="width: 50%;"
! Type
! 类型
! Name
! 名字
! Description
! 介绍
|-
|-
| Vector
| Vector
| startpos
| startpos
| Global vector where to start the trace.
| 从指定全局向量处开始追踪。
|-
|-
| Vector
| Vector
| endpos
| endpos
| Global vector where to end the trace.
| 从指定全局向量处结束追踪。
|-
|-
| CBaseEntity
| CBaseEntity
| ent
| ent
| Entity to trace against.
| 欲追踪的实体。
|-
|-
| Vector
| Vector
| mins
| mins
| (Optional) Minimum coordinates of the bounding box. Local to the entity.
| (可选)边界体的最小坐标。实体本地向量。
|-
|-
| Vector
| Vector
| maxs
| maxs
| (Optional) Maximum coordinates of the bounding box. Local to the entity.
| (可选)边界体的最大坐标。实体本地向量。
|}
|}


== Output parameters ==
== Output parameters ==
Output written by the function to the ''parameters'' table.
被本函数添加到表中的成员。
{{Note | These parameters may not exist if the trace does not hit. Check for nil before using.}}
{{Note | 如果未命中,则这些参数可能不存在。使用前检查是否为空。}}


{| class="standard-table" style="width: 50%;"
{| class="standard-table" style="width: 50%;"
! Type
! 类型
! Name
!
! Description
! 介绍
|-
|-
| Vector
| Vector
| pos
| pos
| Global vector where the trace hit.
| 存储命中位置的全局向量。
|-
|-
| float
| float
| fraction
| fraction
| Fraction from the start to end where the trace hit.
| 在追踪轴上移动距离的百分比(?)
|-
|-
| bool
| bool
| hit
| hit
| Whether the trace hit something. Always present.
| 是否命中。始终会存在。
|-
|-
| bool
| bool
| startsolid
| startsolid
| Whether the trace started inside the entity. {{Note | This parameter is set to nil if it is false}}
| 跟踪是否在实体内部开始。{{Note | 如果为false,则此参数设置为 nil}}
|-
|-
| Vector
| Vector
| normal
| normal
| Global normal vector of the surface hit.
| 命中的曲面的全局法线向量。
|}
|}

Revision as of 04:38, 8 October 2023

Note.pngNote: This page was previously automatically generated. Any changes may be overwritten

Function Description

bool TraceCollideable(table parameters)

对单个实体执行光线跟踪。可以使用“mins”和“maxs”参数添加以实体为中心的额外边界框。


Example
-- 沿轨迹从玩家的位置向前追踪100个单位,试图找到一个名为“my_prop”的实体。
-- 如果命中,将沿曲面法线在命中位置绘制一条线。
local playerEnt = Entities:FindByClassname(nil, "player")
local startVector = playerEnt:GetOrigin() + Vector(0, 0, 32)
local traceTable =
{
	startpos = startVector;
	
	-- 玩家前方100个单位的玩家位置
	endpos = startVector + RotatePosition(Vector(0,0,0), playerEnt:GetAngles(), Vector(100, 0, 0));
	ent = Entities:FindByName(nil, "my_prop"); -- Traces the entity named "my_prop"
	
	-- 在模型上添加一个额外的20 x 20 x 20边界体。
	mins = Vector(-10, -10, -10 );
	maxs = Vector(10, 10, 10 )
}

TraceCollideable(traceTable)

if traceTable.hit 
then
	DebugDrawLine(traceTable.startpos, traceTable.pos, 0, 255, 0, false, 1)
	DebugDrawLine(traceTable.pos, traceTable.pos + traceTable.normal * 10, 0, 0, 255, false, 1)
else
	DebugDrawLine(traceTable.startpos, traceTable.endpos, 255, 0, 0, false, 1)
end

参数

类型 名字 介绍
参数 用于输入和输出的表。

返回

bool - 追踪是否成功。这仅表示追踪已完成,而不是是否击中任何东西。

输入参数

传递到参数表以设置跟踪。

类型 名字 介绍
Vector startpos 从指定全局向量处开始追踪。
Vector endpos 从指定全局向量处结束追踪。
CBaseEntity ent 欲追踪的实体。
Vector mins (可选)边界体的最小坐标。实体本地向量。
Vector maxs (可选)边界体的最大坐标。实体本地向量。

Output parameters

被本函数添加到表中的成员。

Note.pngNote: 如果未命中,则这些参数可能不存在。使用前检查是否为空。
类型 介绍
Vector pos 存储命中位置的全局向量。
float fraction 在追踪轴上移动距离的百分比(?)
bool hit 是否命中。始终会存在。
bool startsolid 跟踪是否在实体内部开始。
Note.pngNote: 如果为false,则此参数设置为 nil
Vector normal 命中的曲面的全局法线向量。