我是不熟悉stackoverflow发布的人。
在我对特工/乌龟看穿墙壁时遇到问题之前,但在这里找到了解决方案:
现在的问题是,当视锥内还有另一堵墙时,有效视线(绿色斑块)不正确。我目前仍在修补此问题。
这是我目前正在使用的代码,
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 80
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]
;;my wall is vertical. you can do trig above and below to adjust for not vert lines.
ask wall 1 [set heading 45]
ask wall 1 [set color hsb 216 50 100]
;; I made it so that it could consider not vertical lines
ask wall 1 [ set first-end (list (round (sin heading * (size / 2))) (round (cos heading * (size / 2))))]
ask wall 1 [ set second-end (list (round((-1 * (size / 2) * sin heading))) (round ((-1 * (size / 2) * cos heading))))]
;;Test wall. UN-COMMENT THE SET BELOW TO SEE THE PROBLEM
; create-walls 1 [setxy 4 4]
; ask wall 2 [set size 10]
; ask wall 2 [set heading 90]
; ask wall 2 [ set first-end (list (round (sin heading * (size / 2))) (round (cos heading * (size / 2))))]
; ask wall 2 [ set second-end (list (round((-1 * (size / 2) * sin heading))) (round ((-1 * (size / 2) * cos heading))))]
; ask wall 2 [set color hsb 216 50 100]
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 trutle isnt 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
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: Doesnt work for colinear segments
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
如果您不注释测试墙的代码集,这就是它的样子。
任何建议或提示将非常有帮助! 预先谢谢你。