求一个2D游戏人物与物体的遮挡算法

求大佬给一个2D游戏,人物在地图上与物体之前的遮挡算法,不是用格子计算的

试试坐标算,x越大z越小,y越大z越大

zindex_DEMO.zip (821.8 KB)
这样的嘛 :13:

2d游戏的物体也可以有z轴,根据z轴大小判断遮挡

https://mp.weixin.qq.com/s/-8-cXkJ9hG8Di-yeJcPo7w
参考这篇文章,主要是图的构建,然后拓扑排序,这些网上搜一下学一下就可以

我搞定了,自己写了个算法,人物的层级会根据物体的层级进行变化,建筑设置2个点构成一个线段,如果人物在线段上方,人物的层级就比物体的层级低反之就比物体层级高,就OK了

发出来观摩一下,dddd

let zIndexOut = { out: 0, dir: 0, wid: this.comState.node.height };

    let _boundingBox = this.comState.node.getBoundingBoxToWorld();

    let isInBuild = _boundingBox.contains(pos);

    if (isInBuild) {

        if (this._buildConfig.zIndexPos.length > 0) {

            let pos1 = cc.v2(this._buildConfig.zIndexPos[0][0], this._buildConfig.zIndexPos[0][1]), pos2 = cc.v2(this._buildConfig.zIndexPos[1][0], this._buildConfig.zIndexPos[1][1]);

            if (pos1 && pos2) {

                let dis = ((pos2.y - pos1.y) * pospre.x + (pos1.x - pos2.x) * pospre.y + (pos2.x * pos1.y - pos1.x * pos2.y));

                if (dis > 0) {

                    zIndexOut.out = this.node.zIndex + 1;

                    zIndexOut.dir = 0;

                } else if (dis < 0) {

                    zIndexOut.out = this.node.zIndex - 1;

                    zIndexOut.dir = 1;

                }

            }

        }

    }
2赞


:ox: :frog: :ox: :frog: :ox: :frog: :ox: :frog:

没哪么复杂,把人物&物体的zindex设置成自身坐标的Y轴值(竖方向)就可以了。


不错,好东西

https://blog.codingnow.com/2014/01/isometric_tileset_engine.html