Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You have a very usefull link about box2D and libgdx teaching concepts about logic and render. <a href="http://www.aurelienribon.com/blog/projects/physics-body-editor/" rel="noreferrer">here</a></p> <p>Then you can separate the logic part of the render part like this:</p> <p>Logic part:</p> <pre><code>private void createBottleBody() { // 0. Create a loader for the file saved from the editor. BodyEditorLoader loader = new BodyEditorLoader(sourceFile); // 1. Create a BodyDef, as usual. BodyDef bd = new BodyDef(); bd.position.set(0, 0); bd.type = BodyType.DynamicBody; // 2. Create a FixtureDef, as usual. FixtureDef fd = new FixtureDef(); fd.density = 1; fd.friction = 0.5f; fd.restitution = 0.3f; // 3. Create a Body, as usual. bottleModel = world.createBody(bd); // 4. Create the body fixture automatically by using the loader. loader.attachFixture(bottleModel, "test01", fd, BOTTLE_WIDTH); } </code></pre> <p>Render part:</p> <pre><code>public void render() { Vector2 bottlePos = bottleModel.getPosition().sub(bottleModelOrigin); bottleSprite.setPosition(bottlePos.x, bottlePos.y); bottleSprite.setOrigin(bottleModelOrigin.x, bottleModelOrigin.y); bottleSprite.setRotation(bottleModel.getAngle() * MathUtils.radiansToDegrees); ... } </code></pre> <p>The loader: In the link you can find a loader.</p> <pre><code>public void attachFixture(Body body, String name, FixtureDef fd, float scale) { //Load the rigidModel by key RigidBodyModel rbModel = (RigidBodyModel) this.model.rigidBodies.get(name); if (rbModel == null) throw new RuntimeException("Name '" + name + "' was not found."); //Loading polygons Vector2 origin = this.vec.set(rbModel.origin).mul(scale); Vector2[] vertexes; PolygonModel polygon; for (int i = rbModel.polygons.size()-1; 0 &lt;= i; i--) { polygon = (PolygonModel) rbModel.polygons.get(i); vertexes = polygon.vectorBuffer; //Loading vertexes (scaled) from polygon for (int ii = vertexes.length-1; 0 &lt;= ii; ii--) { vertexes[ii] = new Vector2().set((Vector2) polygon.vertices.get(ii)).mul(scale); vertexes[ii].sub(origin); } //sets vertexs to polygon this.polygonShape.set(vertexes); fd.shape = this.polygonShape; body.createFixture(fd); } //Loading circles CircleModel circle; Vector2 center; float radius; for (int i = rbModel.circles.size()-1; 0 &lt;= i; i--) { circle = (CircleModel) rbModel.circles.get(i); center = new Vector2().set(circle.center).mul(scale); radius = circle.radius * scale; this.circleShape.setPosition(center); this.circleShape.setRadius(radius); fd.shape = this.circleShape; body.createFixture(fd); } } </code></pre> <p>And finally... models</p> <pre><code>public static class CircleModel { public final Vector2 center = new Vector2(); public float radius; } public static class PolygonModel { public final List&lt;Vector2&gt; vertices = new ArrayList&lt;Vector2&gt;(); private Vector2[] vectorBuffer; } public static class RigidBodyModel { public String name; public String imagePath; public final Vector2 origin = new Vector2(); public final List&lt;PolygonModel&gt; polygons = new ArrayList&lt;PolygonModel&gt;(); public final List&lt;CircleModel&gt; circles = new ArrayList&lt;CircleModel&gt;(); } public static class Model { public final Map&lt;String, RigidBodyModel&gt; rigidBodies = new HashMap&lt;String, RigidBodyModel&gt;(); } </code></pre> <p>Hope it helps!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload