Libraries‎ > ‎Library‎ > ‎Interact with others‎ > ‎

Turn towards others

BehaviourComposer: ignore everything before this.
Begin micro-behaviour:
Begin description:
Compute the heading needed to turn towards other agents.
End description

Turn towards others

Begin NetLogo code:
substitute-text-area-for expression-for-others other all-individuals
substitute-text-area-for cycle-duration 1                      
substitute-text-area-for weight-expression 1
do-every cycle-duration
   [let weight 
    ask expression-for-others
        [ask myself 
            [set my-desired-headings 
                 my-desired-headings + 
                 weight * canonical-heading heading-towards-another myself
            set my-desired-headings-count my-desired-headings-count + weight]]]
End NetLogo code


This updates my-desired-headings by adding the headings towards each of the others. You can replace it an expression such as all-of-kind "Prototype1".  Or if you wish to limit this to only those within 3 units use (all-of-kind "Prototype1") in-radius 3.

You can change the weight expression if you want this to count more or less than other micro-behaviours that update my-desired-headings.

How this works

This updates my-desired-headings by adding all the headings to the others.  my-desired-headings/my-desired-headings-count computes the average desired heading. canonical-heading ensures that equivalent headings (e.g. -30 and 330) are treated the same.

Related micro-behaviours

Turn toward the desired-direction computed by other behaviours is need to actually turn the agent in the direction computed by this micro-behaviour.

Compute the heading needed to turn away from other agents can be used to avoid rather than follow other agents.


Turn towards others was implemented by Ken Kahn on 13 August 2010.

BehaviourComposer: ignore everything after this.