Sunday, January 12, 2014

wARTerfall Update

The wARTerfall, Updated!

   Upon extremely popular demand, similar to that for the next Nickelback album, I decided to throw in some more features for my wARTerfall. Some of those features include:
  • transparency slider
  • different rain shapes
  • circular area of effect for the mouse click 
    • To see if a turtle is in a circle with an origin at a mouse click, you can use:

if mouse-down?
        [
          ask turtles
            [
              if ( ( xcor - mouseX ) ^ 2 ) + ( ( ycor - mouseY) ^ 2 ) < (click-effect-size ^ 2)
               [                   
                  set color (list (item 0 extract-rgb clickcolor) (item 1 extract-rgb clickcolor) (item 2 extract-rgb clickcolor) (random 40 + set-water-transparency))

  Where mouseX is the mouse click x coordinate, mouseY for the y. click-effect size is the radius of the clicking area. The super long line simply sets those turtles to a certain color - my point in adding it is to demonstrate how you can get the RGB color values of a turtle so that you can set the transparency. The click would initially color the turtles a flat color, but now they have to follow the same transparency rules as the rest of the turtles.

   I'm working on introducing an alternative click effect that creates a whirlpool where the turtles in the click area spiral toward it and then get trapped but I havent figured out the spiral math yet.

My code is posted at the bottom, contact me if you'd like to have the actual file. I could run my mouth more about the program . . . or I could post pretty pictures!















;;-------------------------------------------------------
;;Turtle Properties: xvelocity, yvelocity, age
;;-------------------------------------------------------
turtles-own [ xvel yvel] 


;;-------------------------------------------------------
;;Initiate Particles
;;-------------------------------------------------------
to initiate
    
    set xcor random-xcor
    set ycor max-pycor - random 3
    set size random-float water-size
    let turtle-color random 7 + water-color
    set color turtle-color
    set color (list (item 0 extract-rgb turtle-color) (item 1 extract-rgb turtle-color) (item 2 extract-rgb turtle-color) (random 40 + set-water-transparency))
    
    set shape water-shape    
    set xvel 0
    set yvel -.02
  
end

;;-------------------------------------------------------
;;Prepares scene, Resets values
;;-------------------------------------------------------
to setup
  clear-all
  reset-ticks
  create-turtles 1
  ask turtles[
  initiate
  ]
end

;;-------------------------------------------------------
;;Procedure to create gravity
;;-------------------------------------------------------
to apply-gravity
  set yvel yvel - gravity 
end

;;-------------------------------------------------------
;;Random drifting of particles
;;-------------------------------------------------------
to apply-random-spreading
 set yvel yvel + random-float 0.01 - random-float 0.01
 set xvel xvel + random-float 0.01 - random-float 0.01
end

;;-------------------------------------------------------
;;Wind Blowing West <-
;;-------------------------------------------------------
to apply-windWest
  set xvel xvel - wind-speed-west
end

;;-------------------------------------------------------
;;Wind Blowing East ->
;;-------------------------------------------------------
to apply-windEast
  set xvel xvel + wind-speed-east
end

;;-------------------------------------------------------
;;Spirals particles toward x, y
;;-------------------------------------------------------
to create-swirl [X Y]
  set xvel xvel + .006 * ycor
  set yvel yvel + -.006 * xcor
  facexy X Y
  ;;forward .001
  
end
;;-------------------------------------------------------
;;Interact
;;-------------------------------------------------------
to interact
  let mouseX mouse-xcor
  let mouseY mouse-ycor
  ifelse use-whirlpool?
  ;;=============================
  ;;Whirlpool
  ;;=============================
    [
      if mouse-down?
        [
          ask turtles
            [
              if ( ( xcor - mouseX ) ^ 2 ) + ( ( ycor - mouseY) ^ 2 ) < (click-effect-size ^ 2)
               [                   
                  ;;Make the water foamy
                  if random 30 = 0 [set color (list (255) (255) (255) (set-water-transparency))]
                  create-swirl mouseX mouseY
                ]
            ]
        ]
    ]
  ;;=============================
  ;;Color Change
  ;;=============================
    [
      if mouse-down?
        [
          ask turtles
            [
              if ( ( xcor - mouseX ) ^ 2 ) + ( ( ycor - mouseY) ^ 2 ) < (click-effect-size ^ 2)
                [
                  set color (list (item 0 extract-rgb clickcolor) (item 1 extract-rgb clickcolor) (item 2 extract-rgb clickcolor) (random 40 + set-water-transparency))
                ]
            ]
        ]
    ]
  ;;=============================
  
end
;;-------------------------------------------------------
;;Begins the Program (Main)
;;-------------------------------------------------------
to go
  create-turtles 1 + random 100 + amount-of-water
    [
      set color random 27 + 81
      initiate
    ]
  interact
  ask turtles [
      if usingpen?
      [
        set pen-size pensize
        pen-down
      ]
       
      apply-gravity
      apply-random-spreading
      apply-windEast
      apply-windWest
      let new-y ycor + yvel
      ifelse new-y < ((random 2) - 20) or new-y > ((random 1) + 19)
        [die]
        [set ycor ycor + yvel]
      set xcor xcor + xvel
     
  ]
  tick
  
end

4 comments:

  1. I like all the shapes of "water drops" you have in your waterfall. And the colors are pretty too! Thank you for having your codes posted. They are very helpful :)

    ReplyDelete
  2. These look awesome! I could imagine them being a screensaver or maybe even a music visualization program. I agree with Sara too that the other shapes look really good, especially the squares.

    ReplyDelete
  3. I see what you did there with the smiley face haha.

    ReplyDelete
  4. Thanks, everyone! I'll add the file to the freebox if any of you want to give it a spin.

    ReplyDelete