NetLogo 墙壁碰撞 - “弹跳”功能

发布于 2024-10-09 08:45:27 字数 2917 浏览 10 评论 0原文

NetLogo 海龟不断穿过迷宫的墙壁。我如何阻止他们穿过墙壁并让他们改变方向?

我很感激任何帮助。

到目前为止我的代码:

breed [ defaults default ]
defaults-own  [ new-heading ]
breed [squares1 square]
breed [squares2 square]
breed [squares3 square]
globals [ score    ]

to setup-row [row colour segments]
  foreach segments
  [
      if pycor = row * row-patches-width and
        (pxcor >= col-patches-width * (item 0 ?)) and (pxcor <= col-patches-width * (item 1 ?))
          [set pcolor colour]
  ]
end

to setup-col [col colour segments]
  foreach segments
  [
      if pxcor = col * col-patches-width and
        (pycor >= row-patches-width * (item 0 ?)) and (pycor <= row-patches-width * (item 1 ?))
          [set pcolor colour]
  ]
end

to setup-arwels-maze
  clear-all
  set score 0
  setup-defaults 
  setup-squares1 
  setup-squares2
  setup-squares3 
  ask patches
  [     if (pxcor >= min-pxcor and pxcor <= max-pxcor and
        pycor >= min-pycor and pycor <= max-pycor)
          [set pcolor black] 

      setup-row  8  white [[-15 15]]
      setup-row  6  white [[-15 -10][10 15]]
      setup-row  3  white [[-15 -10][10 15]]
      setup-row  6  white [[-4 4]]
      setup-row  4  white [[-6 6]]
      setup-row -1  white [[-2 2]]
      setup-row -3  white [[-4 4]]
      setup-row  1  white [[-3 3]]
      setup-row  0  white [[-15 -10][10 15]]
      setup-row -4  white [[-15 -10][10 15]]
      setup-row -7  white [[-15 -10][10 15]]
      setup-row -6  white [[-3 -2][2 3]]
      setup-row -9  white [[-3 3]]
      setup-row -11 white [[-11 11]]
      setup-row -13 white [[-15 15]]

      setup-col  15 white [[ 0 8][-13 -4]]
      setup-col  10 white [[-7 -4][0 3]]
      setup-col  12 white [[ 3 4]]
      setup-col  7  white [[-7.5 2][6 8]]
      setup-col  3  white [[-9 -6][1 4]]
      setup-col  0  white [[-3 -1]]
      setup-col -3  white [[-9 -6][1 4]]
      setup-col -7  white [[-7.5 2][6 8]]
      setup-col -10 white [[-7 -4][0 3]]
      setup-col -12 white [[ 3 4]]
      setup-col -15 white [[ 0 8][-13 -4]]
  ]
end

to setup-defaults
  create-defaults 1
  [ set color yellow
    set shape "default"
    set size 4
    setxy 2 -48
    set heading 0            
      ]
end

to setup-squares1
   create-squares1 1
     [ 
    set shape "square"
    set color random 14 * 10 + 5
    set size 3
    setxy 38 28
    set heading 0
      ]
end

to setup-squares2
   create-squares2 1
     [ 
    set shape "square"
    set color random 14 * 10 + 5
    set size 3
    setxy -5 -8
    set heading 0

      ]
end

to setup-squares3
   create-squares3 1
     [ 
    set shape "square"
    set color random 14 * 10 + 5
    set size 3
    setxy 40 -45
    set heading 0
      ]
end


to move-up
  ask defaults[ fd 1.00 ]
end

to move-right
  ask defaults [ rt 90 ]
end

to move-down
  ask defaults [ bk 1.00 ]
end

to move-left
  ask defaults [ lt 90 ]
end

感谢您的时间和帮助。

The NetLogo turtles keep going through the walls of the maze. How do I stop them from going through the walls and instead have them change direction?

I'm grateful for any help.

My code so far :

breed [ defaults default ]
defaults-own  [ new-heading ]
breed [squares1 square]
breed [squares2 square]
breed [squares3 square]
globals [ score    ]

