【问题标题】:How can we implement a bidimensional collision on a NetLogo Billiards?我们如何在 NetLogo 台球上实现二维碰撞?
【发布时间】:2014-12-18 15:39:14
【问题描述】:

我们是两个电子游戏设计和开发的学生,我们必须使用 NetLogo 制作一些程序。我们的想法是台球,我们已经尽力做到了,但是我们无法想象如何实现游戏中16个球之间的二维碰撞,它们什么时候碰撞以及我们需要写什么让它发生。我们不希望您完成我们的工作,但如果您能或多或少地告诉我们如何轻松完成工作,我们将不胜感激,因为这对我们来说是新的,我们需要一些不难的东西,这样我们就能更好地理解它(如果解决方案很复杂,好吧,我们不在乎,我们需要知道它,所以继续!)。

这是迄今为止我们的 NetLogo 代码:

breed [BALLS ball]
balls-own [speed velocity x-vel y-vel] 
globals [points]



to setup
  clear-all
  setup-patches
  setup-balls
  set-default-shape balls "circle"
  ask ball 0 [hatch 1
  [
  set breed turtles
  fd 3
  set color red - 1
  ask myself [create-link-to myself [tie hide-link]]
  ]
  ]
  reset-ticks
end

to setup-patches
  ask patches [ set pcolor green ]
  ask patches [if (pxcor > -25) and (pycor > 12)
[ set pcolor brown ]
  ]
  ask patches [if (pxcor < 25) and (pycor < -12)
[ set pcolor brown ]
  ]
  ask patches [if (pxcor < -21)
[ set pcolor brown ]
  ]
  ask patches [if (pxcor > 21)
[ set pcolor brown ]
  ]
  ;Up left corner
  ask patches [if (pxcor = -22) and (pycor = 13)
    [set pcolor black]
  ]
  ask patches [if (pxcor = -21) and (pycor = 13)
    [set pcolor black]
  ]
  ask patches [if (pxcor = -22) and (pycor = 12)
    [set pcolor black]
  ]
  ;Up right hole
  ask patches [if (pxcor = 22) and (pycor = 13)
    [set pcolor black]
  ]
   ask patches [if (pxcor = 21) and (pycor = 13)
    [set pcolor black]
  ]
    ask patches [if (pxcor = 22) and (pycor = 12)
    [set pcolor black]
  ]
  ;Down left hole
  ask patches [if (pxcor = -22) and (pycor = -13)
    [set pcolor black]
  ]
   ask patches [if (pxcor = -21) and (pycor = -13)
    [set pcolor black]
  ]
    ask patches [if (pxcor = -22) and (pycor = -12)
    [set pcolor black]
  ]
  ;Down right hole
  ask patches [if (pxcor = 22) and (pycor = -13)
    [set pcolor black]
  ]
  ask patches [if (pxcor = 21) and (pycor = -13)
    [set pcolor black]
  ]
  ask patches [if (pxcor = 22) and (pycor = -12)
    [set pcolor black]
  ]
  ;Up hole
  ask patches [if (pxcor = -1) and (pycor = 13)
    [set pcolor black]
  ]
  ask patches [if (pxcor = 0) and (pycor = 13)
    [set pcolor black]
  ]
  ask patches [if (pxcor = 1) and (pycor = 13)
    [set pcolor black]
  ]
  ;Down hole
  ask patches [if (pxcor = -1) and (pycor = -13)
    [set pcolor black]
  ]
  ask patches [if (pxcor = 0) and (pycor = -13)
    [set pcolor black]
  ]
  ask patches [if (pxcor = 1) and (pycor = -13)
    [set pcolor black]
  ]
end

to setup-balls
  create-balls 16
  ask ball 0 [setxy 10 0]
  ask ball 0 [set color white]
  ask ball 0 [set heading angle]
  ask ball 1 [setxy -10 0]
  ask ball 1 [set color blue]
  ask ball 2 [setxy -11 0.5]
  ask ball 2 [set color blue]
  ask ball 3 [setxy -11 -0.5]
  ask ball 3 [set color blue]
  ask ball 4 [setxy -12 1]
  ask ball 4 [set color blue]
  ask ball 5 [setxy -12 0]
  ask ball 5 [set color black]
  ask ball 6 [setxy -12 -1]
  ask ball 6 [set color blue]
  ask ball 7 [setxy -13 1.5]
  ask ball 7 [set color blue]
  ask ball 8 [setxy -13 0.5]
  ask ball 8 [set color blue]
  ask ball 9 [setxy -13 -0.5]
  ask ball 9 [set color blue]
  ask ball 10 [setxy -13 -1.5]
  ask ball 10 [set color blue]
  ask ball 11 [setxy -14 2]
  ask ball 11 [set color blue]
  ask ball 12 [setxy -14 1]
  ask ball 12 [set color blue]
  ask ball 13 [setxy -14 0]
  ask ball 13 [set color blue]
  ask ball 14 [setxy -14 -1]
  ask ball 14 [set color blue]
  ask ball 15 [setxy -14 -2]
  ask ball 15 [set color blue]

end

to go
    ask balls [setxy (xcor + x-vel)(ycor + y-vel)
      set velocity 1.01
    if(velocity > 1)[
    set x-vel x-vel / velocity
    set y-vel y-vel / velocity
    ]
  ]
  ask ball 0 [set heading angle]
  ask balls [
    if pcolor = black [ 
    setxy 10 0
    set x-vel 0
    set y-vel 0
    set points points - 1
    ]
  ]


  ask balls [
    if pcolor = brown [
      if pxcor > 21 [
        set y-vel y-vel - 2 * y-vel
      ]
      if pxcor > -21 [
        set y-vel y-vel - 2 * y-vel
      ]
      if pycor > 12 [
        set x-vel x-vel - 2 * x-vel
      ]
      if pycor > -12 [
        set x-vel x-vel - 2 * x-vel
      ]
    ]
  ]

tick
end

to shoot
  ask ball 0 [set x-vel (sin angle * (power / 100))
    set y-vel (cos angle * (power / 100))
    set speed power / 100
  ]
end 

我们知道这可能不是更好的方法,但它确实有效!您需要的按钮是 setup、shoot、go、一个名为 points 的显示器和两个名为 angle (0-360) 和 power (0-100) 的拖鞋。

我们进行了大量研究,但几乎一无所获。我们已经查看了 NetLogo 库中名为:的示例,它对我们不是很有用。我们还查看了一些链接,例如https://gamedev.stackexchange.com/questions/7862/is-there-an-algorithm-for-a-pool-game/7901#7901Mass Ball-to-Ball Collision Handling (as in, lots of balls),但我们可以解决所有这些问题。就像我说的,我们只需要海龟的二维碰撞,感谢阅读!

【问题讨论】:

  • 您需要描述 NetLogo 应该为我们做什么,以帮助您使用 NetLogo 代码来实现它。例如,您是否无法检测到两个球何时发生碰撞?或者你知道两个球相撞了,你有两个球的新方向和速度的数学计算,但不能让 NetLogo 做数学计算?
  • 我们需要知道球什么时候必须碰撞以及我们如何做到这一点,我们必须实施什么才能使它们正确碰撞。

标签: collision netlogo billiards


【解决方案1】:

在 NetLogo 模型库的 GasLab 圆形粒子模型中,在 Sample Models -> Chemistry & Physics 下,有用于此的代码,具有正确的物理特性。

所涉及的数学和逻辑相当复杂,但在“信息”和“代码”选项卡中有相当多的解释。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多