如何在Godot引擎中获取bottomCenter等边缘中心的坐标?
在Godot引擎中获取节点边缘中心坐标的方法
2D节点场景
对于Sprite2D、ColorRect这类2D节点,直接利用节点的尺寸和位置属性就能快速计算边缘中心:
- 底部中心(bottomCenter):
rect_position + Vector2(rect_size.x / 2, rect_size.y) - 顶部中心(topCenter):
rect_position + Vector2(rect_size.x / 2, 0) - 左侧中心(leftCenter):
rect_position + Vector2(0, rect_size.y / 2) - 右侧中心(rightCenter):
rect_position + Vector2(rect_size.x, rect_size.y / 2)
如果需要全局坐标的边缘中心,把rect_position换成get_global_position()即可,比如:get_global_position() + Vector2(rect_size.x / 2, rect_size.y)
3D节点场景
3D节点需要先获取包围盒(AABB)再计算,以MeshInstance3D为例:
- 先获取节点的AABB:
var aabb = get_aabb() - 基于包围盒计算各边缘中心(默认Y轴向上、X轴向右):
- 底部中心(bottomCenter):
aabb.position + Vector3(aabb.size.x/2, 0, aabb.size.z/2) - 顶部中心(topCenter):
aabb.position + Vector3(aabb.size.x/2, aabb.size.y, aabb.size.z/2) - 左侧中心(leftCenter):
aabb.position + Vector3(0, aabb.size.y/2, aabb.size.z/2) - 右侧中心(rightCenter):
aabb.position + Vector3(aabb.size.x, aabb.size.y/2, aabb.size.z/2)
- 底部中心(bottomCenter):
要转换为全局坐标,用全局变换矩阵相乘:global_transform * bottom_center
提示:节点的缩放会自动体现在AABB的尺寸中,无需额外处理缩放值。
内容的提问来源于stack exchange,提问作者Legend589 Gamer
相关产品推荐
相关产品推荐

