BouncingBalls.nlogo 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. extensions [reflection]
  2. turtles-own
  3. [
  4. velocity-x
  5. velocity-y
  6. new-velocity-x
  7. new-velocity-y
  8. my-state
  9. ]
  10. to setup
  11. random-seed 1337
  12. clear-all
  13. set-default-shape turtles "circle"
  14. reset-ticks
  15. end
  16. to go
  17. if mouse-down? [
  18. let grabbed one-of turtles with [ (distancexy mouse-xcor mouse-ycor) < size / 2 ]
  19. if grabbed != nobody
  20. [
  21. ask grabbed
  22. [
  23. select
  24. ]
  25. ]
  26. ]
  27. create-particle
  28. delete-particles-at-random
  29. check-state
  30. check-collisions
  31. move-particles
  32. tick
  33. display
  34. end
  35. to select
  36. set my-state "selected"
  37. end
  38. to check-state
  39. ask turtles with [my-state = "selected"]
  40. [
  41. set velocity-x 0
  42. set velocity-y 0
  43. set new-velocity-x velocity-x
  44. set new-velocity-y velocity-y
  45. set color orange
  46. ]
  47. end
  48. to delete-selected
  49. ask turtles with [my-state = "selected"]
  50. [
  51. die
  52. ]
  53. end
  54. to create-particle
  55. if ticks mod 30 = 0
  56. ;if count turtles < 2
  57. [
  58. let the-patch one-of patches with [ not any? turtles-here ]
  59. if the-patch != nobody
  60. [
  61. ask the-patch
  62. [
  63. let temp-size 0.5 + random-float 2.5
  64. ;let temp-xcor (min-pxcor - 0.5 + temp-size / 2) + random-float (max-pxcor + 0.499 - temp-size / 2)
  65. ;let temp-ycor (min-pycor - 0.5 + temp-size / 2) + random-float (max-pycor + 0.499 - temp-size / 2)
  66. sprout 1
  67. [
  68. set my-state "bouncing"
  69. set color red
  70. ; setxy temp-xcor temp-ycor
  71. set size temp-size
  72. if xcor > max-pxcor + 0.499 - size / 2
  73. [
  74. set xcor max-pxcor + 0.499 - size / 2
  75. ]
  76. if xcor < min-pxcor - 0.5 + size / 2
  77. [
  78. set xcor min-pxcor - 0.5 + size / 2
  79. ]
  80. if ycor > max-pycor + 0.499 - size / 2
  81. [
  82. set ycor max-pycor + 0.499 - size / 2
  83. ]
  84. if ycor < min-pycor - 0.5 + size / 2
  85. [
  86. set ycor min-pycor - 0.5 + size / 2
  87. ]
  88. let colliding-turtle find-collision
  89. ifelse colliding-turtle != nobody
  90. [
  91. die
  92. ]
  93. [
  94. set velocity-x 0.05 + random-float 0.5
  95. set velocity-y 0.05 + random-float 0.5
  96. set new-velocity-x velocity-x
  97. set new-velocity-y velocity-y
  98. ]
  99. ]
  100. ]
  101. ]
  102. ]
  103. end
  104. to delete-particles-at-random
  105. ask turtles
  106. [
  107. if my-state = "bouncing" and random-float 1.0 < 0.005
  108. [
  109. die
  110. ]
  111. ]
  112. end
  113. to move-particles
  114. ask turtles
  115. [
  116. move
  117. ]
  118. end
  119. to move
  120. let new-x xcor + velocity-x
  121. let new-y ycor + velocity-y
  122. if new-x > max-pxcor + 0.499 - size / 2 or new-x < min-pxcor - 0.5 + size / 2
  123. [
  124. set velocity-x (- velocity-x)
  125. set new-velocity-x velocity-x
  126. set new-x xcor + velocity-x
  127. ]
  128. if new-y > max-pycor + 0.499 - size / 2 or new-y < min-pycor - 0.5 + size / 2
  129. [
  130. set velocity-y (- velocity-y)
  131. set new-velocity-y velocity-y
  132. set new-y ycor + velocity-y
  133. ]
  134. setxy new-x new-y
  135. end
  136. to check-collisions
  137. ask turtles with [my-state = "bouncing"]
  138. [
  139. let colliding-turtle find-collision
  140. if colliding-turtle != nobody
  141. [
  142. let other-state [my-state] of colliding-turtle
  143. ifelse other-state = "selected"
  144. [
  145. set new-velocity-x (- velocity-x)
  146. set new-velocity-y (- velocity-y)
  147. ]
  148. [
  149. let ct-velocity-x [velocity-x] of colliding-turtle
  150. let ct-velocity-y [velocity-y] of colliding-turtle
  151. set new-velocity-x ct-velocity-x
  152. set new-velocity-y ct-velocity-y
  153. ]
  154. ]
  155. ]
  156. ask turtles [
  157. set velocity-x new-velocity-x
  158. set velocity-y new-velocity-y
  159. ]
  160. end
  161. to-report find-collision
  162. report one-of other turtles with [collision? self myself]
  163. end
  164. to-report collision? [turtle-a turtle-b]
  165. let ax [xcor] of turtle-a
  166. let ay [ycor] of turtle-a
  167. let asize [size] of turtle-a
  168. let bx [xcor] of turtle-b
  169. let by [ycor] of turtle-b
  170. let bsize [size] of turtle-b
  171. let distance-x ax - bx
  172. let distance-y ay - by
  173. let the-distance sqrt ((distance-x * distance-x) + (distance-y * distance-y))
  174. ;let mylist (list ax ay asize bx by bsize distance-x distance-y the-distance)
  175. ;print mylist
  176. report (the-distance < (asize / 2 + bsize / 2))
  177. end
  178. @#$#@#$#@
  179. GRAPHICS-WINDOW
  180. 230
  181. 80
  182. 576
  183. 427
  184. -1
  185. -1
  186. 13.0
  187. 1
  188. 10
  189. 1
  190. 1
  191. 1
  192. 0
  193. 0
  194. 0
  195. 1
  196. 0
  197. 25
  198. 0
  199. 25
  200. 1
  201. 1
  202. 1
  203. ticks
  204. 9999.0
  205. BUTTON
  206. 76
  207. 198
  208. 139
  209. 231
  210. NIL
  211. go
  212. T
  213. 1
  214. T
  215. OBSERVER
  216. NIL
  217. NIL
  218. NIL
  219. NIL
  220. 1
  221. BUTTON
  222. 43
  223. 111
  224. 106
  225. 144
  226. NIL
  227. setup
  228. NIL
  229. 1
  230. T
  231. OBSERVER
  232. NIL
  233. NIL
  234. NIL
  235. NIL
  236. 1
  237. BUTTON
  238. 110
  239. 111
  240. 188
  241. 145
  242. step
  243. go
  244. NIL
  245. 1
  246. T
  247. OBSERVER
  248. NIL
  249. NIL
  250. NIL
  251. NIL
  252. 1
  253. BUTTON
  254. 43
  255. 150
  256. 161
  257. 184
  258. delete-selected
  259. delete-selected
  260. NIL
  261. 1
  262. T
  263. OBSERVER
  264. NIL
  265. NIL
  266. NIL
  267. NIL
  268. 1
  269. @#$#@#$#@
  270. ## WHAT IS IT?
  271. (a general understanding of what the model is trying to show or explain)
  272. ## HOW IT WORKS
  273. (what rules the agents use to create the overall behavior of the model)
  274. ## HOW TO USE IT
  275. (how to use the model, including a description of each of the items in the Interface tab)
  276. ## THINGS TO NOTICE
  277. (suggested things for the user to notice while running the model)
  278. ## THINGS TO TRY
  279. (suggested things for the user to try to do (move sliders, switches, etc.) with the model)
  280. ## EXTENDING THE MODEL
  281. (suggested things to add or change in the Code tab to make the model more complicated, detailed, accurate, etc.)
  282. ## NETLOGO FEATURES
  283. (interesting or unusual features of NetLogo that the model uses, particularly in the Code tab; or where workarounds were needed for missing features)
  284. ## RELATED MODELS
  285. (models in the NetLogo Models Library and elsewhere which are of related interest)
  286. ## CREDITS AND REFERENCES
  287. (a reference to the model's URL on the web if it has one, as well as any other necessary credits, citations, and links)
  288. @#$#@#$#@
  289. default
  290. true
  291. 0
  292. Polygon -7500403 true true 150 5 40 250 150 205 260 250
  293. airplane
  294. true
  295. 0
  296. Polygon -7500403 true true 150 0 135 15 120 60 120 105 15 165 15 195 120 180 135 240 105 270 120 285 150 270 180 285 210 270 165 240 180 180 285 195 285 165 180 105 180 60 165 15
  297. arrow
  298. true
  299. 0
  300. Polygon -7500403 true true 150 0 0 150 105 150 105 293 195 293 195 150 300 150
  301. box
  302. false
  303. 0
  304. Polygon -7500403 true true 150 285 285 225 285 75 150 135
  305. Polygon -7500403 true true 150 135 15 75 150 15 285 75
  306. Polygon -7500403 true true 15 75 15 225 150 285 150 135
  307. Line -16777216 false 150 285 150 135
  308. Line -16777216 false 150 135 15 75
  309. Line -16777216 false 150 135 285 75
  310. bug
  311. true
  312. 0
  313. Circle -7500403 true true 96 182 108
  314. Circle -7500403 true true 110 127 80
  315. Circle -7500403 true true 110 75 80
  316. Line -7500403 true 150 100 80 30
  317. Line -7500403 true 150 100 220 30
  318. butterfly
  319. true
  320. 0
  321. Polygon -7500403 true true 150 165 209 199 225 225 225 255 195 270 165 255 150 240
  322. Polygon -7500403 true true 150 165 89 198 75 225 75 255 105 270 135 255 150 240
  323. Polygon -7500403 true true 139 148 100 105 55 90 25 90 10 105 10 135 25 180 40 195 85 194 139 163
  324. Polygon -7500403 true true 162 150 200 105 245 90 275 90 290 105 290 135 275 180 260 195 215 195 162 165
  325. Polygon -16777216 true false 150 255 135 225 120 150 135 120 150 105 165 120 180 150 165 225
  326. Circle -16777216 true false 135 90 30
  327. Line -16777216 false 150 105 195 60
  328. Line -16777216 false 150 105 105 60
  329. car
  330. false
  331. 0
  332. Polygon -7500403 true true 300 180 279 164 261 144 240 135 226 132 213 106 203 84 185 63 159 50 135 50 75 60 0 150 0 165 0 225 300 225 300 180
  333. Circle -16777216 true false 180 180 90
  334. Circle -16777216 true false 30 180 90
  335. Polygon -16777216 true false 162 80 132 78 134 135 209 135 194 105 189 96 180 89
  336. Circle -7500403 true true 47 195 58
  337. Circle -7500403 true true 195 195 58
  338. circle
  339. false
  340. 0
  341. Circle -7500403 true true 0 0 300
  342. circle 2
  343. false
  344. 0
  345. Circle -7500403 true true 0 0 300
  346. Circle -16777216 true false 30 30 240
  347. cow
  348. false
  349. 0
  350. Polygon -7500403 true true 200 193 197 249 179 249 177 196 166 187 140 189 93 191 78 179 72 211 49 209 48 181 37 149 25 120 25 89 45 72 103 84 179 75 198 76 252 64 272 81 293 103 285 121 255 121 242 118 224 167
  351. Polygon -7500403 true true 73 210 86 251 62 249 48 208
  352. Polygon -7500403 true true 25 114 16 195 9 204 23 213 25 200 39 123
  353. cylinder
  354. false
  355. 0
  356. Circle -7500403 true true 0 0 300
  357. dot
  358. false
  359. 0
  360. Circle -7500403 true true 90 90 120
  361. face happy
  362. false
  363. 0
  364. Circle -7500403 true true 8 8 285
  365. Circle -16777216 true false 60 75 60
  366. Circle -16777216 true false 180 75 60
  367. Polygon -16777216 true false 150 255 90 239 62 213 47 191 67 179 90 203 109 218 150 225 192 218 210 203 227 181 251 194 236 217 212 240
  368. face neutral
  369. false
  370. 0
  371. Circle -7500403 true true 8 7 285
  372. Circle -16777216 true false 60 75 60
  373. Circle -16777216 true false 180 75 60
  374. Rectangle -16777216 true false 60 195 240 225
  375. face sad
  376. false
  377. 0
  378. Circle -7500403 true true 8 8 285
  379. Circle -16777216 true false 60 75 60
  380. Circle -16777216 true false 180 75 60
  381. Polygon -16777216 true false 150 168 90 184 62 210 47 232 67 244 90 220 109 205 150 198 192 205 210 220 227 242 251 229 236 206 212 183
  382. fish
  383. false
  384. 0
  385. Polygon -1 true false 44 131 21 87 15 86 0 120 15 150 0 180 13 214 20 212 45 166
  386. Polygon -1 true false 135 195 119 235 95 218 76 210 46 204 60 165
  387. Polygon -1 true false 75 45 83 77 71 103 86 114 166 78 135 60
  388. Polygon -7500403 true true 30 136 151 77 226 81 280 119 292 146 292 160 287 170 270 195 195 210 151 212 30 166
  389. Circle -16777216 true false 215 106 30
  390. flag
  391. false
  392. 0
  393. Rectangle -7500403 true true 60 15 75 300
  394. Polygon -7500403 true true 90 150 270 90 90 30
  395. Line -7500403 true 75 135 90 135
  396. Line -7500403 true 75 45 90 45
  397. flower
  398. false
  399. 0
  400. Polygon -10899396 true false 135 120 165 165 180 210 180 240 150 300 165 300 195 240 195 195 165 135
  401. Circle -7500403 true true 85 132 38
  402. Circle -7500403 true true 130 147 38
  403. Circle -7500403 true true 192 85 38
  404. Circle -7500403 true true 85 40 38
  405. Circle -7500403 true true 177 40 38
  406. Circle -7500403 true true 177 132 38
  407. Circle -7500403 true true 70 85 38
  408. Circle -7500403 true true 130 25 38
  409. Circle -7500403 true true 96 51 108
  410. Circle -16777216 true false 113 68 74
  411. Polygon -10899396 true false 189 233 219 188 249 173 279 188 234 218
  412. Polygon -10899396 true false 180 255 150 210 105 210 75 240 135 240
  413. house
  414. false
  415. 0
  416. Rectangle -7500403 true true 45 120 255 285
  417. Rectangle -16777216 true false 120 210 180 285
  418. Polygon -7500403 true true 15 120 150 15 285 120
  419. Line -16777216 false 30 120 270 120
  420. leaf
  421. false
  422. 0
  423. Polygon -7500403 true true 150 210 135 195 120 210 60 210 30 195 60 180 60 165 15 135 30 120 15 105 40 104 45 90 60 90 90 105 105 120 120 120 105 60 120 60 135 30 150 15 165 30 180 60 195 60 180 120 195 120 210 105 240 90 255 90 263 104 285 105 270 120 285 135 240 165 240 180 270 195 240 210 180 210 165 195
  424. Polygon -7500403 true true 135 195 135 240 120 255 105 255 105 285 135 285 165 240 165 195
  425. line
  426. true
  427. 0
  428. Line -7500403 true 150 0 150 300
  429. line half
  430. true
  431. 0
  432. Line -7500403 true 150 0 150 150
  433. pentagon
  434. false
  435. 0
  436. Polygon -7500403 true true 150 15 15 120 60 285 240 285 285 120
  437. person
  438. false
  439. 0
  440. Circle -7500403 true true 110 5 80
  441. Polygon -7500403 true true 105 90 120 195 90 285 105 300 135 300 150 225 165 300 195 300 210 285 180 195 195 90
  442. Rectangle -7500403 true true 127 79 172 94
  443. Polygon -7500403 true true 195 90 240 150 225 180 165 105
  444. Polygon -7500403 true true 105 90 60 150 75 180 135 105
  445. plant
  446. false
  447. 0
  448. Rectangle -7500403 true true 135 90 165 300
  449. Polygon -7500403 true true 135 255 90 210 45 195 75 255 135 285
  450. Polygon -7500403 true true 165 255 210 210 255 195 225 255 165 285
  451. Polygon -7500403 true true 135 180 90 135 45 120 75 180 135 210
  452. Polygon -7500403 true true 165 180 165 210 225 180 255 120 210 135
  453. Polygon -7500403 true true 135 105 90 60 45 45 75 105 135 135
  454. Polygon -7500403 true true 165 105 165 135 225 105 255 45 210 60
  455. Polygon -7500403 true true 135 90 120 45 150 15 180 45 165 90
  456. sheep
  457. false
  458. 15
  459. Circle -1 true true 203 65 88
  460. Circle -1 true true 70 65 162
  461. Circle -1 true true 150 105 120
  462. Polygon -7500403 true false 218 120 240 165 255 165 278 120
  463. Circle -7500403 true false 214 72 67
  464. Rectangle -1 true true 164 223 179 298
  465. Polygon -1 true true 45 285 30 285 30 240 15 195 45 210
  466. Circle -1 true true 3 83 150
  467. Rectangle -1 true true 65 221 80 296
  468. Polygon -1 true true 195 285 210 285 210 240 240 210 195 210
  469. Polygon -7500403 true false 276 85 285 105 302 99 294 83
  470. Polygon -7500403 true false 219 85 210 105 193 99 201 83
  471. square
  472. false
  473. 0
  474. Rectangle -7500403 true true 30 30 270 270
  475. square 2
  476. false
  477. 0
  478. Rectangle -7500403 true true 30 30 270 270
  479. Rectangle -16777216 true false 60 60 240 240
  480. star
  481. false
  482. 0
  483. Polygon -7500403 true true 151 1 185 108 298 108 207 175 242 282 151 216 59 282 94 175 3 108 116 108
  484. target
  485. false
  486. 0
  487. Circle -7500403 true true 0 0 300
  488. Circle -16777216 true false 30 30 240
  489. Circle -7500403 true true 60 60 180
  490. Circle -16777216 true false 90 90 120
  491. Circle -7500403 true true 120 120 60
  492. tree
  493. false
  494. 0
  495. Circle -7500403 true true 118 3 94
  496. Rectangle -6459832 true false 120 195 180 300
  497. Circle -7500403 true true 65 21 108
  498. Circle -7500403 true true 116 41 127
  499. Circle -7500403 true true 45 90 120
  500. Circle -7500403 true true 104 74 152
  501. triangle
  502. false
  503. 0
  504. Polygon -7500403 true true 150 30 15 255 285 255
  505. triangle 2
  506. false
  507. 0
  508. Polygon -7500403 true true 150 30 15 255 285 255
  509. Polygon -16777216 true false 151 99 225 223 75 224
  510. truck
  511. false
  512. 0
  513. Rectangle -7500403 true true 4 45 195 187
  514. Polygon -7500403 true true 296 193 296 150 259 134 244 104 208 104 207 194
  515. Rectangle -1 true false 195 60 195 105
  516. Polygon -16777216 true false 238 112 252 141 219 141 218 112
  517. Circle -16777216 true false 234 174 42
  518. Rectangle -7500403 true true 181 185 214 194
  519. Circle -16777216 true false 144 174 42
  520. Circle -16777216 true false 24 174 42
  521. Circle -7500403 false true 24 174 42
  522. Circle -7500403 false true 144 174 42
  523. Circle -7500403 false true 234 174 42
  524. turtle
  525. true
  526. 0
  527. Polygon -10899396 true false 215 204 240 233 246 254 228 266 215 252 193 210
  528. Polygon -10899396 true false 195 90 225 75 245 75 260 89 269 108 261 124 240 105 225 105 210 105
  529. Polygon -10899396 true false 105 90 75 75 55 75 40 89 31 108 39 124 60 105 75 105 90 105
  530. Polygon -10899396 true false 132 85 134 64 107 51 108 17 150 2 192 18 192 52 169 65 172 87
  531. Polygon -10899396 true false 85 204 60 233 54 254 72 266 85 252 107 210
  532. Polygon -7500403 true true 119 75 179 75 209 101 224 135 220 225 175 261 128 261 81 224 74 135 88 99
  533. wheel
  534. false
  535. 0
  536. Circle -7500403 true true 3 3 294
  537. Circle -16777216 true false 30 30 240
  538. Line -7500403 true 150 285 150 15
  539. Line -7500403 true 15 150 285 150
  540. Circle -7500403 true true 120 120 60
  541. Line -7500403 true 216 40 79 269
  542. Line -7500403 true 40 84 269 221
  543. Line -7500403 true 40 216 269 79
  544. Line -7500403 true 84 40 221 269
  545. wolf
  546. false
  547. 0
  548. Polygon -16777216 true false 253 133 245 131 245 133
  549. Polygon -7500403 true true 2 194 13 197 30 191 38 193 38 205 20 226 20 257 27 265 38 266 40 260 31 253 31 230 60 206 68 198 75 209 66 228 65 243 82 261 84 268 100 267 103 261 77 239 79 231 100 207 98 196 119 201 143 202 160 195 166 210 172 213 173 238 167 251 160 248 154 265 169 264 178 247 186 240 198 260 200 271 217 271 219 262 207 258 195 230 192 198 210 184 227 164 242 144 259 145 284 151 277 141 293 140 299 134 297 127 273 119 270 105
  550. Polygon -7500403 true true -1 195 14 180 36 166 40 153 53 140 82 131 134 133 159 126 188 115 227 108 236 102 238 98 268 86 269 92 281 87 269 103 269 113
  551. x
  552. false
  553. 0
  554. Polygon -7500403 true true 270 75 225 30 30 225 75 270
  555. Polygon -7500403 true true 30 75 75 30 270 225 225 270
  556. @#$#@#$#@
  557. NetLogo 6.0.3
  558. @#$#@#$#@
  559. @#$#@#$#@
  560. @#$#@#$#@
  561. @#$#@#$#@
  562. @#$#@#$#@
  563. default
  564. 0.0
  565. -0.2 0 0.0 1.0
  566. 0.0 1 1.0 0.0
  567. 0.2 0 0.0 1.0
  568. link direction
  569. true
  570. 0
  571. Line -7500403 true 150 150 90 180
  572. Line -7500403 true 150 150 210 180
  573. @#$#@#$#@
  574. 0
  575. @#$#@#$#@