<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[PhysicsEditor Exporter for Defold &amp; Box2D NE]]></title><description><![CDATA[<p><a href="https://www.codeandweb.com/physicseditor" rel="nofollow">PhysicsEditor</a> 并不带有 Defold 的 Exporter, 为了方便使用我就自己写了一个.<br />
配合 <a href="https://github.com/d954mas/defold-box2d" rel="nofollow">Box2D Native Extension</a> 使用.</p>
<p><img src="/assets/uploads/files/1667490389784-8cbdfc59-a98b-4e4d-9442-d21503b87f00-image.png" alt="0_1667490387913_8cbdfc59-a98b-4e4d-9442-d21503b87f00-image.png" class="img-responsive img-markdown" /></p>
<p>defold.lua</p>
<pre><code>-- This file is for use with Defold &amp; Box2D native extension
--
-- Usage example:
--			local scaleFactor = 0.04
--			local physicsData = (require &quot;shapedefs&quot;).physicsData(scaleFactor)
--          local def = physicsData:get(&quot;defname&quot;)
--			for i, v in ipairs(def) do
--		        body:CreateFixture(v)
--	        end
--

-- copy needed functions to local scope
local unpack = unpack
local pairs = pairs
local ipairs = ipairs

local M = {}

function M.physicsData(scale)
	local physics = { data =
	{ {% for body in bodies %}
		{% if not forloop.first %}, {% endif %}
		[&quot;{{body.name}}&quot;] = {
                    {% for fixture in body.fixtures %}
                    {% if not forloop.first %} ,{% endif %}
                    {% if fixture.isCircle %}
                    {
                    density = {{fixture.density}}, friction = {{fixture.friction}}, restitution = {{fixture.restitution}}, {% if fixture.isSensor %}isSensor=true, {% endif %}
                    filter = { categoryBits = {{fixture.filter_categoryBits}}, maskBits = {{fixture.filter_maskBits}}, groupIndex = {{fixture.filter_groupIndex}} },
                    shape = {shape = 0, circle_radius = {{fixture.radius|floatformat:3}}}
                    }
                    {% else %}
                    {% for polygon in fixture.polygons %}{% if not forloop.first %} ,{% endif %}
                    {
                    density = {{fixture.density}}, friction = {{fixture.friction}}, restitution = {{fixture.restitution}}, {% if fixture.isSensor %}isSensor=true, {% endif %}
                    filter = { categoryBits = {{fixture.filter_categoryBits}}, maskBits = {{fixture.filter_maskBits}}, groupIndex = {{fixture.filter_groupIndex}} },
                    shape ={shape = 2, polygon_vertices = { {% for point in polygon %} {% if not forloop.first %}, {% endif %} vmath.vector3({{point.x}}, {{point.y}}, 0) {% endfor %} }}
                    }
                    {% endfor %}
                    {% endif %}
                    {% endfor %}
		}
		{% endfor %}
	} }

        -- apply scale factor
        local s = scale or 0.04
        for bi,body in pairs(physics.data) do
                for fi,fixture in ipairs(body) do
                    if(fixture.shape.polygon_vertices) then
                        for ci,coordinate in ipairs(fixture.shape.polygon_vertices) do
                            fixture.shape.polygon_vertices[ci] = s * coordinate
                        end
                    else
                        fixture.shape.circle_radius = s * fixture.shape.circle_radius
                    end
                end
        end
	
	function physics:get(name)
		return self.data[name]
	end

	return physics;
end

return M


</code></pre>
<p>exporter.xml</p>
<pre><code>&lt;exporter&gt;
	&lt;name&gt;defold-box2d&lt;/name&gt;
        &lt;displayName&gt;Defold + Box2D NE&lt;/displayName&gt;
        &lt;description&gt;Exporter for Defold-Box2D, lua&lt;/description&gt;
        &lt;version&gt;1.0&lt;/version&gt;
	&lt;yAxisDirection&gt;up&lt;/yAxisDirection&gt;
	&lt;physicsEngine&gt;box2d&lt;/physicsEngine&gt;
	&lt;template&gt;defold.lua&lt;/template&gt;
	&lt;fileExtension&gt;lua&lt;/fileExtension&gt;
	&lt;anchorPoint&gt;
		&lt;enabled&gt;no&lt;/enabled&gt;
	&lt;/anchorPoint&gt;
	&lt;origin&gt;
        &lt;type&gt;fixed&lt;/type&gt;
	    &lt;relX&gt;0.5&lt;/relX&gt;
		&lt;relY&gt;0.5&lt;/relY&gt;
    &lt;/origin&gt;
    &lt;!-- Circle support does not work as with standard box2d - you can't change the center
         or add more than one circle to a shape. This is why it is disabled for now --&gt;
    &lt;supportsCircles&gt;yes&lt;/supportsCircles&gt;
	&lt;body&gt;
	&lt;/body&gt;
	&lt;global&gt;
	&lt;/global&gt;
	&lt;body&gt;
	&lt;/body&gt;
	&lt;fixture&gt;
		&lt;parameter&gt;
			&lt;name&gt;density&lt;/name&gt;
			&lt;displayName&gt;Density&lt;/displayName&gt;
			&lt;type&gt;float&lt;/type&gt;
			&lt;min&gt;-1000&lt;/min&gt;
			&lt;max&gt;1000&lt;/max&gt;
			&lt;default&gt;2.0&lt;/default&gt;
		&lt;/parameter&gt;
		&lt;parameter&gt;
			&lt;name&gt;restitution&lt;/name&gt;
			&lt;displayName&gt;Restitution&lt;/displayName&gt;
			&lt;type&gt;float&lt;/type&gt;
			&lt;min&gt;0&lt;/min&gt;
			&lt;max&gt;1&lt;/max&gt;
			&lt;default&gt;0.0&lt;/default&gt;
		&lt;/parameter&gt;
		&lt;parameter&gt;
			&lt;name&gt;friction&lt;/name&gt;
			&lt;displayName&gt;Friction&lt;/displayName&gt;
			&lt;type&gt;float&lt;/type&gt;
			&lt;min&gt;0&lt;/min&gt;
			&lt;max&gt;1000&lt;/max&gt;
			&lt;default&gt;0.0&lt;/default&gt;
		&lt;/parameter&gt;
        &lt;parameter&gt;
            &lt;name&gt;isSensor&lt;/name&gt;
            &lt;displayName&gt;Is Sensor&lt;/displayName&gt;
            &lt;description&gt;If set the physial &lt;/description&gt;
            &lt;type&gt;bool&lt;/type&gt;
            &lt;default&gt;false&lt;/default&gt;
        &lt;/parameter&gt;

        &lt;parameter&gt;
            &lt;name&gt;filter_groupIndex&lt;/name&gt;
            &lt;displayName&gt;Group&lt;/displayName&gt;
            &lt;description&gt;Collision group.&lt;/description&gt;
            &lt;shortDescription&gt;&lt;/shortDescription&gt;
            &lt;type&gt;int&lt;/type&gt;
            &lt;default&gt;0&lt;/default&gt;
        &lt;/parameter&gt;

        &lt;parameter&gt;
            &lt;name&gt;filter_bitfield&lt;/name&gt;
            &lt;type&gt;bitfield&lt;/type&gt;
            &lt;size&gt;16&lt;/size&gt;
        &lt;/parameter&gt;

        &lt;parameter&gt;
            &lt;name&gt;filter_categoryBits&lt;/name&gt;
            &lt;displayName&gt;Cat.&lt;/displayName&gt;
            &lt;description&gt;Collision category&lt;/description&gt;
            &lt;shortDescription&gt;Collision category&lt;/shortDescription&gt;
            &lt;type&gt;uint16&lt;/type&gt;
            &lt;default&gt;1&lt;/default&gt;
            &lt;bitfield&gt;yes&lt;/bitfield&gt;
        &lt;/parameter&gt;
        &lt;parameter&gt;
            &lt;name&gt;filter_maskBits&lt;/name&gt;
            &lt;displayName&gt;Mask&lt;/displayName&gt;
            &lt;description&gt;Collision mask&lt;/description&gt;
            &lt;shortDescription&gt;Collision mask&lt;/shortDescription&gt;
            &lt;type&gt;uint16&lt;/type&gt;
            &lt;default&gt;65535&lt;/default&gt;
            &lt;bitfield&gt;yes&lt;/bitfield&gt;
        &lt;/parameter&gt;
        &lt;/fixture&gt;
&lt;/exporter&gt;

</code></pre>
<p><img src="/assets/uploads/files/1667490411972-4ca50a6c-cf2e-4ac4-a6bb-8e25e89630fa-image.png" alt="0_1667490410510_4ca50a6c-cf2e-4ac4-a6bb-8e25e89630fa-image.png" class="img-responsive img-markdown" /></p>
<p><img src="/assets/uploads/files/1667490423039-bb5df96d-ce19-469e-a625-647c8c5e4438-image.png" alt="0_1667490420923_bb5df96d-ce19-469e-a625-647c8c5e4438-image.png" class="img-responsive img-markdown" /></p>
<p>FYI, 如果 body:CreateFixture() 支持 table 做参数就更好了.</p>
]]></description><link>http://discuss.colyseus.io/topic/812/physicseditor-exporter-for-defold-box2d-ne</link><generator>RSS for Node</generator><lastBuildDate>Sun, 08 Mar 2026 21:57:27 GMT</lastBuildDate><atom:link href="http://discuss.colyseus.io/topic/812.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 03 Nov 2022 15:47:09 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to PhysicsEditor Exporter for Defold &amp; Box2D NE on Sat, 05 Nov 2022 12:21:17 GMT]]></title><description><![CDATA[<p><a href="https://www.codeandweb.com/physicseditor" rel="nofollow">PhysicsEditor</a> 并不带有 Defold 的 Exporter, 为了方便使用我就自己写了一个.<br />
配合 <a href="https://github.com/d954mas/defold-box2d" rel="nofollow">Box2D Native Extension</a> 使用.</p>
<p><img src="/assets/uploads/files/1667490389784-8cbdfc59-a98b-4e4d-9442-d21503b87f00-image.png" alt="0_1667490387913_8cbdfc59-a98b-4e4d-9442-d21503b87f00-image.png" class="img-responsive img-markdown" /></p>
<p>defold.lua</p>
<pre><code>-- This file is for use with Defold &amp; Box2D native extension
--
-- Usage example:
--			local scaleFactor = 0.04
--			local physicsData = (require &quot;shapedefs&quot;).physicsData(scaleFactor)
--          local def = physicsData:get(&quot;defname&quot;)
--			for i, v in ipairs(def) do
--		        body:CreateFixture(v)
--	        end
--

