碰撞体可以正常选择不同形状添加

This commit is contained in:
Hector 2026-02-26 15:21:18 +08:00
parent 19235ca43a
commit fe373c0e5d
3 changed files with 27 additions and 8 deletions

View File

@ -312,9 +312,14 @@ class CollisionManager:
# 获取考虑变换后的实际尺寸和中心
transformed_info = self._getTransformedModelInfo(model)
if not transformed_info:
# 默认小球体
return CollisionSphere(Point3(0, 0, 0), 1.0)
# 对于EmptyObject等无几何体的节点提供默认尺寸以便创建各种碰撞体
transformed_info = {
'center': Point3(0, 0, 0),
'radius': 1.0,
'size': Vec3(1.0, 1.0, 1.0),
'scale_factor': 1.0
}
center = transformed_info['center']
radius = transformed_info['radius']
actual_size = transformed_info['size']

View File

@ -1768,7 +1768,8 @@ class EditorPanels:
collision_shapes = ["球形 (Sphere)", "盒型 (Box)", "胶囊体 (Capsule)", "平面 (Plane)", "自动选择 (Auto)"]
# 获取当前形状
current_shape = self._get_current_collision_shape(node) if has_collision else "球形 (Sphere)"
current_shape = self._get_current_collision_shape(node) if has_collision else getattr(self.app, '_selected_collision_shape', "球形 (Sphere)")
if has_collision: self.app._selected_collision_shape = current_shape
# 形状选择下拉框
current_index = collision_shapes.index(current_shape) if current_shape in collision_shapes else 0
@ -1776,10 +1777,11 @@ class EditorPanels:
if changed:
# 始终更新选择的形状
selected_shape = collision_shapes[selected_index]
self._selected_collision_shape = selected_shape
self.app._selected_collision_shape = selected_shape
# 如果已经有碰撞体,询问用户是否要重新创建
if has_collision:
print(f"形状已更改为 {selected_shape},点击'移除碰撞''添加碰撞'来应用新形状")
self._remove_collision_from_node(node)
self._add_collision_to_node(node)
imgui.separator()

View File

@ -143,7 +143,19 @@ class PropertyHelpers:
if hasattr(child, 'getName') and child.getName():
name = child.getName()
if 'collision' in name.lower() or 'Collision' in name:
# 根据碰撞节点名称判断形状
if hasattr(child.node(), 'getSolids') and child.node().getNumSolids() > 0:
from panda3d.core import CollisionSphere, CollisionBox, CollisionCapsule, CollisionPlane
solid = child.node().getSolid(0)
if isinstance(solid, CollisionSphere):
return "球形 (Sphere)"
elif isinstance(solid, CollisionBox):
return "盒型 (Box)"
elif isinstance(solid, CollisionCapsule):
return "胶囊体 (Capsule)"
elif isinstance(solid, CollisionPlane):
return "平面 (Plane)"
# 兜底根据名称判断形状
if 'sphere' in name.lower():
return "球形 (Sphere)"
elif 'box' in name.lower():
@ -229,7 +241,7 @@ class PropertyHelpers:
return
# 获取选择的形状
shape_name = getattr(self, '_selected_collision_shape', '球形 (Sphere)')
shape_name = getattr(self.app, '_selected_collision_shape', '球形 (Sphere)')
if hasattr(self, 'collision_manager'):
# 使用碰撞管理器添加碰撞体