Note that there are some explanatory texts on larger screens.

plurals
  1. POFlipping polygon in libgdx
    text
    copied!<p>I have Tiled maps working in libgdx, now I'm trying to do collision with these maps. I figured it'd be really useful just to draw polygons in Tiled and use them for collisions in libgdx. I'd actually prefer it this way rather than doing a per tile collision since it'll give me collision flexibility which the game requires. </p> <p>I've successfully parsed each object's xml and grabbed the polygon points. I've been able to make a box2d PolygonShape with these and it shows up in game! But it's mirrored upside down...</p> <p>I figured tiled must be on a different coordinate system than libgdx so I just created a function that finds the max y point and subtracts the rest of the y points from it in correct y flipping fashion. But now I'm getting this error:</p> <pre><code>java: /var/lib/hudson/jobs/libgdx-git/workspace/gdx/jni/Box2D/Collision/Shapes/b2PolygonShape.cpp:115: b2Vec2 ComputeCentroid(const b2Vec2*, int32): Assertion `area &gt; 1.19209289550781250000e-7F' failed. </code></pre> <p>It looks like an error from libgdx's native side. <strong>Is there anyone that can help me figure this out?</strong></p> <p>Here's the relevant section from my tmx file.</p> <pre><code>&lt;objectgroup height="20" name="Object Layer 1" width="20"&gt; &lt;object x="100" y="320"&gt; &lt;polygon points="0,0 244,1 300,50 300,95 -12,88 -29,19"/&gt; &lt;/object&gt; &lt;object height="104" width="116" x="437" y="175"/&gt; &lt;/objectgroup&gt; </code></pre> <p>Here's the bit of code, it's in Scala, that flips the polygon. It takes a Border object that has a polygon field which is just an array of Vector2s. The _type field is a symbol, it looks like it messes with stackoverflow's syntax highlighting.</p> <pre><code>def flipPolygonY(border: Border){ if(border._type == 'polygon){ //find the highest y point var max = border.polygon(0).y for(vect &lt;- border.polygon){ max = max.max(vect.y) } //flip it, take max - y for(vect &lt;- border.polygon){ vect.set(vect.x, max - vect.y) } } } </code></pre>
 

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