-- copy needed functions to local scope
local unpack = unpack
local pairs = pairs
local ipairs = ipairs

local M = {}

function M.physicsData(scale)
	local physics = { data =
	{ {% for body in bodies %}
		{% if not forloop.first %}, {% endif %}
		[&quot;{{body.name}}&quot;] = {
                    {% for fixture in body.fixtures %}
                    {% if not forloop.first %} ,{% endif %}
                    {% if fixture.isCircle %}
                    {
                    density = {{fixture.density}}, friction = {{fixture.friction}}, restitution = {{fixture.restitution}}, {% if fixture.isSensor %}isSensor=true, {% endif %}
                    filter = { categoryBits = {{fixture.filter_categoryBits}}, maskBits = {{fixture.filter_maskBits}}, groupIndex = {{fixture.filter_groupIndex}} },
                    shape = {shape = 0, circle_radius = {{fixture.radius|floatformat:3}}}
                    }
                    {% else %}
                    {% for polygon in fixture.polygons %}{% if not forloop.first %} ,{% endif %}
                    {
                    density = {{fixture.density}}, friction = {{fixture.friction}}, restitution = {{fixture.restitution}}, {% if fixture.isSensor %}isSensor=true, {% endif %}
                    filter = { categoryBits = {{fixture.filter_categoryBits}}, maskBits = {{fixture.filter_maskBits}}, groupIndex = {{fixture.filter_groupIndex}} },
                    shape ={shape = 2, polygon_vertices = { {% for point in polygon %} {% if not forloop.first %}, {% endif %} vmath.vector3({{point.x}}, {{point.y}}, 0) {% endfor %} }}
                    }
                    {% endfor %}
                    {% endif %}
                    {% endfor %}
		}
		{% endfor %}
	} }

        -- apply scale factor
        local s = scale or 0.04
        for bi,body in pairs(physics.data) do
                for fi,fixture in ipairs(body) do
                    if(fixture.shape.polygon_vertices) then
                        for ci,coordinate in ipairs(fixture.shape.polygon_vertices) do
                            fixture.shape.polygon_vertices[ci] = s * coordinate
                        end
                    else
                        fixture.shape.circle_radius = s * fixture.shape.circle_radius
                    end
                end
        end
	
	function physics:get(name)
		return self.data[name]
	end

	return physics;
