Courses/CS 461/Winter 2006/Nghia Phan/Nghia Phan/Week 3
From CSWiki
< Courses | CS 461 | Winter 2006 | Nghia Phan
Contents |
[edit] Ball's moving in the direction of the mouse
I build this model because I am interesting in how Netlogo captures the mouse events. When you run this model, you will notice that an arrow on the ball will move in the direction of the mouse. However, you need to move the mouse inside the view to make such effect.
[edit] Screen Shot
[edit] Download
Ball moving in direction of mouse.nlogo
[edit] Code
globals
[
step
main_ball_xcor
main_ball_ycor
main_ball_size
main_ball_direction
ball_is_not_moving?
mass_of_edge
force_of_friction
]
breeds
[
balls
direction_guides
]
balls-own
[
ball_mass
ball_velocity
ball_velocity_x_component
ball_velocity_y_component
]
direction_guides-own
[
]
;;This is the procedure to setup the model
to setup
ca
setup-turtles
end
;;******************************************************************************************************
;;This is the procedure to setup the turtles
to setup-turtles
set main_ball_xcor 0
set main_ball_ycor -15
set main_ball_size 2.0
set main_ball_direction 0
set mass_of_edge (Mass * 2)
set force_of_friction (Velocity / 1000)
set ball_is_not_moving? true
create-custom-direction_guides 1
[
set xcor main_ball_xcor
set ycor main_ball_ycor
jump main_ball_size
]
create-custom-balls 1
[
set shape "circle"
set xcor main_ball_xcor
set ycor main_ball_ycor
set color white
set size main_ball_size
set heading main_ball_direction
set ball_mass Mass
set ball_velocity Velocity
set ball_velocity_x_component ((ball_velocity) * (cos heading))
set ball_velocity_y_component ((ball_velocity) * (sin heading))
]
end
;;******************************************************************************************************
to go
if (mouse-inside? and ball_is_not_moving?)
[
ask turtle 1
[
set heading towardsxy-nowrap mouse-xcor mouse-ycor
set main_ball_direction heading
]
ask direction_guides
[
set xcor main_ball_xcor
set ycor main_ball_ycor
set heading main_ball_direction
jump (main_ball_size)
]
]
if (mouse-down? and ball_is_not_moving?)
[
set ball_is_not_moving? false
]
ifelse (not ball_is_not_moving?)
[
ask turtle 1
[
if (mouse-down? and ball_velocity <= 0)
[
set ball_velocity Velocity
set ball_velocity_x_component ((ball_velocity) * (cos heading))
set ball_velocity_y_component ((ball_velocity) * (sin heading))
]
;;Bouds to left/right of the wall
if (pxcor-of patch-ahead 1 >= screen-edge-x or pxcor-of patch-ahead 1 <= (- screen-edge-x))
[
set heading (- heading)
]
;;Bounds to top/bottom of the wall
if (pycor-of patch-ahead 1 >= screen-edge-y or pycor-of patch-ahead 1 <= (- screen-edge-y))
[
set heading (180 - heading)
]
set step ball_velocity
set main_ball_direction heading
ifelse (ball_velocity >= 0)
[
fd step
]
[
set ball_is_not_moving? true
]
set main_ball_xcor xcor
set main_ball_ycor ycor
set ball_velocity (ball_velocity - force_of_friction)
set ball_velocity_x_component ((ball_velocity) * (cos heading))
set ball_velocity_y_component ((ball_velocity) * (sin heading))
]
ask direction_guides
[
hideturtle
setxy main_ball_xcor main_ball_ycor
set heading main_ball_direction
jump (main_ball_size)
]
]
[
ask direction_guides
[
showturtle
]
]
end

