Java StarLogo
2.0
english
`turtle`
turtles-own [speed energy totalenergy]

to go
  check-patches            
  collide     
  fd speed  
  getout 
  scale-color yellow energy 0 500
end

to collide           
   grab one-of-turtles-here         ;if there's a turtle on your patch make him your partner                 
       [seth random 360          
        seth-of partner random 360                           ;give partner random heading
	settotalenergy energy + (energy-of partner)          ;add energy and partner's energy
        setenergy (totalenergy * (((random 99) + 1) / 100))  ;randomize distribution of total energy
        setenergy-of partner totalenergy - energy            
        setspeed (sqrt (energy / 500))                       ;set speed based on energy
        setspeed-of partner (sqrt ((energy-of partner)/ 500)) 
        check-patches 
        fd 1]
end

to check-patches       
    if pc-ahead = red            ;check to see if you're at a wall
      [setenergy min (energy * 1.2) 500       ;change energy but don't get more than 500
       setspeed (sqrt (energy / 500))
       ifelse (heading < 180) 
         [setheading 180 - heading] 
         [setheading 540 - heading]
       check-patches]  
  if  pc-ahead = sky 
      [setenergy min (energy * 1.1) 500
       setspeed (sqrt (energy / 500))
       setheading 360 - heading
       check-patches]
  if pc-ahead = lime  
      [setenergy min (energy * .9) 500
       setspeed (sqrt (energy / 500))
       ifelse (heading < 180) 
          [setheading 180 - heading] 
          [setheading 540 - heading]
      check-patches]
end

to getout        ;check if need to redraw wall
  if (((xcor >= screen-half-width - .5) or (xcor <= .5 - screen-half-width)) or
      ((ycor >= screen-half-height - .5) or (ycor <= .5 - screen-half-height)))
     [redraw seth random 360 jump 1 getout]
end

to redraw        ;redraw wall
  if ((xcor = screen-half-width) or (xcor = 0 - screen-half-width)) [stamp sky]  
  if ycor = screen-half-height [stamp lime]       
  if ycor = 0 - screen-half-height  [stamp red]          
end


`observer`
to setup  
  ask-turtles [die]                                                
  crt number 
  ask-turtles   
    [setxy random (screen-width) random (screen-height)   
     setenergy ((random 200) + 1)                              
     setspeed sqrt (energy / 500)              
     scale-color yellow energy 0 500  
     getout]     ;don't start on a wall, redraw the wall if you did start there
 end

to clear-path        
   ask-patches [if (pc > 39) and (pc < 50)
               [setpc black]]
end


`information`
For use with the Adventures in Modeling book,  by Vanessa Colella, Eric Klopfer, and Mitchel Resnick published by Teachers College Press, 2001.  

For more information, see  
<http://education.mit.edu/starlogo/adventures>  

Challenge 7: Collisions  
This project is an idea model of a particle system that illustrates the use of grab. In Collisions, particles bounce off of walls and off of each other. They conserve energy, but not momentum, when they collide. Once a turtle has grabbed a partner in the collide procedure, it runs all of the instructions in between the square brackets. If a turtle doesn’t find a partner to grab, then it skips all of those instructions and continues going.   

Set the "number" slider to the desired number of particles, then press "setup" and "go". Can you see any collisions? Is it any easier to track the collisions when the turtles’ pens are down?  When the turtles take large steps they sometimes jump over one another. Can you reduce their forward step size to eliminate this effect?


`interface`
SLCanvas
top-left 11 212

SLButton
turtle-or-observer? observer
top-left 154 62
width-height 61 31
name "button6"
line-to-run "clear-path"
forever? false
button-number 7
show-name? false
whichip 7

SLButton
turtle-or-observer? turtle
top-left 110 28
width-height 56 30
name "button4"
line-to-run "pd"
forever? false
button-number 5
show-name? false
whichip 8

SLButton
turtle-or-observer? turtle
top-left 110 101
width-height 56 30
name "button5"
line-to-run "pu"
forever? false
button-number 6
show-name? false
whichip 9

SLSlider
top-left 65 47
width-height 93 25
name "slider1"
variable "number"
min-value 0
max-value 99
current-value 99
slider-number 0
show-name? false

SLButton
turtle-or-observer? turtle
top-left 27 101
width-height 56 30
name "button2"
line-to-run "go"
forever? true
button-number 3
show-name? false
whichip 10

