Surface Walking 3D

Surface Walking 3D preview image

1 collaborator

Uri_dolphin3 Uri Wilensky (Author)


(This model has yet to be categorized with any tags)
Model group CCL | Visible to everyone | Changeable by group members (CCL)
Model was written in NetLogo 3D 4.1pre7 • Viewed 549 times • Downloaded 18 times • Run 0 times
Download the 'Surface Walking 3D' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)


This model is a 3D version of a surface-walking algorithm used in "Surface Walk 2D". Turtles approximate a user-defined surface using a simple algorithm that considers the turtle's current position relative to neighboring surface patches.


Turtles exist in a world that consists of two different kinds of patches: surface and non-surface. Turtles follow two basic rules in order to walk along the patch-defined surface:

1) Look for opposite neighboring patches. If the turtle is currently on a surface patch, it will look for a non-surface one, and vice versa.

2) Of those neighboring patches of opposite type, identify the one that requires the smallest change in turtle angle for the turtle to face.

3) Adjust pitch and heading halfway to the patch that requires the smallest change in angle to face and take a step.

Why does this algorithm work? Consider any perturbation along a flat surface (in the case of 2D, a curve or angle along an otherwise straight line). In order to traverse the perturbation and remain on the surface, a turtle needs to find and remain on the 'edge' of the surface, and maintain its direction while traveling along this edge. Since turtles in NetLogo always report being on only one patch (the one that the center of their body is over), we maintain the turtle's position along the edge by having it search for a patch that is of opposite type to the patch it is positioned over (Rule 1).

Rule 2 enables the turtles to travel along that edge while maintaining their current direction. Consider a surface in 2D: turtles are able to travel along that surface in one of two opposite directions (for example, they can walk along a circle in either a clockwise or counterclockwise direction). If a surface is flat, a turtle need not change its heading at all to continue to travel in the same direction along that surface. But, if the surface is curved or has an angle, then in order to continue to stay along the edge of the surface the turtle must change its heading. If it changes its heading exactly 180 degrees, it will reverse its direction of travel. But whether the angle is concave or convex, it will be of some measure between 0 and 360 degrees (non-inclusive). Then the heading change required to continue along the surface without reversing the direction of travel will be less than that required to reverse direction.

Finally, Rule 3 reduces the 'weaving' effects produced by turtles moving toward surface and non-surface patches in order to remain close to the edge by having turtles actually point only halfway to the patch of interest.

In 3D versions of this surface-walking algorithm, the same rules are used as in the 2D version, but the way to determine the angle between a turtle's current heading and the nearest opposite patch is a little more complicated. Because there are two angle measures, heading and pitch, we use the inverse dot product to determine the angle between a turtle's heading and a given patch. For more detail, it may help to look through the "Trig for Angle Calculations" section of the procedures tab or visit the "Geometric Interpretation" of the dot product entry at Wikipedia:


NUMBER-OF-TURTLES: Allows the user to adjust the number of turtles that will appear along the shape surface when SETUP is pressed.

STEP-SIZE: Allows the user to adjust how far each turtle moves forward during each step.

SURFACE-SHAPE: Allows the user to select the surface shape to appear when SETUP is pressed.

COLORED-SURFACE?: Allows user to toggle whether the surface on which turtles will walk is colored red, or is invisible.

SETUP: Sets up the environment by creating a surface and placing turtles along the surface.

GO: Runs the model by asking turtles to walk along the surface.

TRACE: Asks a random turtle to leave a trail.


Try adjusting STEP-SIZE while the model is running. What happens to the motion of the turtles? What happens to their speed?

Do turtles behave differently on different types of surfaces? Try using the SURFACE-SHAPE chooser to test different shapes. Try creating your own.

Set the SURFACE-SHAPE chooser to "Sphere", and set NUMBER-OF-TURTLES to 1. Run the model and observe the motion of that one turtle over the sphere. Is its motion what you would expect? Try using the TRACE button to trace the turtle's trajectory over the surface, or try different surfaces.


What might happen if the STEP-SIZE is set to 1 or larger?

Currently, turtles turn half of the way to the edge patch that they identify. What happens if they turn the whole way?

Select "Mickey Mouse" from the SURFACE-SHAPE chooser. SETUP and tell the model to GO. What happens to turtles when they pass over the acute angles where Mickey's ears meet his head? Try adjusting the STEP-SIZE slider while the model is running to investigate.

When does the surface-walking algorithm fail? Why? Does changing the world's resolution effect turtle motion?


Currently, turtles seek the surface edge by seeking patches that are classified in certain ways. Find and implement another way that patches can be identified or that turtles can identify edge patches.

