看不透墙 [英] Not see through the walls

查看:77
本文介绍了看不透墙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Netlogo中对某些建筑物搜索进行建模,但是现在我陷入了困境,因为我不知道如何解决穿墙问题.我的特工可以看到圆锥形,可以说60度和前方5个斑块.我可以在视线范围内检测到墙壁,也可以检测其他物体,问题是要检测对象是否在墙壁后面.我该如何解决?有什么想法吗?

I am trying to model some building search in Netlogo, but now I am stuck, because I have no idea how to solve seeing through walls. My agent can see in cone lets say 60 degrees and 5 patches ahead. I can detect wall somewhere in my sight and i can detect other objects as well, problem is to detect if the obejct is behind wall or not. How can I solve it? Any ideas?

推荐答案

这是一个完整的示例.您可以对需要检查哪些墙或补丁进行优化,而不是全部检查,但我将留给您...基本上,您可以想象您在代理与所有其他需要检查的补丁之间划了一条界限在药剂师的视线范围内,并去除斑块,使之有相交的壁.线相交问题已得到很好的研究,我提供了一个链接供您查看.为了简化计算,我存储了墙的端点-如果墙不像我的情况那样垂直,则可以使用trig来计算端点.我也使用一些美学...

Here's a full working example. You can do some optimizations regarding which walls or patches need to be checked, rather than all of them, but I'll leave that to you...Essentially, You can imagine that you draw a line between the agent and all other patches that are in the vision of the agent and remove the patches such that there is an intersecting wall. The line-intersection problem is well studied and I put a link for you to look at. To make my calculations easier, I store the end points of the wall--you could use trig to calculate the end points if the wall wasn't vertical like in my case. I use some aesthetics too...

breed [ walls wall]
walls-own [first-end second-end]

to setup
  ca
  reset-ticks
  set-default-shape walls "line"
  crt 1 [ setxy 4 0]
  ask turtles [facexy 0 0]

  ;;color all cones in radius blue by default
  let dist 10
  let angle 30
  ask turtles [ ask patches in-cone dist angle [set pcolor blue]]

  ;; place a wall down...the line of sight is blocked (keyword: line)
  create-walls 1 [ setxy 0 0 ]
  ;;This is an interpretation of a wall. Two points that define the edges.
  ask wall 1 [set size 10]
  ask wall 1 [set first-end (list 0 (size / 2))]
  ask wall 1 [set second-end (list 0 (-1 * size / 2))]
  ;;my wall is vertical. You can do trig above and below to adjust for not vert lines.
  ask wall 1 [ set heading 0]
  ask wall 1 [set color hsb  216 50 100] ;;pretty blue =)

  ask turtle 0 [ ask in-sight dist angle [ set pcolor green]]
end

;;a turtle can see a patch if the line from the patch to the turtle isn't intersected by a wall.
to-report in-sight [dist angle]
  let turtle-x xcor
  let turtle-y ycor
  report patches in-cone dist angle with 
  [
    not any? walls with [intersects [pxcor] of myself [pycor] of myself turtle-x turtle-y  ;; line 1
                                   (first first-end) (last first-end) (first second-end) (last second-end)] ;; line 2
  ]
end
;; See http://stackoverflow.com/questions/3838329/how-can-i-check-if-two-segments-intersect
;;counter clockwise method (doesn't consider colinearity)
to-report counter-clockwise [x1 y1 x2 y2 x3 y3]
  ;;returns true if triplet creates counter clockwise angle (uses slopes)
  ;(C.y-A.y) * (B.x-A.x) > (B.y-A.y) * (C.x-A.x)
  report (y3 - y1) * (x2 - x1) > (y2 - y1) * (x3 - x1)
end

to-report intersects [x1 y1 x2 y2 x3 y3 x4 y4]
  ;;line 1: x1 y1 x2 y2
  ;;line 2: x3 y3 x4 y4
  ;;DANGER: Doesn't work for colinear segments!!!
  ;ccw(A,C,D) != ccw(B,C,D) and ccw(A,B,C) != ccw(A,B,D)
  report (counter-clockwise x1 y1 x3 y3 x4 y4) != (counter-clockwise x2 y2 x3 y3 x4 y4)
  and (counter-clockwise x1 y1 x2 y2 x3 y3) != (counter-clockwise x1 y1 x2 y2 x4 y4)
end

这篇关于看不透墙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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