Search code examples
collisionnetlogobilliards

How can we implement a bidimensional collision on a NetLogo Billiards?


We're two videogame design and development students and we have to make some program using NetLogo. Our idea was a Billiards, and we have done it as well as we could, but there's impossible for us to imagine how can we implement the bidimensional collision between the 16 balls of the game, when do they collide and what do we need to write to make it happen. We do not want you to make our work, but we will be grateful if you could tell us, more or less, how to do it easily, because that's new for us and we need something not to hard, this way we will be able to comprehend it better (If the solution is complicated, well, we don't care, we need to know it so go ahead!).

This is our NetLogo code so far:

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 

We know that this is probably not the better way to do it, but it works! The buttons that you need are setup, shoot, go, a monitor named points and two slippers named angle (0-360) and power (0-100).

We've done a lot of research, and we've got almost nothing. We've looked at the example from NetLogo library named: that it wasn't very useful for us. Also we've looked some links like https://gamedev.stackexchange.com/questions/7862/is-there-an-algorithm-for-a-pool-game/7901#7901 or Mass Ball-to-Ball Collision Handling (as in, lots of balls) but we can make our head around with all this. Like I said, we only need the turtles bidimensional collision, thanks for reading!


Solution

  • There's code for this, with correct physics, in the GasLab Circular Particles model in the NetLogo Models Library, under Sample Models -> Chemistry & Physics.

    The math and the logic involved is rather complex, but there is a fair amount of explanation in the Info and Code tabs.