libgdx多边形三角剖分 [英] Libgdx polygon triangulation

查看:142
本文介绍了libgdx多边形三角剖分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好,所以我有一个多边形(简单但凹入),我试图将其切成三角形以使其与另一个多边形碰撞.

Ok, so I have a polygon (simple but concave) that I'm trying to cut into triangles to make it collide with an other polygon.

我知道我的多边形是凹面的,所以我决定使用LibGDX EarClippingTriangulator 设法将其切成三角形.

I knew my polygone was concave, so i decided to use LibGDX EarClippingTriangulator to manage to cut it into triangles.

因此,使用此代码,我得到了三角形的顶点:

So, with this code, I get my triangles vertices :

public void triangulate()
    {
        Vector<float[]> trianglesVertices = new Vector<float[]>();
        ShortArray pointsCoords = new ShortArray();
        EarClippingTriangulator triangulator = new EarClippingTriangulator();

        // Cut in triangles
        pointsCoords = triangulator.computeTriangles(this.getTransformedVertices());

        // Make triangles
        for (int i = 0; i < pointsCoords.size / 6; i++)
        {
            trianglesVertices.add(new float[] {
                    pointsCoords.get(i), pointsCoords.get(i+1),
                    pointsCoords.get(i+2), pointsCoords.get(i+3),
                    pointsCoords.get(i+4), pointsCoords.get(i+5),
            });
            Polygon triangle = new Polygon(trianglesVertices.get(i));
            triangles.add(triangle);
        }

        System.out.printf("Triangulation made %d triangles.\n", pointsCoords.size / 6);
    }

但是当我尝试绘制我刚刚制作的那些三角形时, 它们只是堆叠在0,0坐标中. 而且,所有三角形看起来几乎都一​​样是正常的吗,我是说它们都具有相同的方向?

But when i try to draw thoses triangles I just made, they just stack in the 0,0 coord.. And, is it normal that all triangles seems almost the sames, I mean they all got the same orientation ?

我没有找到关于libgdx的这种截断使用的太多信息 你能帮忙吗?

I didn't found so much info about this trangulation use for libgdx Can you help ?

(抱歉我的英语是法语,很抱歉没有照片,我在这里还太小)

(Sorry for my english i'm french, and sorry for no pictures, i'm too young here)

编辑:这是我的多边形(在CCW中)

This is my polygon (in CCW)

hitbox.setVertices(new float[]{  
                this.getX() + 13, this.getY() - 60,
                this.getX() + 16, this.getY() - 74,
                this.getX() + 39, this.getY() - 74,
                this.getX() + 45, this.getY() - 105,
                this.getX() + 81, this.getY() - 105,
                this.getX() + 88, this.getY() - 74,
                this.getX() + 108, this.getY() - 74,
                this.getX() + 114, this.getY() - 61,
                this.getX() + 106, this.getY() - 30, // Top right
                this.getX() + 101, this.getY() - 29,
                this.getX() + 101, this.getY() - 57,
                this.getX() + 83, this.getY() - 62,
                this.getX() + 75, this.getY() - 50,
                this.getX() + 65, this.getY() - 4, // Top mid
                this.getX() + 62, this.getY() - 4, // Top mid
                this.getX() + 52, this.getY() - 50,
                this.getX() + 44, this.getY() - 62,
                this.getX() + 25, this.getY() - 56, 
                this.getX() + 25, this.getY() - 30,
                this.getX() + 19, this.getY() - 30,  // Top left
                });

现在,我有足够的要点向您展示这里的多边形

Now i got enough point to show you the polygon here it is

推荐答案

问题出在你的循环上

        // Make triangles
        for (int i = 0; i < pointsCoords.size / 6; i++)
        {
            trianglesVertices.add(new float[] {
                    pointsCoords.get(i), pointsCoords.get(i+1),
                    pointsCoords.get(i+2), pointsCoords.get(i+3),
                    pointsCoords.get(i+4), pointsCoords.get(i+5),
            });
            Polygon triangle = new Polygon(trianglesVertices.get(i));
            triangles.add(triangle);
        }

第一个三角形将具有正确的坐标,但是第二个三角形将使用pointsCoords的1、2、3、4、5、6个没有任何意义的元素.您应将i乘以6的内部循环以考虑偏移量:

First triangle will have correct coordinates, but second one will will use 1, 2, 3, 4, 5, 6 elements of pointsCoords that doesn't make any sence. You should multiply i by 6 inside loop to take offset into account:

                    pointsCoords.get(i*6), pointsCoords.get(i*6 + 1),
                    pointsCoords.get(i*6 + 2), pointsCoords.get(i*6 + 3),
                    pointsCoords.get(i*6 + 4), pointsCoords.get(i*6 + 5),

这篇关于libgdx多边形三角剖分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