Generating collision boxes based on images with transparency?
Posted: Thu Oct 25, 2018 11:37 am
Hi! I'm considering migrating my project to Love2D, and as part of evaluating it, I'm trying to find out if there's any built-in (doesn't look like it) or community library that supports generating collision meshes based on an image using its transparency. This could be used to auto-gen a collision mesh from just a normal image, or the way I intended to use it, using specific collision outline images (just a transparent image with a shape defined using any colored pixels).
CoronaSDK has this built-in (API reference), which was very convenient to use, and it's also using Box2D physics, so I thought it might have been a built in feature. But it doesn't seem like it.
If there's no convenient way to do this out of the box, does anyone have experience with implementing their own solution to do something like this?
I only need to do it once, when the game launches - and I'll probably cache the results as well for subsequent runs. But, I'm somewhat of a novice programmer so I'm not super clear on what a good way to approach this would be.
I think I can probably figure out how to outline an image based on transparency, using marching squares for example - but an outline created by that method would be waaaay too complex to use with Box2D as-is, right? So I need a way to simplify the "mesh" (polygon). I don't intend to use very complex outlines, on the order of a dozen vertices or so, usually.
I got this working well in Corona, but then ran into a ton of other problems since CoronaSDK is so focused on mobile devices, that I ran out of patience and wanted to try out Love2D instead. I'm super impressed with the amount of features I've seen, reading through the documentation - the only thing left of the list of things I need to support before I can confidently switch to Love2D for this project is this whole image-transparency-collision-thing.
It has to be done at run-time because the collision meshes are user-supplied via a file and images, as part of a custom user level system. Part of the specification for the custom user level format is that the user supplies their own collision mask image for the object in question, or if it's omitted, the game should try to auto-generate a collision mesh based on the graphical sprite used. Really, those two scenarios are basically the same, just that the collision mask is the preferred method (since it allows an object to specify a simplified collision mesh for a complex sprite graphic with lots of semi-transparent pixels etc).
I'm not really looking for suggestions saying "not" do this, it's a requirement for what I'm trying to do, and seeing as it's a feature in a slew of other frameworks and engines, I can't be the only one! Any help or pointers to good source material would be much appreciated!
CoronaSDK has this built-in (API reference), which was very convenient to use, and it's also using Box2D physics, so I thought it might have been a built in feature. But it doesn't seem like it.
If there's no convenient way to do this out of the box, does anyone have experience with implementing their own solution to do something like this?
I only need to do it once, when the game launches - and I'll probably cache the results as well for subsequent runs. But, I'm somewhat of a novice programmer so I'm not super clear on what a good way to approach this would be.
I think I can probably figure out how to outline an image based on transparency, using marching squares for example - but an outline created by that method would be waaaay too complex to use with Box2D as-is, right? So I need a way to simplify the "mesh" (polygon). I don't intend to use very complex outlines, on the order of a dozen vertices or so, usually.
I got this working well in Corona, but then ran into a ton of other problems since CoronaSDK is so focused on mobile devices, that I ran out of patience and wanted to try out Love2D instead. I'm super impressed with the amount of features I've seen, reading through the documentation - the only thing left of the list of things I need to support before I can confidently switch to Love2D for this project is this whole image-transparency-collision-thing.
It has to be done at run-time because the collision meshes are user-supplied via a file and images, as part of a custom user level system. Part of the specification for the custom user level format is that the user supplies their own collision mask image for the object in question, or if it's omitted, the game should try to auto-generate a collision mesh based on the graphical sprite used. Really, those two scenarios are basically the same, just that the collision mask is the preferred method (since it allows an object to specify a simplified collision mesh for a complex sprite graphic with lots of semi-transparent pixels etc).
I'm not really looking for suggestions saying "not" do this, it's a requirement for what I'm trying to do, and seeing as it's a feature in a slew of other frameworks and engines, I can't be the only one! Any help or pointers to good source material would be much appreciated!