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" workbook. 

WHAT IS IT? ----------- 
This project models collisions between point-like objects.   Each turtle moves around the screen, at a speed based on its  energy variable. When a turtle runs into a wall, its energy  value changes and it bounces off the wall.  When a turtle runs into another turtle, the turtles randomly  combine their energies and bounce off of each other in new  directions.  

HOW TO USE IT ------------- 
Click the SETUP button to set up the colored walls and the  distribution of turtles. Click GO to start the turtles  moving.  Click PD to watch the turtles draw as the move, and  click PU to stop the drawing.   CLEAR-PATH clears the trails which the turtles have left.  The NUMBER slider controls the number of turtles in the  simulation. (If you reset the number of turtles, you will  need to press GO again to stop the simulation and then  click SETUP for the changes to take effect.)  

THINGS TO NOTICE ---------------- 
Watch the distribution of the energy amoung the turtles.  What does this tell you about how much energy the turtle had to  begin with? About the way the turtles exchange energy?  What do you think would happen if there were walls to bounce off  of in the middle of the screen, as well as the edges?  

EXPLORATIONS ------------ 
The turtles don't exchange energy or bounce off of each other in  a realistic way. How would you change this project to more  closely model a physical situation? How do you think this  change would affect the motion of the simulation?   The distribution of energy among the turtles? 

STARLOGO FEATURES ----------------- 
This project demonstrates a simple use of the "grab" command, in the "collide" procedure. Grab provides an easy way for a turtle to communicate with one or more other turtles.


`interface`
SLCanvas
top-left 22 210

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, 5200 characters]
DhUAABUO2Q==

`template`
null

`settings`
patch-size 4
num-shapes 256
screen-half-width 50
screen-half-height 50
interface-window-xcor 236
interface-window-ycor 55
interface-window-size 643 439
output-window-xcor 0
output-window-ycor 0
output-window-width 300
output-window-height 219
info-window-xcor 92
info-window-ycor 64
info-window-width 500
info-window-height 419
control-center-xcor 18
control-center-ycor 75
control-center-width 315
control-center-height 522
turtle-command-center-height 148
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/1OWbVwyqxHkJR33dPnwRtPQsf76Qycsev7xGIkMZ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sIl8gnjbSfSHtM
2AYTLGa+TdYILebfJsEDL2TK83dZjJ5ei8Ta+uhriQ+Y37DSREkl5sjhNvu04j82
catiaVtmhFGaLYEKHUJ/85RvvqvGwsUJz39daOpKmw1Yaackxbv68rqyqO9FVvud
8P6tCo22oS3nrrSqh02+2JcX7bqkSv4QKvbepX1YUxrb/1YWK4njOmRcV04Wj33E
uKaM65hxzZi/rHCdML9pGfM/sdqWf8GwXjGs93Q2Z7kXDOs1w3rDMCyZ/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]