Can there be more than two types or classifications of surface patches? When would this be useful?

Try using STAMP to trace the trajectory of a turtle over different kids of surfaces. How might one describe surface-walking accuracy?

The surface-walking algorithm used in this model fails for surfaces (or gaps in surfaces) that are only one patch wide. Why? How might this be fixed?


Note the use of TOWARDS and TOWARDS-PITCH to compute headings and pitches and MIN-ONE-OF to make a choice between competing patches.


Surface Walking (2D NetLogo model)


If you mention this model in an academic publication, we ask that you include these citations for the model itself and for the NetLogo software:

- Wilensky, U. (2007). NetLogo Surface Walking 3D model. Center for Connected Learning and Computer-Based Modeling, Northwestern University, Evanston, IL.

- Wilensky, U. (1999). NetLogo. Center for Connected Learning and Computer-Based Modeling, Northwestern University, Evanston, IL.

In other publications, please use:

- Copyright 2007 Uri Wilensky. All rights reserved. See for terms of use.


Copyright 2007 Uri Wilensky. All rights reserved.

Permission to use, modify or redistribute this model is hereby granted, provided that both of the following requirements are followed:

a) this copyright notice is included.

b) this model will not be redistributed for profit without permission from Uri Wilensky. Contact Uri Wilensky for appropriate licenses for redistribution for profit.

This is a 3D version of the 2D model Surface Walking.

Comments and Questions

3D Surface Walking (Question)

Hi, Who designed this model, please? Thanks!

Posted over 13 years ago

3D Surface Walking

Hi Jerry, I designed it a couple of years ago. Do you have any questions/comments on it?

Posted over 13 years ago

3D Surface walking (Question)

Hi Michelle, I think it's great and I would like to modify it but don't yet know how. I changed some signs and made the shape concave, but it's split things into two half globes. This is good because I want to work on a dome but don't know how to get rid of one of them. Is there a simple solution to this?

Posted over 13 years ago

3D Surface Walking

Thanks, I'm glad you might find it useful! Have you uploaded, or can you upload, the model here? If I could take a look at your code I may be able to give you some pointers.

Posted over 13 years ago

3D Surface Walking/Cornea Patch Formation (Question)

Hi Michelle, I uploaded the model now called "Cornea Patch Formation" and placed "All users may view" and "Only group can modify" for permissions. However, I think there was a glitch and now I no longer have permission to view/modify. Reuven, can you please help? Thank you, Jerry With respect to the specific question, I am interested in exploring the model but have not really devoted too much thought to it. So, maybe it's premature to ask for your help but I'm sure I will have lots of questions later. Thanks!!

Posted over 13 years ago

Error when running the 3D surface walking (Question)

Hello, my name is Alex I am a phd student. I am trying to run the 3D surface walking program but I am getting many error messages, related to variables not being declared like towards-pitch...and many can i fix this? The model must be really interesting and I thank you for allowing us to download it. hope to hear from you regards Alex

Posted about 7 years ago

Click to Run Model

patches-own [ surface? ]

;; ===========================================================================
;; ============================= Main Procedures =============================
;; ===========================================================================

to setup
  ask patches [ set surface? false ]

  if surface-shape = "Sphere"          [ draw-sphere ]
  if surface-shape = "Cube"            [ draw-cube ]
  if surface-shape = "Convex Shape"    [ draw-convex ]
  if surface-shape = "Mickey Mouse"    [ draw-mickey ]
  if surface-shape = "Hollow Cylinder" [ draw-cylinder ]

  if colored-surface?
    [ ask patches with [ surface? ] [ set pcolor gray ] ]

  ask n-of number-of-turtles patches with [ is-border-patch? ]
    sprout 1 [ set size world-width * 0.05 ]

