2016-01-13 Gravity Example:

posted Jan 13, 2016, 5:13 AM by Samuel Konstantinovich   [ updated Jan 13, 2016, 7:26 AM ]
Goal Project Time, Gravity Example:

globals [g]
turtles-own [xspeed yspeed]
to setup
  ca
  reset-ticks
  set g .2
  crt 1 [set size 4 set shape "sheep" set ycor 6 set yspeed 0]
  crt 1[
     set shape "circle"
     set size 4
     set xspeed random 2 + 1
     set yspeed 3
     setxy -20 -14
  ]
  ask patches with [pycor < -15][set pcolor white]    
end

to go
  every .1
  [
  gravity
  movestuff
  ]
  tick
end

to movestuff
  ask turtles
  [
    set ycor ycor + yspeed 
    set xcor xcor + xspeed
  ]
end  

to gravity
  ask turtles[ 
    set yspeed yspeed - g    
    checkGround; makes the y speed 0 if you are standing on ground

 
    ;not something you need but illustrates the speeds:
    set plabel word  xspeed word "," (round (yspeed * 10) / 10.0)
    
  ]
end



;these change a lot depending on your model

to checkGround
  if isOnGround? 
    [
      set yspeed 0
]
end

to-report isOnGround?
  report  pcolor  = white
end

Comments