PhysicsEditor 并不带有 Defold 的 Exporter, 为了方便使用我就自己写了一个.
配合 Box2D Native Extension 使用.
defold.lua
-- This file is for use with Defold & Box2D native extension
--
-- Usage example:
-- local scaleFactor = 0.04
-- local physicsData = (require "shapedefs").physicsData(scaleFactor)
-- local def = physicsData:get("defname")
-- 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 %}
["{{body.name}}"] = {
{% 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
exporter.xml
<exporter>
<name>defold-box2d</name>
<displayName>Defold + Box2D NE</displayName>
<description>Exporter for Defold-Box2D, lua</description>
<version>1.0</version>
<yAxisDirection>up</yAxisDirection>
<physicsEngine>box2d</physicsEngine>
<template>defold.lua</template>
<fileExtension>lua</fileExtension>
<anchorPoint>
<enabled>no</enabled>
</anchorPoint>
<origin>
<type>fixed</type>
<relX>0.5</relX>
<relY>0.5</relY>
</origin>
<!-- 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 -->
<supportsCircles>yes</supportsCircles>
<body>
</body>
<global>
</global>
<body>
</body>
<fixture>
<parameter>
<name>density</name>
<displayName>Density</displayName>
<type>float</type>
<min>-1000</min>
<max>1000</max>
<default>2.0</default>
</parameter>
<parameter>
<name>restitution</name>
<displayName>Restitution</displayName>
<type>float</type>
<min>0</min>
<max>1</max>
<default>0.0</default>
</parameter>
<parameter>
<name>friction</name>
<displayName>Friction</displayName>
<type>float</type>
<min>0</min>
<max>1000</max>
<default>0.0</default>
</parameter>
<parameter>
<name>isSensor</name>
<displayName>Is Sensor</displayName>
<description>If set the physial </description>
<type>bool</type>
<default>false</default>
</parameter>
<parameter>
<name>filter_groupIndex</name>
<displayName>Group</displayName>
<description>Collision group.</description>
<shortDescription></shortDescription>
<type>int</type>
<default>0</default>
</parameter>
<parameter>
<name>filter_bitfield</name>
<type>bitfield</type>
<size>16</size>
</parameter>
<parameter>
<name>filter_categoryBits</name>
<displayName>Cat.</displayName>
<description>Collision category</description>
<shortDescription>Collision category</shortDescription>
<type>uint16</type>
<default>1</default>
<bitfield>yes</bitfield>
</parameter>
<parameter>
<name>filter_maskBits</name>
<displayName>Mask</displayName>
<description>Collision mask</description>
<shortDescription>Collision mask</shortDescription>
<type>uint16</type>
<default>65535</default>
<bitfield>yes</bitfield>
</parameter>
</fixture>
</exporter>
FYI, 如果 body:CreateFixture() 支持 table 做参数就更好了.