end

return M


</code></pre>
<p>exporter.xml</p>
<pre><code>&lt;exporter&gt;
	&lt;name&gt;defold-box2d&lt;/name&gt;
        &lt;displayName&gt;Defold + Box2D NE&lt;/displayName&gt;
        &lt;description&gt;Exporter for Defold-Box2D, lua&lt;/description&gt;
        &lt;version&gt;1.0&lt;/version&gt;
	&lt;yAxisDirection&gt;up&lt;/yAxisDirection&gt;
	&lt;physicsEngine&gt;box2d&lt;/physicsEngine&gt;
	&lt;template&gt;defold.lua&lt;/template&gt;
	&lt;fileExtension&gt;lua&lt;/fileExtension&gt;
	&lt;anchorPoint&gt;
		&lt;enabled&gt;no&lt;/enabled&gt;
	&lt;/anchorPoint&gt;
	&lt;origin&gt;
        &lt;type&gt;fixed&lt;/type&gt;
	    &lt;relX&gt;0.5&lt;/relX&gt;
		&lt;relY&gt;0.5&lt;/relY&gt;
    &lt;/origin&gt;
    &lt;!-- Circle support does not work as with standard box2d - you can't change the center
         or add more than one circle to a shape. This is why it is disabled for now --&gt;
    &lt;supportsCircles&gt;yes&lt;/supportsCircles&gt;
	&lt;body&gt;
	&lt;/body&gt;
	&lt;global&gt;
	&lt;/global&gt;
	&lt;body&gt;
	&lt;/body&gt;
	&lt;fixture&gt;
		&lt;parameter&gt;
			&lt;name&gt;density&lt;/name&gt;
			&lt;displayName&gt;Density&lt;/displayName&gt;
			&lt;type&gt;float&lt;/type&gt;
			&lt;min&gt;-1000&lt;/min&gt;
			&lt;max&gt;1000&lt;/max&gt;
			&lt;default&gt;2.0&lt;/default&gt;
		&lt;/parameter&gt;
		&lt;parameter&gt;
			&lt;name&gt;restitution&lt;/name&gt;
			&lt;displayName&gt;Restitution&lt;/displayName&gt;
			&lt;type&gt;float&lt;/type&gt;
			&lt;min&gt;0&lt;/min&gt;
			&lt;max&gt;1&lt;/max&gt;
			&lt;default&gt;0.0&lt;/default&gt;
		&lt;/parameter&gt;
		&lt;parameter&gt;
			&lt;name&gt;friction&lt;/name&gt;
			&lt;displayName&gt;Friction&lt;/displayName&gt;
			&lt;type&gt;float&lt;/type&gt;
			&lt;min&gt;0&lt;/min&gt;
			&lt;max&gt;1000&lt;/max&gt;
			&lt;default&gt;0.0&lt;/default&gt;
		&lt;/parameter&gt;
        &lt;parameter&gt;
            &lt;name&gt;isSensor&lt;/name&gt;
            &lt;displayName&gt;Is Sensor&lt;/displayName&gt;
            &lt;description&gt;If set the physial &lt;/description&gt;
            &lt;type&gt;bool&lt;/type&gt;
            &lt;default&gt;false&lt;/default&gt;
        &lt;/parameter&gt;

        &lt;parameter&gt;
            &lt;name&gt;filter_groupIndex&lt;/name&gt;
            &lt;displayName&gt;Group&lt;/displayName&gt;
            &lt;description&gt;Collision group.&lt;/description&gt;
            &lt;shortDescription&gt;&lt;/shortDescription&gt;
            &lt;type&gt;int&lt;/type&gt;
            &lt;default&gt;0&lt;/default&gt;
        &lt;/parameter&gt;

        &lt;parameter&gt;
            &lt;name&gt;filter_bitfield&lt;/name&gt;
            &lt;type&gt;bitfield&lt;/type&gt;
            &lt;size&gt;16&lt;/size&gt;
        &lt;/parameter&gt;

        &lt;parameter&gt;
            &lt;name&gt;filter_categoryBits&lt;/name&gt;
            &lt;displayName&gt;Cat.&lt;/displayName&gt;
            &lt;description&gt;Collision category&lt;/description&gt;
            &lt;shortDescription&gt;Collision category&lt;/shortDescription&gt;
            &lt;type&gt;uint16&lt;/type&gt;
            &lt;default&gt;1&lt;/default&gt;
            &lt;bitfield&gt;yes&lt;/bitfield&gt;
        &lt;/parameter&gt;
        &lt;parameter&gt;
            &lt;name&gt;filter_maskBits&lt;/name&gt;
            &lt;displayName&gt;Mask&lt;/displayName&gt;
            &lt;description&gt;Collision mask&lt;/description&gt;
            &lt;shortDescription&gt;Collision mask&lt;/shortDescription&gt;
            &lt;type&gt;uint16&lt;/type&gt;
            &lt;default&gt;65535&lt;/default&gt;
            &lt;bitfield&gt;yes&lt;/bitfield&gt;
        &lt;/parameter&gt;
        &lt;/fixture&gt;
&lt;/exporter&gt;

</code></pre>
<p><img src="/assets/uploads/files/1667490411972-4ca50a6c-cf2e-4ac4-a6bb-8e25e89630fa-image.png" alt="0_1667490410510_4ca50a6c-cf2e-4ac4-a6bb-8e25e89630fa-image.png" class="img-responsive img-markdown" /></p>
<p><img src="/assets/uploads/files/1667490423039-bb5df96d-ce19-469e-a625-647c8c5e4438-image.png" alt="0_1667490420923_bb5df96d-ce19-469e-a625-647c8c5e4438-image.png" class="img-responsive img-markdown" /></p>
<p>FYI, 如果 body:CreateFixture() 支持 table 做参数就更好了.</p>
]]></description><link>http://discuss.colyseus.io/post/2208</link><guid isPermaLink="true">http://discuss.colyseus.io/post/2208</guid><dc:creator><![CDATA[COCO]]></dc:creator><pubDate>Sat, 05 Nov 2022 12:21:17 GMT</pubDate></item></channel></rss>