SLButton
turtle-or-observer? observer
top-left 27 28
width-height 56 30
name "button3"
line-to-run "setup"
forever? false
button-number 4
show-name? false
whichip 11


`turtle shapes`
[encoded data, 5330 characters]
qqqqqqqaoR8DO5wb3dkOFQAAFQ7Z

`template`
null

`settings`
patch-size 4
num-shapes 256
screen-half-width 50
screen-half-height 50
interface-window-xcor 325
interface-window-ycor 153
interface-window-size 628 425
output-window-xcor 0
output-window-ycor 0
output-window-width 300
output-window-height 219
info-window-xcor 167
info-window-ycor 146
info-window-width 500
info-window-height 419
control-center-xcor 0
control-center-ycor 85
control-center-width 285
control-center-height 415
turtle-command-center-height 150
observer-command-center-height 150

`string table`
H4sIAAAAAAAAAGNgYGAAABzfRCEEAAAAAAAABA==

`symbol table`
H4sIAAAAAAAAAGNgYGAAABzfRCEEAAAAAAAABA==

`double table`
H4sIAAAAAAAAADWTfUyNbRzHj00bh+dZXiJZ1qInkpwnjweR881RR1HTkzrUonPq
7uR1vRxsNkYzNZrYwzgZslQz6slGZuYkj6gRp5MknZy3NfJylCaU19zf+/rvc39/
1/f6/n7XdctksnKl67prdcOZauhvyYaX8vGTpbnphmLE7CVnn4sSjHJEk1vPhkcY
Rr6EmuxyGLfVRTYiy09ku/tBQedgIrTUO2Mrw7MLC5BEf8eBPsOn/bnQkV1x24S4
jDnIlM4fLWtfVNSNFdz//2PhSGh9K2bKRLZ8UZeVHjmIKLIr5eu50MhdyLSLbM7x
mx9cXgZI+Y5azwQkVEKfxnwRHj7F4zyRxryWOze0TR8MiJTy1rntpgBvaMhtXybd
DJzfiViy/dH1giWDTdBCZOvL37OumL2xjrrjfL15qKEW6czntFSZdwf4QigVucXW
nLKvOxUqyW+f3GtFw1KkUb/N/v7g/g71LM37HSlIkPy/yvOqan/6c15PZh8/UCX3
RxzrHcHRXWPre6Aj3+2+1pKpcUBBbslyj/Ec9RrL6Nd+V9f4N/Ygnv2Ya7ZcVBn+
QwTrb/t4x39Pl2OG1M8e7fbYzMvI4Lyftx5LnLBzMVKpP/UdeDbV4zTima9CPF/Z
S/1OWbVwyqxHkJR33dPnwRtPQsf76Qycsev7xGIkMZ9T+97nal4/Mjgfx86B0Jnd
dujIHcbhVYI19GtWoBfeCxAm+bNfnTQ/d05X2KkUpHO/VaFXz60wYS37t52eotlU
l4/10v0orHGlYblIo5+ZDLJ1zFR38tb9WEu2meb5j8hXYD37b9jsbwz58C+Cpfll
z1WpjJeQQf1+Xv/gyqEBLKBuKSps+nZiPCLJjdEx23s1FxFK7vA9ZLrf9xcSmNfx
W0WPPukytPSr5X17sr6GeWVku19Z/K32j9jAerstJuTdlHxoOX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/6qC9WVF
l1c1utz+R5e7el1ajJ9uzQjix7zYnws8fOtbUuKzsD0JOo5IrvS569OFPcfaJMhH
Yr8u+GvB3rG1CLW9dAcfg7tPOsReB9fj/NWDkGWnhkcD4mO+Pa6f8fDNBQ+ll8cO
DPuUYYgcN8hCF9/y8przd3vZ75Jn9U5UumOwxzD+dC6K/Y8Pq1XpB6hBRRHAhj7s
uguHrUEiZZ3xTMZJjVH/9rQJn00V+EQaY+yMA3fR/hewd0pDHf4FEG0kp0gIAAAA
AAhI

`observer world`
H4sIAAAAAAAAAGMQSMhPKk4tKkstUiguSSxJTXBggAMrBq6CnPyS8syU9NQSIJ+F
gS0nNS+9JAPI5mBgB8kVpOaBlXKDNSPJQgAjCAMA+OWZc2QAAAAAAABk

`patch world`
[encoded data, 30680 characters]
2+qd+wQAAAT7nQ==

`turtle world`
[encoded data, 4225 characters]

