0 votes

A project I'm working relies on procedurally editing CollisionShapes. For debugging purposes, it is obviously very helpful if I can see where godot currently "thinks" each CollisionShape's boundary lies. As an example, I have a seen with 9 2x2 objects in it, each of which is composed of a StaticBody with two children, a 2x2x2 CubeMesh MeshInstance, and ConvexPolygonShape CollisionShape.

The CollisionShape's shape I set in a script. e.g., in a minimal working example:

func _ready():
    targetRB = $TestColliders/TestFloor
    targetCS = $TestColliders/TestFloor/CollisionShape
    var points = []
    points.append(Vector3(1.0, 1.0, 1.0))
    points.append(Vector3(1.0, 1.0, -1.0))
    points.append(Vector3(1.0, -1.0, 1.0))
    points.append(Vector3(-1.0, 1.0, 1.0))
    points.append(Vector3(-1.0, -1.0, -1.0))
    points.append(Vector3(-1.0, -1.0, 1.0))
    points.append(Vector3(-1.0, 1.0, -1.0))
    points.append(Vector3(1.0, -1.0, -1.0))
    targetRB.remove_child(targetCS)
    for i in range(len(targetCS.shape.points)):
        targetCS.shape.points[i] += points[i]# + 2.0*Vector3.BACK
    targetRB.add_child(targetCS)
    targetRB.translation += 1.0 * Vector3.BACK

This does appear to correctly change the CollisionShape data. If I uncomment the + 2.0*Vector3.BACK snippet and play this scene, and move a KinematicBody player around a bit, then I now fall through the space previously occupied by that CollisionShape, but can stand on what now looks like thin air if I move 2 units "back" in space.

Unfortunately, the "Visible Collision Shape" outline still stays in the same place it was at before I altered the CollisionShape's points in my script, so I now have to "just know" that the shape was moved (and have to correctly account for parenting relationships, rotation, scale, etc myself, which can be a bit tricky without a clear visual indicator to work with, especially as I haven't found much useful documentation on how these work internally, and some of the behavior has surprised me).

Curiously, I have noticed that this behavior isn't universal: if I save my CollisionShape as a separate scene and load it (with no node as its parent when loaded and instanced), change its shape in a script, and then, also by script, add it as a child of a StaticBody, then the Visible Collision Shape indicator appears to correctly update. I thought maybe I could exploit this to properly update the visual indicators by procedurally invoking remove_child (which is why this appears in the example script above), changing the collision shape, and then calling add_child again, but this doesn't seem to "reset" the visual indicator as I'd hoped. The behavior is the same whether I invoke these or leave them out: in either case, the CollisionShape data clearly alters (as reflected in moving the player around and running into the CollisionShape in its new location), but the visual indicator fails to reflect this.

Is there any way to force Visible Collision Shapes to re-render correctly each time they are updated, even when parented to other nodes? Or, failing that, any way to force them to visually update by un-parenting and re-parenting or something similar?

in Engine by (12 points)

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.