codecat / godot-tbloader

TrenchBroom Loader for Godot 4. (Alternative to Qodot)
MIT License
240 stars 30 forks source link

Area3D brush creates ConcavePolygonShape3D which has issues for Areas #26

Closed makepanic closed 2 years ago

makepanic commented 2 years ago

Hi, thanks for building this great addon.

I've noticed that when having brush with a Area3D entity, it'll generate a ConcavePolygonShape3D as the collision shape. This is causing issues as mentioned in the docs:

Warning: Using this shape for an Area3D (via a CollisionShape3D node, created e.g. by using the Create Trimesh Collision Sibling option in the Mesh menu that appears when selecting a MeshInstance3D node) may give unexpected results: the area will only detect collisions with the triangle faces in the ConcavePolygonShape3D (and not with any "inside" of the shape, for example); moreover it will only detect all such collisions if backface_collision is true.

via https://docs.godotengine.org/en/latest/classes/class_concavepolygonshape3d.html

In my case that means I can only detect bodies which are on the edge of the brush but not inside of it. E.g. if I have a trigger brush that's a large rectangle.

I manually build the addon using ColliderShape::Convex instead of ColliderShape::Concave as collision shapes for CollisionObject3D and it fixes my issue.

I'm not sure if there are any issues which this will create. Can trenchbroom brushes create concave geometry?

If it's ok I can create a PR which changes the collider shape in https://github.com/codecat/godot-tbloader/blob/master/src/builder.cpp#L346-L350 to:

    } else if (node->is_class("CollisionObject3D")) {
        // If it's not a dynamic body, we can just use a convex trimesh collider
        need_collider = ColliderType::Mesh;
        need_collider_shape = ColliderShape::Convex;
    }

(tbh I'm not sure where exactly the code flows from the area3d brush conversion)

codecat commented 2 years ago

If the class name in TrenchBroom is set to area, it will use Builder::build_entity_area, which should correctly use a Concave collider shape. For custom entities however this behavior will probably indeed need to change.

In the code you highlighted, it should check for something like node->is_class("Area3D") and then change the needed collider shape from there. I'm open to a PR if doing that fixes it for you! 👍

makepanic commented 2 years ago

ok I've tried to do this in https://github.com/codecat/godot-tbloader/pull/27 and it fixes building the correct shape for Area3D entities