Change collision shapes/polygons at runtime

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By timoschwarzer
:warning: Old Version Published before Godot 3 was released.

Hi there!

Is it possible to change collision shapes/polygons at runtime?

Best regards,
Timo

:bust_in_silhouette: Reply From: kubecz3k

Yes it’s possible, however modifying ShapeNode won’t work in this case (at current moment in 2.0 it’s just a helper for body.)
You can do this by using interface that is provided by CollisionBody object, you can use following methods:

void add_shape( Shape shape, Transform transform=Transform() )
int get_shape_count() 
void set_shape( int shape_idx, Shape shape )
void remove_shape( int shape_idx )
void clear_shapes()

Usually I’m doing this by adding every possible shape (that I might want to use) to the scene, and then in ready:

    #setting default shape
    self.clear_shapes();
    var defaultShape = shape get_node("DefaultShape").get_shape();
    self.add_shape(defaultShape);

This is the way how you can change collision shape of a body at any time in runtime.

At the moment I’m not 100% sure if it’s necessary to use get_shape() on ShapeNode → it’s very possible that you can use ShapeNode directly with add_shape

kubecz3k | 2016-04-10 17:41