GKObstacleGraph如何找到最近的有效点? [英] GKObstacleGraph How to Find Closest Valid point?

查看:142
本文介绍了GKObstacleGraph如何找到最近的有效点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用户想要通过鼠标导航或触摸到某些无法通过的地图的情况下.将点发送到GKObstacleGRpah FindPath时,它只会返回一个空数组.

In a scenario where user wants to navigate via mouse or touch to some are of the map that is not passable. When sending the point to GKObstacleGRpah FindPath it just returns an empty array.

我希望该设备到达最接近(或足够接近)的可通过点.

I want the unit to go to closest (or close enough) passable point.

GKObstacleGraph中找到最接近的有效点的合适方法是什么.

What would be an appropriate way to find a closest valid point in GKObstacleGraph.

我知道我可以得到GKObstacle,所以我可以枚举它的顶点,而且我知道我单位的位置...

I understand that I can get the GKObstacle so I can enumerate it's vertices, and I know my unit's position...

但是...下一步是什么?

But well... what is the next step ?

注意:我没有使用GKAgengts.

推荐答案

在解决此问题时,这是我的想法.可能有一个更简单的答案,但我还没有找到.

Here was my thinking as I worked through this problem. There may be an easier answer, but I haven’t found it.

1.找出一个点是否有效.

为此,我实际上有每个障碍物的CGPath表示形式.我从 PhysicsEditor 导出路径,然后通过将其转换为CGPath的自定义脚本加载路径.我总是将CGPath和我从路径创建的GKObstacle一起存储.最后,我可以调用CGPathContainsPoint来确定包含该点的障碍物.如果为真,则知道该点无效.

To do this, I actually have a CGPath representation of each obstacle. I export paths from PhysicsEditor and then load them in through a custom script that converts them to CGPath. I always store that CGPath along with the GKObstacle I created from the path. Finally, I can call CGPathContainsPoint to determine the the obstacle contains the point. If true, I know the point is invalid.

2.一旦该点无效,请找出单击了哪个障碍.

在#1中,我已经有了CGPath的句柄及其所属的障碍物.

With my approach in #1, I already have a handle on the CGPath and obstacle it belongs to.

3.查找最接近的顶点

现在我知道了障碍物,找到最靠近正在移动的单元的顶点.我找不到要点击的壁橱顶点,因为那可能在拐角处.而是找出所需位置和单位之间的向量.然后,从所需位置穿过顶点绘制一条不可见的线.我用这个方程式找出线的交叉点.

Now that I know the obstacle, find the vertex closest to the unit that is moving. I wouldn’t find the closet vertex to the point clicked because that could be around a corner. Instead, figure out the vector between the desired position and the unit. Then, draw an invisible line from the desired position through the vertex. I used this equation to find out where the lines would cross.

   // http://stackoverflow.com/questions/1811549/perpendicular-on-a-line-from-a-given-point
    let k = ((end.y - start.y) * (hitPoint.x - start.x) - (end.x - start.x) * (hitPoint.y - start.y)) / ((end.y - start.y) * (end.y - start.y) + (end.x - start.x) * (end.x - start.x))
    let x4 = hitPoint.x - k * (end.y - start.y)
    let y4 = hitPoint.y + k * (end.x - start.x)
    let ret = float2(x: x4, y: y4)

4.通过单位尺寸向移动单位偏移交点

知道路径与矢量朝着单位相交的位置后,我们可以移至交点+(矢量*单位大小).

Knowing where the paths intersect along with the vector towards the unit, we can just move to intersection point + (the vector * unit size).

5.边缘案例

当两个障碍物相互接触时,或者如果障碍物很大时,这将变得很棘手.用户可能认为他们正在移动到地图的远端,但是此脚本将返回到可能在附近的壁橱有效点.因此,我取消了所有代码,只是不允许您移动到无效位置.出现红色的X和嗡嗡声,提示用户在地图上选择新的有效位置.

This gets tricky when you have two obstacles touching each other, or if you have very large obstacles. The user may think they are moving to the far side of the map, but this script will follow back to the closet valid point which could be nearby. Because of that, I scrapped all of the code and I just don’t allow you to move to invalid positions. A red X and a buzz sound happens prompting the user to select a new, valid position on the map.

6.演示机器人

我可能是错的,但是我隐约记得苹果公司的DemoBots具有类似的逻辑-它们可能也是很好的参考.

I could be wrong, but I vaguely recall DemoBots from Apple having similar logic — they may be a good reference too.

7.调试层

这花了我永远在一起才能证明概念.我强烈建议在调试层上画线以验证您的逻辑.

This took forever for me to get a proof of concept together. I’d highly recommend drawing lines on a debug layer to verify your logic.

这篇关于GKObstacleGraph如何找到最近的有效点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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