Is it possible to implement a custom Shape2D?

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

Hi,

I’m currently working on terrain generation, using a simple diamond square algorithm , that generates an Image (put_pixel) which is then loaded into TextureImage etc. This works fine, albeit a bit slow. But then there is collision detection.

Is it possible to implement custom Shape2D which the physicsserver can handle?

From looking at the Godot source code, it does not seem possible :-/

In general it would be nice to have a Node2D derivative, that has a Texture property, and a TextureShape2D that uses same said texture to do bitmap collision detection, calculate collision normal etc. (i.e. as done in Coding Destructible Pixel Terrain: How to Make Everything Explode | Envato Tuts+)

Of course it could currently be faked by using a rather large CollisionPolygon2D (screen.width + 2 points) but I’m not sure the engine would handle that very well or at all, and it would necessary to update the polygon every time part of the “bitmap” is destroyed/changed :slight_smile:

So what are your thoughts? any chance that we will se “bitmap” collision support in Godot?

:bust_in_silhouette: Reply From: dc1a0

Use Polygon2D, That allows you to pretty much draw the shape you want. CollisionPolygon2D for collision shape.

thx :slight_smile:

Works nicely with Polygon2D, but I can’t figure out how to use CollisionPolygon2D runtime (Which is designed to be an Editor helper, that partitions the polygon into shapes and updates its parent (CollisionObject2D)) and not as such usable via get_node etc.

I’ve tried instantiating it runtime, but it fails to trigger _update_parent() (protected method) which then calls _update_shapes_from_children() on the parent (which is not public available, https://github.com/godotengine/godot/blob/master/scene/2d/collision_object_2d.h)

I would hate to decompose the polygon my self :-/ Any suggestions?

UPDATE! I’m using the ConcavePolygonShape2D in the meanwhile, but I suspect better performance from several convex shapes (hence the decomposition need)

styf | 2016-03-04 21:05

You don’t use collisionpolygon runtime for that, you access the collisionbody and use add_shape(polygon)

example:

var polygon = polygon2d.new()
#then set the shape of polygon
Rigidbody2D.add_shape(polygon)

edit: since your description implies you’d be adding/and removing shapes, it might also be in your best interest to keep track of the shapes’ IDXs making it easier to add and remove them. I don’t have Godot up as I write this to get the commands needed, but they should be in Godot’s help still. I just know to remove, scale or check which shape got hit on a physicsbody, you need the IDX.

dc1a0 | 2016-03-04 21:38

Hi,

Yes that is what ended up doing, using ConcavePolygonShape2d. I just do a clear_shapes() followed by add_shape() works fine :slight_smile:

I would be nice thou to have access to the decomposition feature of CollisionPolygon2d, thus avoiding concave polygons all together and their inherit performance hit.

styf | 2016-03-04 23:26