to setup-row [row colour segments]
  foreach segments
  [
      if pycor = row * row-patches-width and
        (pxcor >= col-patches-width * (item 0 ?)) and (pxcor <= col-patches-width * (item 1 ?))
          [set pcolor colour]
  ]
end

to setup-col [col colour segments]
  foreach segments
  [
      if pxcor = col * col-patches-width and
        (pycor >= row-patches-width * (item 0 ?)) and (pycor <= row-patches-width * (item 1 ?))
          [set pcolor colour]
  ]
end

to setup-arwels-maze
  clear-all
  set score 0
  setup-defaults 
  setup-squares1 
  setup-squares2
  setup-squares3 
  ask patches
  [     if (pxcor >= min-pxcor and pxcor <= max-pxcor and
        pycor >= min-pycor and pycor <= max-pycor)
          [set pcolor black] 

      setup-row  8  white [[-15 15]]
      setup-row  6  white [[-15 -10][10 15]]
      setup-row  3  white [[-15 -10][10 15]]
      setup-row  6  white [[-4 4]]
      setup-row  4  white [[-6 6]]
      setup-row -1  white [[-2 2]]
      setup-row -3  white [[-4 4]]
      setup-row  1  white [[-3 3]]
      setup-row  0  white [[-15 -10][10 15]]
      setup-row -4  white [[-15 -10][10 15]]
      setup-row -7  white [[-15 -10][10 15]]
      setup-row -6  white [[-3 -2][2 3]]
      setup-row -9  white [[-3 3]]
      setup-row -11 white [[-11 11]]
      setup-row -13 white [[-15 15]]

      setup-col  15 white [[ 0 8][-13 -4]]
      setup-col  10 white [[-7 -4][0 3]]
      setup-col  12 white [[ 3 4]]
      setup-col  7  white [[-7.5 2][6 8]]
      setup-col  3  white [[-9 -6][1 4]]
      setup-col  0  white [[-3 -1]]
      setup-col -3  white [[-9 -6][1 4]]
      setup-col -7  white [[-7.5 2][6 8]]
      setup-col -10 white [[-7 -4][0 3]]
      setup-col -12 white [[ 3 4]]
      setup-col -15 white [[ 0 8][-13 -4]]
  ]
end

to setup-defaults
  create-defaults 1
  [ set color yellow
    set shape "default"
    set size 4
    setxy 2 -48
    set heading 0            
      ]
end

to setup-squares1
   create-squares1 1
     [ 
    set shape "square"
    set color random 14 * 10 + 5
    set size 3
    setxy 38 28
    set heading 0
      ]
end

to setup-squares2
   create-squares2 1
     [ 
    set shape "square"
    set color random 14 * 10 + 5
    set size 3
    setxy -5 -8
    set heading 0

      ]
end

to setup-squares3
   create-squares3 1
     [ 
    set shape "square"
    set color random 14 * 10 + 5
    set size 3
    setxy 40 -45
    set heading 0
      ]
end


to move-up
  ask defaults[ fd 1.00 ]
end

to move-right
  ask defaults [ rt 90 ]
end

to move-down
  ask defaults [ bk 1.00 ]
end

to move-left
  ask defaults [ lt 90 ]
end

Thank you for your time and help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪意 2024-10-16 08:45:27

您必须自己实施反弹。你的代码没有这样做。无论前方是什么,海龟都只会移动 1。

一个简单的弹跳(像球一样,保持入射角)功能是:

to bounce 
  if [pcolor] of patch-at dx 0 = white [
    set heading (- heading)
  ]
  if [pcolor] of patch-at 0 dy = white [
    set heading (180 - heading)
  ]
end

它针对白色斑块弹跳。请注意,您的乌龟必须朝其移动的方向“前进”。

You have to implement the bounce yourself. Your code does not do that. The turtles just move by 1 regardless of what is a ahead.

A simple bounce (like a ball, preserving incidence angle) function is:

to bounce 
  if [pcolor] of patch-at dx 0 = white [
    set heading (- heading)
  ]
  if [pcolor] of patch-at 0 dy = white [
    set heading (180 - heading)
  ]
end

It bounces against white patches. Note that your turtle has to be 'heading' in the direction it is moving.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文