to go
  ask turtles [
    sfd 1

to trace
  ask one-of turtles [ pen-down ]

;; ===========================================================================
;; =========================== Surface Walking ===============================
;; ===========================================================================

to sfd [ steps ] ;; turtle procedure
  repeat ( 4 * steps ) [ seek-border ]

to seek-border ;; turtle procedure
  face-closest-neighbor neighbors with [ surface? != [surface?] of myself ]
  fd step-size

;; how much turtle faces calling patch

to-report pitch-if-facing-caller [ the-patch ] ;; turtle procedure
  report ( towards-pitch the-patch + 180 ) mod 360

to-report facing-nearest [ the-turtle ] ;; patch procedure
  let difference 0
  let heading-toward [heading] of the-turtle

  let pitch-toward (towards-pitch the-turtle - 180 ) mod 360
  if not (pxcor = [xcor] of the-turtle and pycor = [ycor] of the-turtle)
    [ set heading-toward (towards the-turtle - 180 ) mod 360 ]

  set difference angle-between [heading] of the-turtle [pitch] of the-turtle heading-toward pitch-toward

  report min list difference (360 - difference)

to face-closest-neighbor [ neighbor-subset ] ;; turtle procedure
  let closest-border-patch min-one-of neighbor-subset [ facing-nearest myself ]

  ifelse abs( towards-pitch closest-border-patch - pitch ) <= 180
    set pitch ( towards-pitch closest-border-patch + pitch ) / 2
    set pitch ( towards-pitch closest-border-patch - 360 + pitch ) / 2

  if [pxcor] of closest-border-patch != xcor and [pycor] of closest-border-patch != ycor
    ifelse abs( towards closest-border-patch - heading ) <= 180
      set heading ( towards closest-border-patch + heading ) / 2
      set heading ( towards closest-border-patch - 360 + heading ) / 2

to-report is-border-patch? ;; patch procedure
  report any? neighbors with [surface? != [surface?] of myself]

;; ===========================================================================
;; =========================== Surface Drawing ===============================
;; ===========================================================================

to draw-sphere
  ask patches with [ distancexyz 0 0 0 < ( world-width * .85 / 2 ) ]
    [ set surface? true ]

to draw-cube
  ask patches with [ abs( pxcor ) < ( world-width * .75 / 2 ) and
                     abs( pycor ) < ( world-width * .75 / 2 ) and
                     abs( pzcor ) < ( world-width * .75 / 2 ) ]
    [ set surface? true ]

to draw-convex
  ask patches with [ distancexyz 0 0 (     world-width / 2 )
                       < ( world-width * .75 / 2 ) or
                     distancexyz 0 0 ( 0 - world-width / 2 )
                       < ( world-width * .75 / 2 ) ]
    [ set surface? false ]

to draw-mickey
  ask patches with [ distancexyz  0 ( 0 - world-width * .2 / 2 ) 0
                       < ( world-width * .65 / 2 ) ]
    [ set surface? true ]
  ask patches with [ distancexyz  ( world-width * .55 / 2 ) ( world-width * .45 / 2 ) 0
                       < ( world-width * .35 / 2 )  ]
    [ set surface? true ]
  ask patches with [ distancexyz  ( 0 - world-width * .55 / 2 ) ( world-width * .45 / 2 ) 0
                       < ( world-width * .35 / 2 )  ]
    [ set surface? true ]

to draw-cylinder
  ask patches with [ distancexy 0 0 < ( world-width * 0.75 / 2 ) and
                     distancexy 0 0 > ( world-width * 0.5  / 2 ) ]
    [ set surface? true ]

;; ===========================================================================
;; ====================== Trig for Angle Calculations ========================
;; ===========================================================================

to-report xvel [ angle tilt ]
  report sin( angle ) * abs( cos( tilt ) )

to-report yvel [ angle tilt ]
  report cos( angle ) * abs( cos( tilt ) )

to-report zvel [ tilt ]
  report sin tilt

to-report dot-product [ angle1 pitch1 angle2 pitch2 ]
  let x1 ( xvel angle1 pitch1 )
  let y1 ( yvel angle1 pitch1 )
  let z1 ( zvel pitch1 )

  let x2 ( xvel angle2 pitch2 )
  let y2 ( yvel angle2 pitch2 )
  let z2 ( zvel pitch2 )

  ;; to take care of slightly-over or slightly-under dot products
  ;; these break the acos used in "angle-between"
  report min ( list 1 ( max ( list -1 ( ( x1 * x2 ) + ( y1 * y2 ) + ( z1 * z2 ) ) ) ) )

to-report angle-between [ angle1 pitch1 angle2 pitch2 ]
  ifelse angle1 = angle2
    report min( list abs( pitch1 - pitch2 ) abs( 360 - ( pitch1 - pitch2 ) ) )
  ifelse pitch1 = pitch2
    report min( list abs( angle1 - angle2 ) abs( 360 - ( angle1 - angle2 ) ) )
    report acos( dot-product angle1 pitch1 angle2 pitch2 )
  ] ]

; Copyright 2007 Uri Wilensky. All rights reserved.
; The full copyright notice is in the Information tab.

There are 3 versions of this model.

Uploaded by When Description Download
Uri Wilensky over 12 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky over 12 years ago Model from NetLogo distribution Download this version
Uri Wilensky over 12 years ago Surface Walking 3D Download this version

Attached files

File Type Description Last updated
Surface Walking 3D.png preview Preview for 'Surface Walking 3D' over 9 years ago, by Uri Wilensky Download

This model does not have any ancestors.

This model does not have any descendants.