Could I make a custom sprite "collidable"?

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

I’m making a game where the user can make and import a map, and then play it. The game is like a platformer mixed with a sandbox, and to make the map they get the background, the music, and a .png file that would be used to mark the collision.I don’t think using tilesets would help. Is there a script using CollisionPolygon2D that makes a collider in the same shape as the sprite? This is my first time using Godot, so I’m not that skilled, but I’m slowly learning! :smiley:

:bust_in_silhouette: Reply From: Tinkleberg

Hello hissapower,

I’m new at this myself but I think what you may be looking for is the “CollisionPolygon2D” node for your sprite.

If you add this as a child to the sprite node that you’ve imported into the game and draw the collision shape around your sprite with CollisionPolygon2D, you should get something like what you’re looking for.

As for using the .png file as the collision shape itself via code, that’s quite a bit more technical (as far as I currently know); but someone else here may be able to help you with that :slight_smile:

Here’s a guide I found on Godot Docs that has more information.

Hope that helps and good luck!

Thanks Tinkleberg, but that wasn’t what I needed. Although I’m really grateful that you tried to help, tilesets won’t combine with my game.But thanks, anyway! I tried to explain it with more details, if it helps! :slight_smile:

hissapower | 2017-06-13 00:55

:bust_in_silhouette: Reply From: eons

This is not something simple to do, you need to access the Image data, create an optimal polygon from that, then triangulate the polygon into convex shapes and add it to a CollisionObject2D (because concave shapes can be problematic).

I guess that you should be able to find generic algorithms for all the steps on different places and adapt it to use in Godot.

Do you know where could I learn to do all that? Like a video, or a page in Godot Docs?

hissapower | 2017-06-13 15:42

Nope :confused:

Is something too specific, you need to search for image processing, basic graphic editors can do that so there should be a known algorithm or technique (maybe edge detection?).

The polygon optimization, because you don’t want many points, is more math thing, triangulation too.


There is another option, if your game is not too complex it may work but you will have to do your own physics.

Check this https://twitter.com/bogdan_kustan/status/832993691325386752

eons | 2017-06-13 17:25