Defold Polygon Editor

You’re a god. Game ideas are pouring out my brain because of this. Thank you XD. Ill probably use this sometime in the future after getting comfortable with using Defold.

5 Likes

@ross.grams hey! thanks for your editor!
any idea how to add the polygon figure with internal angles? like this? split into 3 figures?
image

Yeah, that would have to be split up. All shapes must be convex like a ball “( )”. Any concave shapes, like the inside of a bowl " ) ( ", doesn’t work for collision.

4 Likes

Yep, what Mattias said. But if you want to create a dynamic collision object with that shape, as far as I know it is not possible. Polygons are treated differently from other collision shapes, and you can only use one per collision object. If you want a static, kinematic, or trigger body this shouldn’t be a big problem (you just add multiple collision object components), but with dynamic bodies each component will be a separate object, so you can’t have multiple polygons “welded” together. You can use one polygon and multiple “normal” collision shapes though.

2 Likes

@Mattias_Hedberg , @ross.grams okay, understand. thanks!

Finally used this in a project. I may have a pull request in the future with some new features…

I highly recommend people use this tool if they need shapes which they are tempted to make from combining multiple premade collision shapes together. Previously on a project I was using a box and a circle to make a pill like shape, and had some bugs for a while I didn’t know the cause. Turns out it was the box shape’s corners causing the bugs. Instead making a water tight custom shape with this tool fixed the issues.

5 Likes

Would this shape ever be possible, even if breaking it up into multiple collision shapes?

In Box2D a chain shape would work, because those don’t have the limitations of bodies made up of polygon shapes. Are chain shapes supported by Defold? I’m guessing not.

Maybe a Tile Map could be used instead?

You can make any shape you want if you break it up into pieces. This tool is really, really simple and doesn’t do that for you though. Depending on how precise you want it, you could probably do this with 4 shapes. Though if you want to use it for a dynamic body, as far as I know Defold doesn’t let you have multiple polygons on a single Collision Object, so you can’t really make dynamic bodies with multiple polygons.

Tile Maps only do convex collision as well, though if you split this up over multiple tiles, that -might- be easier than using polygons. I’m not sure how well that would work, but it’s worth a try.

3 Likes

For this project it looks like Tile Map is out:

17

The result is a bit too uneven and unpredictable. Maybe a different approach entirely is the way to go, by somehow calculating the y collision of the character. The mechanic is similar to Tiny Wings and those types of games.

2 Likes

Ah, yeah. For that kind of game I think a more mathematical approach would be better. Use a Bezier curve and do the math for a body on an inclined plane yourself.


[Edit] Actually, the physics part would only be a few lines of code. The trickier parts would be:

  1. Find a good Bezier curve Lua library and figure out how to use it, (or write one yourself…).
  2. Get the slope of the curve at the X-position of your character.
  3. Figure out how to draw the ground to match the curve. (put the Bezier math into a shader and stretch/skew a texture to fit?)
2 Likes

Would be cool if the editor could automatically divide and generate sets of ready to use collision shapes for concave bodies maybe even with a go with all of the shapes defined too. Anyone out there could add support for this…

Doesn’t have to be automatic, could add cut ability between points and then mark the remaining concave subshapes.

2 Likes

Alternatively, load a JSON file spat out by RUBE! It divides shapes into separate convex fixtures automatically.

1 Like

Can’t RUBE export custom formats?

Yeah, this solution sounds like a can of worms (to me). I was hoping to be able to stick to standard Defold objects, so I can visualise it in the amazing IDE. Trying the “many collisions shapes” idea now, praying it will be good enough. :slight_smile:

It can export to JSON or C code.

Ok lovely people, I’ve created a VERY rudimentary bridge between RUBE and Defold. Maybe this could be useful for reference when integrating a more robust polygon support in Defold, or maybe even offer RUBE support.

First, a video of the result:

This uses collision data from a RUBE JSON file:
test.json.zip (1.3 KB)

I wrote a small Lua script to extract the data and print it in the right format:

    local data = sys.load_resource("/json/test.json")
	local data_table = json.decode(data)
	local bodies = data_table.body

	for b=1, #bodies do
		
		local body = bodies[b]
		local fixtures = body.fixture

		for f=1, #fixtures do

			print( "shape_type: TYPE_HULL" )
			
			local fixture = fixtures[f]
			local polygon = fixture.polygon
			local vertices = polygon.vertices
			local xs = vertices.x
			local ys = vertices.y
			
			for v=1, #xs do

				local x = xs[v]
				local y = ys[v]
				print( "data: ", x )
				print( "data: ", y )
				print( "data: 0.0" )	
							
			end

			print()
			
		end

	end

Then I added the 15 (!) collision shapes to the game object, each file containing a convex polygon, making up the final concave polygon:

Now I need a coffee.

The test project zipped: PolygonTest.zip (75.6 KB)

13 Likes

Well-deserved, amazing work :raised_hands:

3 Likes

In terms of performance, could there be an issue with the above method of using concave polys? It’s preferable to me, because even though it takes a bit of faff to get going it gives complete freedom of how the various shapes can look (not just bezier).

1 Like

I made an equally rustic bridge between PhysicsEditor by Code and Web, which saves a bit of time because of their automated shape detection. The result:

10 Likes

In small numbers, not really an issue. Ultimately it’s all about what kind of device you want to target though.

2 Likes

A defold tool written in defold, mind blown! Looks awesome!

4 Likes