I would like to propose a feature to speed the creation of collisionpolygon2d.
A common case is to have an image with a transparent background and want to create a collisionpolygon2d which has the same shape as the outline of the image.
My proposal is a feature that to create a collisionpolygon from an image.
e.g. you can convert the image into a 2-color (black and transparent) and use the non-transparent color as shape to findup the vertices of the polygon.
AFAIK, they have similar plan for this at 2.1 release.
Editor #15: Create a Sprite -> Polygon Tool to optimize drawing of large objects (investigate algorithm for this)
http://www.godotengine.org/projects/godot-engine/roadmap
I'd like to say that Inkscape has this "Trace bitmap" option which I think it's relevant in some way. It might be more complicated then necessary cause Inkscape can vectorize shapes based on colors etc, not just alpha channel, but might be a good starting point for algorithm investigation... I was just thinking about this ability (lack of) in godot.
There are editors out there that have these features along with loaders/importers for popular game engines. The typical workflow is you open some images (or sprite sheet), trace them and tweak the number of vertices you want (too many will hurt performance) and once you are happy, you export the results. Then you have a loader script/plugin/class in your game that imports the data and create physics objects.
Of course for Godot we'd just want this feature integrated into CollisionPolygon2D editor as the data is all already connected to the game objects by Godot.
Physics Body Editor is one editor that does this and is open source, might be worth a look to get ideas for implementation. There are others out there, my personal favorite is PhysicsEditor - it is not open source however.
Note that with the addition of plugins in 2.1, it should be possible to make such a plugin for the assetlib :tada:
@bojidar-bg I'm not sure about that; the plugins will only be able to use GDScript; does GDScript have functionality to read images at a pixel level? I couldn't find any way to do that in the API. I think to do this in a plugin we are blocked on #3936. I would guess though that a feature like this could probably be accepted into the main code base.
edit: fix ref to 3936 rather than 3996
@jeremyjh Yes, it is possible, you would just use the Image::get_pixel
method.
+1 this feature would save me a huge amount of time drawing collision polygons. is there still interest in it? or is it an old abandoned idea?
I think there is some interest in this, though nobody has claimed the task yet...
bojidar-bg, what do you think would take for someone to claim the task? is there a process to follow? in the meantime i am trying to find out how to export for godot from the software jeremyjh mentioned, PhysicsEditor. hope i manage to get something useful out of it.
To use PhysicsEditor with Godot, someone would have to write a loader for Godot. PhysicsEditor can export to a generic XML format, but someone has to write then a plugin or module which reads that XML file and creates physics game objects. The PhysicsEditor author might do so if there were enough interested paying customers but that seems unlikely to me, because Godot has its own full-featured editor most people are not going to look outside it. So really the right answer here is the Godot plugin, which just takes an interested person with the time on their hands to do it.
i guess i would be willing to pay for a Godot plugin like that
finally! i managed to make a PhysicsEditor exporter for godot :) an early version of it at least
Hey @aombk, would you mind writing up how you got PhysicsEditor exports to work in Godot?
Can't seem to get this working - https://godotengine.org/qa/14191/using-physicseditors-outlines-with-lightoccluder2d
sure. i made this exporter:
https://iraisynn.attinom.net/demos/godotexporter.zip
you unzip it in C:\Program Files\CodeAndWeb\PhysicsEditor\resources\exporters , if i remember correctly
and it exports a godot xml scene file
Hey @aombk, thanks! How can we consume this godot xml scene file?
well this exporter creates a godot scene with a CollisionPolygon2D node
so in another scene, you add a StaticBody2D or RigidBody2D etc node and you instance the exported xml as a child of the body2D. and you add the sprite separately, as a child also. hope this makes sense
Yeah that makes sense, but I can't figure out how to import the xml as a scene. Any ideas? I keep getting "Error importing scene".
copy the xml in your game working directory and instead of clicking that + button, click that chain button next to it.
Beautiful, amazing, thank you! Just like you said, added a StaticBody2D, then the instance/chain button instanced the polygon2D
Doesn't work with more than one sprite though. I guess that's a limitation of using scenes/nodes
i dont understand what you mean by that.
for more than one sprite you need more than one StaticBody2D
take a look at these:
http://docs.godotengine.org/en/stable/learning/step_by_step/scenes_and_nodes.html
http://docs.godotengine.org/en/stable/learning/step_by_step/instancing.html
Yeah true, but PE allows importing in many sprites and exporting them all in one file. I think the other exports have json/xml with one entry per sprite, and then it can be parsed to find the correct one. This way works fine but you have to be careful to only export one sprite at a time and choose the correct one.
I've made an addon that converts Sprites to Polygon2Ds to cut off transparent areas.
https://github.com/timoschwarzer/polygonizer-gd-addon
Currently it uses the convex hull for the images, thus it's not suitable for CollisionPolygons in the most cases. And I think it's definitively a useful and needed feature for the engine.
hey timoschwarzer, i just saw your post. it didnt work for me in godot 2.1.4 , i couldnt find the polygonizer buttons. its a cool addon though. have you abandoned it or a re you planning to add support for godot 3 and more methods instead of convex hull? thanks
@aombk
The buttons are just above the 2D viewport.
I will make a version for 3.0 as soon as 3.0 has a stable release.
@timoschwarzer
yes i know where the buttons should be.
but they are not there for some reason.
@aombk
Have you enabled the Plugin? Which node do you have selected?
@timoschwarzer hello! are you planning to update your plugin for 3.0? I was considering rolling my own simplified version but then found this 馃憤
I've not planned an update for 3.0 because they have 2D meshes on the roadmap and I want to wait for them.
But feel free to make a PR for the repo: https://github.com/timoschwarzer/polygonizer-gd-addon
Should be easy to port it to 3.0 since the algorithm is the same.
This one may be of interest here: #20862
Since image-to-poly is already on 3.0, I backported, exposed and improved it.
It's pretty easy to generate collision polygons via script/plugin with that.
Exposition and improvements for 3.1 are done too, in another PR.
This was implemented in https://github.com/godotengine/godot/pull/27553, closing.
Most helpful comment
I've made an addon that converts Sprites to Polygon2Ds to cut off transparent areas.
https://github.com/timoschwarzer/polygonizer-gd-addon
Currently it uses the convex hull for the images, thus it's not suitable for CollisionPolygons in the most cases. And I think it's definitively a useful and needed feature for the engine.