关节-链条-绳索系统脚本使用指南(BreakableJointMonitor2D、RopePathBinder2D、ChainPathBinder2D、ChainJointSetupTool、ChainPartResource、JointBreakParams、AreaJointBreaker2D、RigidBodyPusherNatural)
chain_joints_scripts.zip (24.1 KB)
使用本脚本组,您可以监控和切断物理关节(PinJoint2D、GrooveJoint2D),并将绳索或链条的形状可视化为 Path2D。
您可以自由设置关节的断裂条件、链条的自动配置以及绳索的路径生成等功能。
0. 术语
|术语|含义|
| — | — |
|Joint|PinJoint2D 或 GrooveJoint2D(物理体之间的连接)|
|Monitor|BreakableJointMonitor2D(监控并判定关节断裂)|
|RopePathBinder|RopePathBinder2D(从绳索的节点序列生成 Path2D)|
|ChainPathBinder|ChainPathBinder2D(管理链条结构,并在断裂时分割路径)|
|ChainPart|ChainPartResource(链条部件信息:RigidBody2D 和 seg 节点)|
|SetupTool|ChainJointSetupTool(用于自动配置链条关节的工具)|
|AreaBreaker|AreaJointBreaker2D(批量切断 Area2D 范围内的 Joint2D)|
|JointBreakParams|JointBreakParams(每个关节的独立断裂参数)|
|seg节点|每个 RigidBody2D 内的节点标记(如 seg1, seg2 等)|
|RigidBodyPusher|RigidBodyPusherNatural(CharacterBody2D 推动 RigidBody2D)|
1. 配置(创建“可运行状态”)
1-1. 创建基本链条结构
目的
要使用链条系统,首先需要通过多个 RigidBody2D 进行连锁。您可以手动创建,也可以使用 ChainJointSetupTool 进行自动生成。
配置步骤(手动)
-
在场景中放置
Node2D(例如:ChainRoot) -
在其下放置多个
RigidBody2D(例如:Link1,Link2,Link3…) -
为每个 RigidBody2D 添加
CollisionShape2D和Sprite2D -
为每个 RigidBody2D 添加子节点
Node2D,并命名为seg1、seg2等(用作连接点)
配置步骤(自动:使用 ChainJointSetupTool)
-
在场景中放置
Node2D -
附加
chain_setup_tool.gd脚本 -
设置
target_parent为链条的父节点(如果未设置,则默认为该节点的父节点) -
在检查器中勾选
_setup_button,或调用setup_chain()方法 -
子级 RigidBody2D 将被自动检测,并生成关节和 seg 节点
在示例中
- 如果
auto_setup_all=true,将自动生成BreakableJointMonitor2D、ChainPathBinder2D、Path2D和PathRibbonMesh2D
1-2. 配置关节监控(BreakableJointMonitor2D)
目的
为了在关节满足特定条件时自动切断,请放置 BreakableJointMonitor2D 以开始监控。
配置步骤
-
在场景中放置
Node -
附加
breakable_joint_monitor_2d.gd脚本 -
设置
search_root为要监控的根节点(如果未设置,则默认为父节点) -
设置断裂条件(例如:
break_relative_speed、pin_break_error_pixels等) -
如果
auto_collect_on_ready=true(默认值),启动时将自动收集关节
在示例中
-
auto_collect_on_ready=true用于自动收集 -
recollect_after_break=true用于断裂后重新收集
1-3. 配置绳索/链条的可视化
目的
将物理运动的绳索或链条形状可视化为 Path2D。使用 RopePathBinder2D 或 ChainPathBinder2D。
配置步骤(RopePathBinder2D:用于绳索)
-
在场景中放置
Node -
附加
rope_solver.gd脚本 -
设置
source_mode:
-
NODEPATH_LIST:在point_node_paths中明确指定节点的 NodePath -
CHILDREN_RECURSIVE:从子节点递归自动检测(通过child_filter_mode指定类型)
-
将
Path2D添加到output_paths(如果为空,则自动生成) -
设置
auto_update=true以启用自动更新
配置步骤(ChainPathBinder2D:用于链条)
-
在场景中放置
Node -
附加
chain_path_binder.gd脚本 -
如果
auto_detect_mode=true,将自动检测search_root下的 RigidBody2D -
或者,手动将
ChainPartResource添加到chain_parts -
如果将
BreakableJointMonitor2D设置为joint_monitor,断裂时将自动分割路径 -
将
Path2D添加到output_paths(如果为空,则自动生成)
在示例中
auto_update=true、update_in_physics=true表示在物理帧中更新
1-4. 配置 CharacterBody2D 推动 RigidBody2D(RigidBodyPusherNatural)
目的
添加功能,使 CharacterBody2D 在通过 move_and_slide() 移动时,自然地推开接触的 RigidBody2D。
配置步骤
-
将
Node作为CharacterBody2D的子节点添加 -
附加
rigid_body_pusher.gd脚本 -
设置
pusher_physics_priority(设置较大的值以确保在move_and_slide()之后执行,默认值:1000) -
调整推力强度和质量的考量等参数
在示例中
-
use_force=true表示每帧施加力 -
consider_mass=true表示考虑质量 -
wake_rigid_bodies=true表示在接触时唤醒 RigidBody2D
1-5. 批量切断 Area2D 范围内的关节(AreaJointBreaker2D)
目的
此功能用于批量切断特定范围(Area2D)内的关节。可用于剑砍、爆炸等特效。
配置步骤
-
准备作为切断范围的
Area2D,并在其下添加CollisionShape2D -
将
area_joint_breaker.gd附加到任意节点 -
将上述
Area2D设置为target_area -
将
BreakableJointMonitor2D设置为monitor(可选,如果设置,则会触发joint_broken信号) -
调用
break_joints_in_target_area()
在示例中
- 在编辑器中勾选
_break_now_button即可执行
2. 运行(工作原理)
2-1. 关节的自动切断
处理流程
-
BreakableJointMonitor2D在_physics_process中监控每个关节 -
如果满足断裂条件,等待
break_delay_frames(或break_delay_sec) -
如果条件持续满足,则通过
joint.queue_free()切断关节,并发出joint_broken信号 -
如果
recollect_after_break=true,则在切断后重新收集关节
断裂条件示例
-
pin_break_error_pixels:锚点误差超过指定像素 -
break_relative_speed:相对速度超过指定值 -
break_stretch_ratio:相对于初始距离的拉伸率超过指定值 -
break_angle_degrees+break_angle_duration_sec:角度超过指定度数并持续一定时间
示例中的行为
- 当关节被拉伸或角度变大时,会自动切断
2-2. 绳索/链条的路径更新
处理流程
-
RopePathBinder2D或ChainPathBinder2D在_physics_process中收集节点序列 -
将每个节点的
global_position转换为Path2D的局部坐标 -
清除
Curve2D并添加新的点序列 -
可选应用重采样、平滑手柄和端点偏移
-
发出
paths_updated信号(供PathRibbonMesh2D等使用)
链条断裂时的自动分割
-
ChainPathBinder2D接收joint_broken信号 -
获取断裂关节的
body_a和body_b -
重建连接图并检测连通分量
-
为每个连通分量生成/更新
Path2D
示例中的行为
-
当绳索/链条物理运动时,
Path2D会跟随并可视化其形状 -
当关节断裂时,路径会自动分割
2-3. RigidBody2D 的推挤
处理流程
-
RigidBodyPusherNatural在_physics_process中执行(通过pusher_physics_priority控制执行顺序) -
通过父级
CharacterBody2D的get_slide_collision_count()获取碰撞信息 -
如果碰撞对象是
RigidBody2D,则计算推力 -
根据法线方向和移动方向的合成确定推力方向
-
通过
apply_central_force()或apply_central_impulse()施加力
示例中的行为
- 当
CharacterBody2D接触到箱子等RigidBody2D时,会自然地将其推开
3. 配置项(主要参数)
3-1. BreakableJointMonitor2D 的配置
|项目|目的(影响什么)|默认值|
| — | — | — |
|search_root|查找监控目标的根节点|父节点|
|break_relative_speed|相对速度超过此值时作为断裂候选|0.0(无效)|
|pin_break_error_pixels|锚点误差超过此像素值时断裂|0.0(无效)|
|break_stretch_ratio|相对于初始距离的拉伸率超过此值时断裂|0.0(无效)|
|break_stretch_duration_sec|持续拉伸超过此秒数时断裂|0.0(无效)|
|break_angle_degrees|角度超过此值并持续一定时间后断裂|0.0(无效)|
|break_angle_duration_sec|角度超过持续此秒数以上时断裂|0.5|
|use_break_delay_frames|将 BreakDelay 视为帧数(推荐)|true|
|break_delay_frames|超过条件连续此帧数时断裂|1|
|auto_collect_on_ready|启动时自动收集|true|
|recollect_after_break|断裂后自动重新收集|true|
补充(未断裂时的典型原因)
-
断裂条件的值为 0.0(无效)
-
break_delay_frames过大 -
search_root设置不正确 -
关节包含在
exclude_groups中
3-2. RopePathBinder2D / ChainPathBinder2D 的配置
|项目|目的(影响什么)|默认值|
| — | — | — |
|auto_update|自动更新(如果为 true,则每帧更新)|true|
|update_in_physics|在物理帧中更新|true|
|min_point_distance_px|不添加过于接近的点|0.0(无效)|
|max_points|最大点数|0(无限制)|
|resample_spacing_px|以固定间隔对点序列重采样|0.0(无效)|
|enable_smooth_handles|使 Curve2D 的手柄平滑|false|
|start_point_offset_local|起点偏移|Vector2(0, 0)|
|end_point_offset_local|终点偏移|Vector2(0, 0)|
补充(路径未更新时的典型原因)
-
auto_update=false -
output_paths为空 -
未收集到节点(检查
source_mode或search_root的设置)
3-3. ChainJointSetupTool 的配置
|项目|目的(影响什么)|默认值|
| — | — | — |
|target_parent|配置目标的父节点|父节点|
|joint_type|使用的关节类型(GROOVE/PIN)|GROOVE|
|groove_length|GrooveJoint2D 的长度|8.0|
|auto_create_seg_nodes|为每个 RigidBody2D 自动生成 seg 节点|true|
|auto_setup_all|自动生成和配置所有节点|true|
|setup_on_ready|运行时自动配置|false|
补充(配置失败时的典型原因)
-
target_parent下少于 2 个 RigidBody2D -
现有的关节未正确删除
3-4. RigidBodyPusherNatural 的配置
|项目|目的(影响什么)|默认值|
| — | — | — |
|pusher_physics_priority|物理处理的优先级(越大越靠后)|1000|
|require_owner_motion|仅当父节点移动时才推动|false|
|use_force|使用力方式(推荐 true)|true|
|push_strength|推力强度系数|100.0|
|min_push_force|最小推力|50.0|
|consider_mass|考虑质量|true|
|mass_influence|质量的影响程度(0.0-1.0)|0.5|
|normal_direction_weight|法线方向力的权重|0.7|
|movement_direction_weight|移动方向力的权重|0.3|
补充(无法推动时的典型原因)
-
require_owner_motion=true且父节点未移动 -
min_push_force过小 -
consider_mass=true且对方太重
4. 更改步骤(常见用途)
4-1. 调整关节的断裂条件
目的
使关节设置更敏感或更坚固。
步骤
-
选择
BreakableJointMonitor2D -
调整断裂条件的值:
-
更敏感:降低
break_relative_speed,将break_delay_frames设为 1 -
更坚固:提高
break_relative_speed,增加break_delay_frames
- 如果需要单独设置,请将
JointBreakParams资源添加到joint_params
单独设置示例
-
创建
JointBreakParams资源 -
在
joint_path中设置目标关节的 NodePath -
设置单独的断裂参数(-1.0 表示使用全局设置)
-
将其添加到
BreakableJointMonitor2D的joint_params中
4-2. 调整绳索/链条的外观
目的
调整路径的平滑度和点的密度。
步骤
-
选择
RopePathBinder2D或ChainPathBinder2D -
设置
resample_spacing_px,使点序列保持均匀间隔(例如:8.0) -
设置
enable_smooth_handles=true,使Curve2D更平滑 -
设置
min_point_distance_px,剔除过于接近的点
4-3. 增加链条的段数
目的
创建更长的链条。
步骤
-
在链条父节点下添加
RigidBody2D -
重新运行
ChainJointSetupTool的setup_chain() -
或者手动添加
GrooveJoint2D/PinJoint2D
4-4. 调整推挤强度
目的
更改 CharacterBody2D 推动 RigidBody2D 的强度。
步骤
-
选择
RigidBodyPusherNatural -
调整
push_strength(值越大,推力越强) -
调整
min_push_force(用于推动地面上的箱子) -
调整
mass_influence(接近 0.0 表示忽略质量)
5. 故障排除
|症状|主要原因|解决方法|
| — | — | — |
|关节不断裂|断裂条件为 0.0(无效)|将 break_relative_speed 等设置为有效值|
|关节立即断裂|断裂条件过于严格|提高 break_relative_speed,增加 break_delay_frames|
|路径未更新|auto_update=false|设置为 auto_update=true|
|路径未显示|output_paths 为空|添加 Path2D 或等待自动生成|
|链条未配置|RigidBody2D 少于 2 个|在父节点下放置 2 个以上的 RigidBody2D|
|推挤无效|require_owner_motion=true 且父节点静止|设置为 require_owner_motion=false 或移动父节点|
|Area2D 未切断|target_area 未设置|将 Area2D 设置为 target_area|
|单独设置无效|joint_path 不正确|重新设置 joint_path,或检查从 search_root 开始的相对路径|
6. 脚本概要
6-1. BreakableJointMonitor2D
BreakableJointMonitor2D 是一个 Node 脚本,用于监控场景中的 PinJoint2D 和 GrooveJoint2D,并在满足指定条件时自动切断它们。
特点
-
Pin/Groove 通用断裂条件(相对速度、拉伸率、角度、基于时间的拉伸)
-
Pin 专用断裂条件(锚点误差)
-
Groove 专用断裂条件(超出范围、端点拉伸)
-
每个关节的单独设置(
JointBreakParams) -
基于帧/基于秒的断裂延迟
-
自动收集和重新收集功能
前提和必要条件
-
假设使用 Godot 4 系列的 2D 节点结构
-
监控目标是
search_root下的PinJoint2D和GrooveJoint2D -
包含在
exclude_groups中的节点不会被监控
属性列表
|属性|类型 / 默认值|说明|
| — | — | — |
|search_root|NodePath / 空|查找监控目标的根节点|
|exclude_groups|Array[String] / []|从监控中排除的组名|
|break_relative_speed|float / 0.0|相对速度超过此值时作为断裂候选(0 表示无效)|
|pin_break_error_pixels|float / 0.0|锚点误差超过此像素值时断裂(0 表示无效)|
|break_stretch_ratio|float / 0.0|相对于初始距离的拉伸率超过此值时断裂(0 表示无效)|
|break_stretch_duration_sec|float / 0.0|持续拉伸超过此秒数时断裂(0 表示无效)|
|stretch_threshold_ratio|float / 1.1|拉伸判定阈值(相对于初始距离的比率)|
|break_angle_degrees|float / 0.0|角度超过此值并持续一定时间后断裂(0 表示无效)|
|break_angle_duration_sec|float / 0.5|角度超过持续此秒数以上时断裂|
|groove_break_out_of_range_pixels|float / 0.0|锚点位置超出沟槽范围时断裂(0 表示无效)|
|groove_break_end_pull_speed|float / 0.0|端点向外拉伸的相对速度超过此值时断裂(0 表示无效)|
|groove_end_epsilon_pixels|float / 2.0|端点判定的容差(像素)|
|joint_params|Array[JointBreakParams] / []|每个关节的单独设置|
|use_break_delay_frames|bool / true|将 BreakDelay 视为帧数(推荐)|
|break_delay_frames|int / 1|超过条件连续此帧数时断裂|
|break_delay_sec|float / 0.0|超过条件连续此秒数时断裂(旧方式)|
|over_time_decay_sec|float / 0.0|超过条件中断时的计数器衰减时间|
|auto_collect_on_ready|bool / true|启动时自动收集|
|recollect_interval_sec|float / 0.0|以固定间隔重新收集(0 表示不收集)|
|recollect_every_frame|bool / false|每帧强制重新收集|
|recollect_after_break|bool / true|断裂后自动重新收集|
|debug_print_eval|bool / false|输出判定日志|
信号
-
joint_broken(joint: Joint2D, reason: String):关节切断时发出 -
joint_registered(joint: Joint2D):关节注册时发出 -
joint_unregistered(joint: Joint2D):关节注销时发出
公开方法
-
collect_joints():手动收集关节 -
break_joint(joint: Joint2D, reason: String = "manual"):手动切断关节
6-2. RopePathBinder2D
RopePathBinder2D 是一个 Node 脚本,用于根据“绳索(节点序列)”的当前形状,每帧重建 Path2D 的 Curve2D。
特点
-
节点可以是任何
Node2D(RigidBody2D, Bone2D, CharacterBody2D, Marker2D 等) -
收集方法:显式 NodePath / 子节点递归搜索(类型过滤)
-
输出:更新多个 Path2D
-
点的剔除 / 简易手柄 / 固定间隔重采样
-
绳索断裂时的自动分割
前提和必要条件
-
节点必须是
Node2D -
Path2D必须添加到output_paths或自动生成
属性列表
|属性|类型 / 默认值|说明|
| — | — | — |
|output_paths|Array[Path2D] / []|生成/更新的 Path2D 数组|
|auto_update|bool / true|自动更新(如果为 true,则每帧更新)|
|update_in_physics|bool / true|在物理帧中更新|
|source_mode|enum / NODEPATH_LIST|NODEPATH_LIST / CHILDREN_RECURSIVE|
|point_node_paths|Array[NodePath] / []|当 source_mode==NODEPATH_LIST 时使用|
|child_filter_mode|enum / ANY_NODE2D|子节点搜索中的类型过滤|
|preserve_found_order|bool / true|保持子节点搜索中的顺序|
|search_root|Node / null|子节点搜索的基点|
|auto_split_on_break|bool / true|绳索断裂时自动分割路径|
|joint_monitor|BreakableJointMonitor2D / null|接收 joint_broken 信号|
|path_parent|Node / null|生成的 Path2D 的父节点|
|min_point_distance_px|float / 0.0|不添加过于接近的点(0.0 表示无效)|
|max_points|int / 0|最大点数(0 表示无限制)|
|start_point_offset_local|Vector2 / (0, 0)|起点偏移|
|end_point_offset_local|Vector2 / (0, 0)|终点偏移|
|resample_spacing_px|float / 0.0|以固定间隔对点序列重采样(0.0 表示无效)|
|enable_smooth_handles|bool / false|使 Curve2D 的手柄平滑|
|smooth_handle_strength_px|float / 12.0|手柄强度(像素)|
|weaken_end_handles|bool / true|减弱端点的手柄|
信号
paths_updated(paths: Array[Path2D]):路径更新时发出
公开方法
update_paths_from_rope():手动更新路径
6-3. ChainPathBinder2D
ChainPathBinder2D 是一个 Node 脚本,用于管理链条结构,并在关节断裂时自动分割路径。
特点
-
可以灵活设置每个链条部件(RigidBody)内的 seg 节点
-
自动检测模式(自动检测
search_root下的 RigidBody2D) -
构建关节连接图,并为每个连通分量生成路径
-
断裂时自动分割路径
前提和必要条件
-
链条部件由
ChainPartResource定义 -
如果
auto_detect_mode=true,将自动检测search_root下的 RigidBody2D
属性列表
|属性|类型 / 默认值|说明|
| — | — | — |
|auto_detect_mode|bool / false|自动检测模式(如果为 true,则自动检测)|
|search_root|Node / null|搜索起始节点|
|seg_node_prefix|String / "seg"|seg 节点的命名规则前缀|
|use_rigidbody_as_seg_when_no_seg|bool / true|如果没有 seg 节点,则使用 RigidBody2D 本身作为 seg|
|chain_parts|Array[ChainPartResource] / []|链条部件的数组|
|output_paths|Array[Path2D] / []|生成/更新的 Path2D 数组|
|auto_update|bool / true|自动更新|
|update_in_physics|bool / true|在物理帧中更新|
|joint_monitor|BreakableJointMonitor2D / null|接收 joint_broken 信号|
|path_parent|Node / null|生成的 Path2D 的父节点|
|ribbon_mesh|PathRibbonMesh2D / null|路径更新时直接调用函数|
|min_point_distance_px|float / 0.0|不添加过于接近的点|
|max_points|int / 0|最大点数|
|start_point_offset_local|Vector2 / (0, 0)|起点偏移|
|end_point_offset_local|Vector2 / (0, 0)|终点偏移|
|resample_spacing_px|float / 0.0|以固定间隔对点序列重采样|
|enable_smooth_handles|bool / false|使 Curve2D 的手柄平滑|
|smooth_handle_strength_px|float / 12.0|手柄强度|
|weaken_end_handles|bool / true|减弱端点的手柄|
公开方法
update_paths_from_chain():手动更新路径
6-4. ChainJointSetupTool
ChainJointSetupTool 是用于自动配置链条关节的工具。
特点
-
自动检测子级 RigidBody2D
-
连锁生成关节(GrooveJoint2D 或 PinJoint2D)
-
自动生成 seg 节点
-
自动生成和配置所有节点(BreakableJointMonitor2D、ChainPathBinder2D、Path2D、PathRibbonMesh2D)
前提和必要条件
-
target_parent下需要 2 个以上的 RigidBody2D -
由于是
@tool脚本,也可以在编辑器中执行
属性列表
|属性|类型 / 默认值|说明|
| — | — | — |
|target_parent|Node / null|配置目标的父节点|
|joint_type|enum / GROOVE|使用的关节类型(GROOVE/PIN)|
|groove_length|float / 8.0|GrooveJoint2D 的长度|
|groove_initial_offset|float / 1.0|GrooveJoint2D 的初始偏移|
|groove_joint_position_local|Vector2 / (0, -16)|GrooveJoint2D 的位置|
|pin_joint_position_local|Vector2 / (0, 0)|PinJoint2D 的位置|
|disable_collision|bool / false|禁用关节碰撞|
|bias|float / 0.9|关节的偏差|
|auto_create_seg_nodes|bool / true|为每个 RigidBody2D 自动生成 seg 节点|
|seg_node_prefix|String / "seg"|seg 节点的前缀|
|seg1_position_local|Vector2 / (0, -10)|seg1 的位置|
|seg2_position_local|Vector2 / (0, 10)|seg2 的位置|
|auto_setup_all|bool / true|自动生成和配置所有节点|
|auto_connect_breakable_monitor|bool / true|自动连接到 BreakableJointMonitor2D|
|breakable_monitor_path|NodePath / ""|BreakableJointMonitor2D 的路径|
|auto_setup_chain_binder|bool / true|自动配置 ChainPathBinder2D|
|chain_binder_path|NodePath / ""|ChainPathBinder2D 的路径|
|setup_on_ready|bool / false|运行时自动配置|
|_setup_button|bool / false|从检查器执行的按钮|
公开方法
setup_chain():配置链条
6-5. ChainPartResource
ChainPartResource 是用于保存链条部件信息的 Resource 类。
属性列表
|属性|类型 / 默认值|说明|
| — | — | — |
|rigid_body_path|NodePath / NodePath()|此链条部件的 RigidBody2D(NodePath)|
|seg_node_paths|Array[NodePath] / []|此部件内的 seg 节点(NodePath 数组)|
6-6. JointBreakParams
JointBreakParams 是用于保存每个关节的独立断裂参数的 Resource 类。
属性列表
|属性|类型 / 默认值|说明|
| — | — | — |
|joint_path|NodePath / NodePath()|目标关节(NodePath)|
|break_relative_speed|float / -1.0|相对速度(-1 表示使用全局设置)|
|pin_break_error_pixels|float / -1.0|锚点误差(-1 表示使用全局设置)|
|groove_break_out_of_range_pixels|float / -1.0|锚点位置超出沟槽范围(-1 表示使用全局设置)|
|groove_break_end_pull_speed|float / -1.0|端点向外拉伸的相对速度(-1 表示使用全局设置)|
|groove_end_epsilon_pixels|float / -1.0|端点判定的容差(-1 表示使用全局设置)|
|groove_break_stretch_ratio|float / -1.0|相对于初始距离的拉伸率(-1 表示使用全局设置)|
|groove_break_stretch_duration_sec|float / -1.0|持续拉伸超过此秒数时断裂(-1 表示使用全局设置)|
|groove_stretch_threshold_ratio|float / -1.0|拉伸判定阈值(-1 表示使用全局设置)|
|groove_break_angle_degrees|float / -1.0|角度超过此值并持续一定时间后断裂(-1 表示无效)|
|groove_break_angle_duration_sec|float / 0.5|角度超过持续此秒数以上时断裂|
6-7. AreaJointBreaker2D
AreaJointBreaker2D 是一个由“其他对象”执行的实用工具,用于批量切断指定 Area2D(其 CollisionShape2D)范围内的 Joint2D。
特点
-
如果存在 BreakableJointMonitor2D,则使用
monitor.break_joint()(推荐) -
如果不存在 monitor,则直接通过
joint.queue_free()删除 -
支持的形状:CircleShape2D、RectangleShape2D、CapsuleShape2D
前提和必要条件
-
必须将
Area2D设置为target_area -
Area2D的子级必须包含CollisionShape2D
属性列表
|属性|类型 / 默认值|说明|
| — | — | — |
|enabled|bool / true|启用/禁用|
|monitor|BreakableJointMonitor2D / null|BreakableJointMonitor2D 的引用(可选)|
|search_root|Node / null|扫描的根节点(如果未设置,则为整个场景)|
|target_area|Area2D / null|用作切断范围的 Area2D|
|target_collision_shape|CollisionShape2D / null|用于范围判定的 CollisionShape2D(如果未设置,则自动搜索)|
|include_pin_joint|bool / true|是否包含 PinJoint2D|
|include_groove_joint|bool / true|是否包含 GrooveJoint2D|
|include_other_joint2d|bool / true|是否包含其他 Joint2D|
|break_reason|String / "area_break"|切断理由(如果存在 monitor,则传递此值)|
|max_break_per_call|int / 0|每次执行切断的上限(0 表示无限制)|
|_break_now_button|bool / false|在编辑器中执行按钮|
公开方法
break_joints_in_target_area():切断 target_area 范围内的 Joint2D
6-8. RigidBodyPusherNatural
RigidBodyPusherNatural 是一个 Node 脚本,在父级 CharacterBody2D 的 move_and_slide() 之后执行,自然地推开接触的 RigidBody2D。
特点
-
可选仅当父级移动时才推动
-
可选择力方式/冲量方式
-
考虑质量的推挤
-
法线方向和移动方向的合成
前提和必要条件
-
父节点必须是
CharacterBody2D -
必须调用
move_and_slide()
属性列表
|属性|类型 / 默认值|说明|
| — | — | — |
|pusher_physics_priority|int / 1000|物理处理的优先级(越大越靠后)|
|require_owner_motion|bool / false|仅当父节点移动时才推动|
|min_owner_position_delta_pixels|float / 0.01|如果父级在一帧内的实际移动量小于此值,则不推动|
|min_owner_real_speed|float / 0.1|如果父级的实际速度小于此值,则不推动|
|use_force|bool / true|力方式(每帧用途推荐 force)|
|push_strength|float / 100.0|推力强度系数|
|min_push_force|float / 50.0|最小推力|
|max_force|float / 200000.0|力的上限(当 use_force=true 时)|
|max_impulse|float / 5000.0|冲量的上限(当 use_force=false 时)|
|consider_mass|bool / true|考虑质量|
|mass_influence|float / 0.5|质量的影响程度(0.0-1.0)|
|normal_direction_weight|float / 0.7|碰撞法线方向力的权重(0.0-1.0)|
|movement_direction_weight|float / 0.3|移动方向力的权重(0.0-1.0)|
|wake_rigid_bodies|bool / true|唤醒 RigidBody2D|
7. 使用示例
7-1. 基本链条创建
-
在场景中放置
Node2D(ChainRoot) -
在其下放置多个
RigidBody2D(Link1,Link2,Link3…) -
将
ChainJointSetupTool放置在ChainRoot的父级 -
将
target_parent设置为ChainRoot -
勾选
_setup_button -
关节和 seg 节点将自动生成
7-2. 关节的自动切断
-
在场景中放置
BreakableJointMonitor2D -
将
search_root设置为链条的根节点 -
设置断裂条件,例如
break_relative_speed=500.0 -
运行后,满足条件的关节将自动切断
7-3. 链条的可视化
-
在场景中放置
ChainPathBinder2D -
设置
auto_detect_mode=true -
将
search_root设置为链条的根节点 -
将
BreakableJointMonitor2D设置为joint_monitor -
放置
PathRibbonMesh2D,并将path_nodes设置为ChainPathBinder2D的output_paths -
运行后,链条的形状将被可视化,并在断裂时自动分割
7-4. CharacterBody2D 推动箱子
-
将
Node作为CharacterBody2D的子节点添加 -
附加
rigid_body_pusher.gd -
调整推力强度,例如
push_strength=100.0 -
运行后,
CharacterBody2D将自然地推开接触的RigidBody2D
7-5. 批量切断 Area2D 范围内的关节
-
准备作为切断范围的
Area2D,并在其下添加CollisionShape2D -
将
area_joint_breaker.gd附加到任意节点 -
将上述
Area2D设置为target_area -
将
BreakableJointMonitor2D设置为monitor(可选) -
调用
break_joints_in_target_area() -
范围内的关节将被批量切断
以上是关节-链条-绳索系统的使用指南。