简体   繁体   中英

Flipping polygon in libgdx

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.

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...

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:

java: /var/lib/hudson/jobs/libgdx-git/workspace/gdx/jni/Box2D/Collision/Shapes/b2PolygonShape.cpp:115: b2Vec2 ComputeCentroid(const b2Vec2*, int32): Assertion `area > 1.19209289550781250000e-7F' failed.

It looks like an error from libgdx's native side. Is there anyone that can help me figure this out?

Here's the relevant section from my tmx file.

<objectgroup height="20" name="Object Layer 1" width="20">
 <object x="100" y="320">
   <polygon points="0,0 244,1 300,50 300,95 -12,88 -29,19"/>
 </object>
 <object height="104" width="116" x="437" y="175"/>
</objectgroup>

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.

def flipPolygonY(border: Border){
    if(border._type == 'polygon){
        //find the highest y point
        var max = border.polygon(0).y
        for(vect <- border.polygon){
            max = max.max(vect.y)
        }
        //flip it, take max - y
        for(vect <- border.polygon){
            vect.set(vect.x, max - vect.y)
        }
    }
}

Okay I figured this out, doing some searching I read that Polygons must be defined in clockwise order. So on a hunch I reversed the order of my array or Vector2's. It works. So if anyone else wants to use the polygons from Tiled in libgdx, flip them on the Y-axis and reverse the order.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM