physicsPolygonCollider显示异常

physicsPolygonCollider的点有什么要求吗,为什么会显示异常,有时候还会报错。
下面是编辑这个多变形点的图案

这是点分别对应的左边


这是实际运行效果,下面多了一个正方形

而且有的点还会导致报错,这是错误信息

求各位大哥哥大姐姐帮帮忙看一下

1赞

我也做到这一步了,但是差距还很大,因为首先,我们的这种写法,不能有重复的点,有重复的点会报错,另外,假如你画的是一个弧形,那么会生成一个扇形刚体

你的事直接在引擎弄得多边形?不是用代码生成的?

用代码生成的,不过有时候生成的多边形会有异常或者报错,我就把点的坐标打出来,用界面编辑找规律,结果一样的也是报错 ,不知道咋弄

没有官方的人来露个脸?编辑器里的确没问题,用代码生成有时就会报这个错误,同时下面多个正方体。

解决了吗,大神们,发现只要是x坐标都相同,或者y坐标都相同,就宕掉了

我也是遇到同样的问题,以为2.0解决了,发现还是一样。
多边形是类似于山坡的曲线,就是用的官方的山坡的那个公式…难怪不用这个多边形,要用一个个矩形。
当y值,接近一样,差值很小的时候,就会报错

这个问题有人解决了吗

同遇到这个bug,请问怎么解决的啊

判断当前点和上一个点的距离大于2试试

修改源码CCPolygonSeparator.js 里的。 报错是因为vertices数组长度为1,i 为-1的时候 i < 0 ? s - (-i s) : i s 的值为1 。所以加了个判断。目前我这边测试这个问题已经解决了

4赞

大佬给力,回去试一下

这是我的解决代码,一点点找到的规律,虽然有时候还会报错,但结果没啥问题了,尽量将所有点转化成整数这是游戏的体验地址

    /**
     * 计算两点之间的直线距离
     * @param {cc.Vec2} origin 起点
     * @param {cc.Vec2} destination 终点
     * 
     * @returns {number}
     */
    deuceDistance(origin: cc.Vec2, destination: cc.Vec2): number {
        return Math.sqrt(Math.pow((destination.x - origin.x), 2) + Math.pow((destination.y - origin.y), 2));
    }

/**
     * 线段和线段是否相交
     * 
     * @param a1 
     * @param a2 
     * @param b1 
     * @param b2 
     * @param interPoint 交点
     */
    lineLine(a1, a2, b1, b2, interPoint) {
        // jshint camelcase:false
        let sa_x = a2.x - a1.x
        let sb_x = b2.x - b1.x
        let sa_y = a2.y - a1.y
        let sb_y = b2.y - b1.y

        var ua_t = sb_x * (a1.y - b1.y) - sb_y * (a1.x - b1.x);
        var ub_t = sa_x * (a1.y - b1.y) - sa_y * (a1.x - b1.x);
        var u_b = sb_y * sa_x - sb_x * sa_y

        if (u_b !== 0) {
            var ua = ua_t / u_b;
            var ub = ub_t / u_b;

            if (0 <= ua && ua <= 1 && 0 <= ub && ub <= 1) {
                interPoint.x = a1.x + (ua * sa_x);
                interPoint.y = a1.y + (ua * sa_y);
                return true;
            }
        }

        return false;
    }

    /**
     * 处理多边形的各种问题 
     * 2019-8-12 快吐血了,也不知道还有没有bug
     * 2019-8-14 应该没bug了
     * @param poly 
     */
    handle(poly: cc.Vec2[]): cc.Vec2[][] {
        let _poly = []

        // 去掉小于3的点
        for (let i = 0; i < poly.length; i++) {
            let j = (i == poly.length - 1 ? 0 : i + 1)
            poly[i].x = Math.round(poly[i].x)
            poly[i].y = Math.round(poly[i].y)

            poly[j].x = Math.round(poly[j].x)
            poly[j].y = Math.round(poly[j].y)

            let dis = Helpers.deuceDistance(poly[i], poly[j])
            if (dis < 2) {
            } else {
                _poly.push(poly[i])
            }
        }

        // 共线解决
        for (let i = 0; i < _poly.length; i++) {
            let next_0 = (i == _poly.length - 1 ? 0 : i + 1)
            let next_1 = (next_0 == _poly.length - 1 ? 0 : next_0 + 1)
            let next_2 = (next_1 == _poly.length - 1 ? 0 : next_1 + 1)
            let next_3 = (next_2 == _poly.length - 1 ? 0 : next_2 + 1)

            // 判断斜率是否相等
            // (y3−y1)(x2−x1)−(y2−y1)(x3−x1)
            let result = (_poly[next_1].y - _poly[i].y) * (_poly[next_0].x - _poly[i].x) - (_poly[next_0].y - _poly[i].y) * (_poly[next_1].x - _poly[i].x)
            if (Math.round(result) == 0) {
                _poly.splice(next_0, 1)
                i--
                continue;
            }

        }

        let vec2IndexOf = (point: cc.Vec2, points: cc.Vec2[]) => {
            for (let i = 0; i < points.length; i++) {
                if (points[i].equals(point)) {
                    return i
                }
            }
            return -1
        }

        // 交叉解决
        let interIndex = []
        for (let i = 0; i < _poly.length; i++) {
            let pre_0 = (i == 0 ? _poly.length - 1 : i - 1)
            for (let j = i; j < _poly.length; j++) {
                let next_0 = (j == _poly.length - 1 ? 0 : j + 1)
                let next_1 = (next_0 == _poly.length - 1 ? 0 : next_0 + 1)
                // let next_2 = (next_1 == _poly.length - 1 ? 0 : next_1 + 1)

                if ([pre_0, next_0, next_1].indexOf(i) >= 0
                    || [next_0, next_1].indexOf(pre_0) >= 0
                    || next_0 == next_1) {
                    continue;
                }

                let interPoints = cc.v2()
                if (
                    Helpers.lineLine(_poly[pre_0], _poly[i], _poly[next_0], _poly[next_1], interPoints)
                ) {

                    interPoints = cc.v2(Math.round(interPoints.x), Math.round(interPoints.y))

                    // cc.log(i, pre_0, next_1, next_2)
                    // cc.log(_poly[i], _poly[pre_0], _poly[next_1], _poly[next_2], interPoints)

                    // 情况1
                    // 1->2   0->4
                    // 先4 后2 =>0->1->x->2->3->4->x 交点下标 2,6

                    // 情况2
                    // 1->2 0->4 交2
                    // 0->1->2->3->4->x 交点下标 2,5

                    // 情况3
                    // 0->1 2->3 交0
                    // 0->1->2->x->3  交点下标 0,3
                    let index = vec2IndexOf(interPoints, [_poly[pre_0], _poly[i], _poly[next_0], _poly[next_1]])
                    if (index >= 0) {
                        if (index < 2) {
                            _poly.splice(next_1, 0, interPoints)
                            interIndex.push([pre_0, i, next_0, next_0][index], next_1)
                        } else {
                            _poly.splice(i, 0, interPoints)
                            interIndex.push(i, [pre_0, i, next_0, next_0][index])
                        }
                    } else {
                        if (next_1 > i) {
                            _poly.splice(next_1, 0, interPoints)
                            _poly.splice(i, 0, interPoints)
                            interIndex.push(i, next_1 + 1)
                        } else {
                            _poly.splice(i, 0, interPoints)
                            _poly.splice(next_1, 0, interPoints)
                            interIndex.push(next_1, i + 1)
                        }
                    }
                    break;
                }
            }
            if (interIndex.length > 1) { break }
        }
        // cc.log(interIndex)

        // 拆分多边形
        let polys = []
        for (let i = 0; i < interIndex.length; i++) {
            let poly = []
            for (let j = interIndex[i], z = 0; z < _poly.length; j-- , z++) {
                let index = j < 0 ? _poly.length + j : j
                let point = _poly[index]

                if (index !== interIndex[i] && interIndex.indexOf(index) >= 0) {
                    polys.push(poly)
                    break;
                } else {
                    poly.push(point)
                }
            }
        }

        // if (polys.length) {
        //     cc.log(_poly, polys, interIndex)
        // }


        if (interIndex.length == 0) {
            return [_poly]
        } else {
            return polys
        }
    }
2赞

@24528690大佬的代码我还特意去试了一下,发现再我的游戏了依然会报很多错误 所以才贴出自己的代码以供参考:joy:

我以前遇到这个的问题是因为点和点之间有距离小于1像素的。

擦除后的碰撞时如何实现的啊,用的是多边形碰撞还是链条碰撞呢?

请问具体怎么解决的?2.4.3好像还有问题

请问,如何修改引擎?

谢谢大佬!!!!!卡了很久终于找到解决办法了

:rofl:试了一个特别复杂的地形 分割还是出错了