to setup ca set-default-shape turtles "circle" draw-walls cct 5 ;; create some turtles [ randomize ] ;; set random coordinates and random direction end ; draws the boundaries (walls) of the "billiard table" to draw-walls ; draw left and right walls ask patches with [abs pxcor = screen-edge-x] [ set pcolor blue ] ; draw top and bottom walls ask patches with [abs pycor = screen-edge-y] [ set pcolor blue ] end ; set random coordinates and random direction to randomize setxy (random screen-size-x) (random screen-size-y) rt random 360 if pcolor = blue ; if it's on the wall... [ randomize ] ; ...try again end to go ;; turtle procedure every 0.5 - speed / 10 [ ifelse leave-trace? ;; the turtle puts its pen up or down depending on the [ pd ] ;; value of the LEAVE-TRACE? switch [ pu ] bounce fd 1 ] end ;; this procedure checks the coordinates and makes the turtles ;; reflect according to the law that the angle of reflection is ;; equal to the angle of incidence to bounce ;; turtle procedure locals [ new-px new-py ] ; get the coordinates of the patch we'll be on if we go forward 1 set new-px round (xcor + dx) set new-py round (ycor + dy) ; check: hitting left or right wall? if (abs new-px = screen-edge-x) ; if so, reflect heading around x axis [ set heading (0 - heading) ] ; check: hitting top or bottom wall? if (abs new-py = screen-edge-y) ; if so, reflect heading around y axis [ set heading (180 - heading) ] end ; This model was created as part of the project: CONNECTED MATHEMATICS: ; MAKING SENSE OF COMPLEX PHENOMENA THROUGH BUILDING OBJECT-BASED PARALLEL ; MODELS (OBPML) ; The project gratefully acknowledges the support of the National Science ; Foundation (Applications of Advanced Technologies Program) -- grant numbers ; RED #9552950 and REC #9632612. ; ; Converted from StarLogoT to NetLogo, 2001 ; Copyright 1999 by Uri Wilensky. All rights reserved. ; ; Permission to use, copy, or modify this software and its documentation for ; educational and research purposes only and without fee is hereby granted, ; provided that this copyright notice and the original authors' names appear ; on all copies and supporting documentation. For any other uses of this ; software, in original or modified form, including but not limited to ; distribution in whole or in part, specific prior permission must be ; obtained from Uri Wilensky. These programs shall not be used, rewritten, or ; adapted as the basis of a commercial software or hardware product without ; first obtaining appropriate licenses from Uri Wilensky. We make no ; representations about the suitability of this software for any purpose. It ; is provided "as is" without express or implied warranty. @#$#@#$#@ GRAPHICS-WINDOW 308 10 634 336 20 20 7.951219512195122 1 10 0 0 CC-WINDOW 14 128 293 486 Command Center BUTTON 77 23 135 56 NIL setup NIL 1 T OBSERVER BUTTON 148 23 203 56 go go T 1 T TURTLE SWITCH 28 77 157 110 leave-trace? leave-trace? 1 1 -1000 SLIDER 170 77 262 110 Speed Speed 0.0 5.0 4.7 0.1 1 NIL @#$#@#$#@ WHAT IS IT? ------------ This demo shows how to make turtles bounce off the walls. @#$#@#$#@ default true 0 Polygon -7566196 true true 150 5 40 250 150 205 260 250 circle true 0 Circle -7566196 true true 10 10 283 @#$#@#$#@ NetLogo 1.1 @#$#@#$#@ @#$#@#$#@ @#$#@#$#@