+2 votes

Hi there!

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

Best regards,
Timo

in Engine by (698 points)

1 Answer

+3 votes
Best answer

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.

by (1,281 points)
selected by

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

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.