Collisions
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.
Turtle procedures
turtles-own [
speed ; particle's speed
energy ; particle's energy
totalenergy ; sum of the energies of this particle and the one it has collided with
]
;; go procedure
;; called by the go button
;; This is the main loop of the program. At each iteration, particles check to see
;; if they are going to hit a wall or collide with another turtle and react accordingly.
;; Then the particle moves forwards and the wall gets cleaned up if the particle took
;; a notch out of it. Finally the particle's color gets scaled to reflect its energy.
;;
to go
check-patches
collide
fd speed
getout
scale-color yellow energy 0 500
end
;; collide procedure
;; called by the go procedure
;; If there is a particle on the patch with you, then change heading of both particles,
;; give each particle some portion of the sum of their energies, and set speed based on
;; the energy.
;; Finally check if the particle is going to hit a wall and go forward one step.
;;
to collide
grab one-of-turtles-here
[seth random 360
seth-of partner random 360
settotalenergy energy + (energy-of partner)
setenergy (totalenergy * (((random 99) + 1) / 100))
setenergy-of partner totalenergy - energy
setspeed (sqrt (energy / 500))
setspeed-of partner (sqrt ((energy-of partner)/ 500))
check-patches
fd 1]
end
;; check-patches procedure
;; called by the go and the collide procedures
;; Check to see if you are at a wall and if so, change energy, speed and heading, then call
;; check-patches recursively in case you moved up to another wall (if in a corner.)
;;
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
;; getout procedure
;; called by the go procedure and by the observer setup procedure
;; Check to see if you need to redreaw the wall because the particle has taken a notch out.
;; This becomes necessary when the pen-down button has been clicked and the particle draws
;; on top of the wall.
;;
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
;; redraw procedure
;; called by the getout procedure
;; Redraw the wall
;;
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 procedures
;; setup procedure
;; called by the setup button
;; Kill any leftover turtles then create the number of turtles the user specified using the number
;; slider. Position the turtles randomly, set their energy randomly, set their speed and color
;; based on their energy. Finally, redraw the wall if necessary.
;;
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
