Gravitation preview image

1 collaborator

Uri_dolphin3 Uri Wilensky (Author)


chemistry and physics 

Tagged by Reuven M. Lerner about 11 years ago


Tagged by Reuven M. Lerner about 11 years ago

Model group CCL | Visible to everyone | Changeable by group members (CCL)
Model was written in NetLogo 5.0.4 • Viewed 483 times • Downloaded 65 times • Run 2 times
Download the 'Gravitation' 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 project displays the common natural phenomenon expressed by the inverse-square law. Essentially this model displays what happens when the strength of the force between two objects varies inversely with the square of the distance between these two objects.


In this model the formula used to guide each object's behavior is the standard formula for the Law of Gravitational Attraction:

(m1 * m2 * G) / r2

This is a single force 'n-body' model, where we have a certain number of small particles, and one large acting mass (the mouse pointer). The force is entirely one-way: the large mass remains unaffected by the smaller particles around it. And the smaller particles remain unaffected by each other as well. (Note that this is purely for purposes of simulation. In the real world, a force such as gravity acts on all bodies around it.)

Gravity is the best example of such a force. You can watch the particles form elliptic orbits around the mouse pointer, or watch them slingshot around it, similar to how a comet streaks past our sun. Think of the individual objects as planets or other solar bodies, and see how they react to various masses that move or remain stationary.


First select the number of particles with the NUMBER slider. Then press the SETUP button to create and scatter them across the world.

The MASS slider sets the value of the mass of the acting force. (Thus, it also determines at what distances the particles can safely orbit before they get sucked in by an overwhelming force.)

The FADE-RATE slider controls the percent of color that the paths marked by the particles fade after each cycle. Thus at 100% there won't be any paths as they fade immediately, and at 0% the paths won't fade at all. With this you can see the ellipses and parabolas formed by different particles' travels.

The COLORS? switch, when set to ON, assigns different colors to the particles, otherwise they will all be white.

When the sliders have been set to desirable levels, press the GO button to begin the simulation. Move the mouse to where you wish it to begin, and click and hold the mouse button. This will start the particles moving. If you wish to stop the simulation (say, to change the value of MASS), release the mouse button and the particles will stop moving. You may then change any settings you wish (except PARTICLES). Then, to continue the simulation, simply put your mouse in the window again and click and hold. Objects in the window will only move while the mouse button is pressed down within the window.


The most important thing to observe is the behavior of the particles. Notice that, as the particles have no initial velocity of their own, a single motionless acting mass will just pull them all in. Even a very slight mass (MASS set to a small value) will pull in all the particles. (Due to limited precision beyond a certain point, the motive-force on a particle can become zero.)

Move the mouse around - watch what happens if you move it quickly or slowly. Jiggle it around in a single place, or let it sit still. Observe what patterns the particles fall into. (Keep FADE-RATE low to watch this explicitly.)


There are a few other parameters, set in the code, that affect the behavior of the model. The force acting upon each particle is multiplied by a constant, 'g' (another global variable). Feel free to play with their values, set in the procedure 'setup'. (Of course, the default value of g for the model, 0.5, is much higher than the value used in Newtonian Mechanics, 6.67e-11.)

Initial conditions are very important for a model such as this one. Try changing how the particles are placed during the setup procedure.

Make sure to watch how different values of the MASS slider impact the model.


Let the particles begin with a constant velocity, or give them all a random velocity. You could add a slider that would let the user set the velocities, and thus be able to compare the effects of different speeds. Or try giving each particle a variable mass, which directly affects the strength of the acting force upon it.

The model assumes the force to be an attractive force (the particles tend to be pulled towards it). However, it should be a relatively easy change to make this into a repulsive force. Try setting up the model with a repulsive force, and observe what happens.


This model creates the illusion of a plane of infinite size, to better model the behavior of the particles. Notice that, with path marking you can see most of the ellipse a particle draws, even though the particle periodically shoots out of bounds. This is done through a combination of the basic turtle primitives hide-turtle and show-turtle, when a particle hits the boundary, keeping every turtle's xcor and ycor as special turtle variables xc and yc, and calculations similar to the distance primitive that uses xc and yc instead of xcor and ycor.

When you examine the code, take note that standard turtle commands like set heading, fd 1, and so on aren't used here. Everything is done directly to the x and y coordinates of the turtles.


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

  • Wilensky, U. (1998). NetLogo Gravitation model. Center for Connected Learning and Computer-Based Modeling, Northwestern Institute on Complex Systems, Northwestern University, Evanston, IL.
  • Wilensky, U. (1999). NetLogo. Center for Connected Learning and Computer-Based Modeling, Northwestern Institute on Complex Systems, Northwestern University, Evanston, IL.


Copyright 1998 Uri Wilensky.


This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License. To view a copy of this license, visit or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.

Commercial licenses are also available. To inquire about commercial licenses, please contact Uri Wilensky at

This model was created as part of the project: CONNECTED MATHEMATICS: MAKING SENSE OF COMPLEX PHENOMENA THROUGH BUILDING OBJECT-BASED PARALLEL MODELS (OBPML). The project gratefully acknowledges the support of the National Science Foundation (Applications of Advanced Technologies Program) -- grant numbers RED #9552950 and REC #9632612.

This model was converted to NetLogo as part of the projects: PARTICIPATORY SIMULATIONS: NETWORK-BASED DESIGN FOR SYSTEMS LEARNING IN CLASSROOMS and/or INTEGRATED SIMULATION AND MODELING ENVIRONMENT. The project gratefully acknowledges the support of the National Science Foundation (REPP & ROLE programs) -- grant numbers REC #9814682 and REC-0126227. Converted from StarLogoT to NetLogo, 2002.

Comments and Questions

Click to Run Model

[ fx     ;; x-component of force vector
  fy     ;; y-component of force vector
  vx     ;; x-component of velocity vector
  vy     ;; y-component of velocity vector
  xc     ;; real x-coordinate (in case particle leaves world)
  yc     ;; real y-coordinate (in case particle leaves world)
  r-sqrd ;; square of the distance to the mouse

[ m-xc  ;; x-coordinate of acting mass
  m-yc  ;; y-coordinate of acting mass
  g     ;; Gravitational Constant to slow the acceleration

;;; Setup Procedures ;;;

to setup
  set g 0.5
  set-default-shape turtles "circle"
  crt number
    if (not colors?)
    [ set color white ]
    set size 10
    fd (random-float (max-pxcor - 6))
    set vx 0
    set vy 0
    set xc xcor
    set yc ycor

;;; Runtime Procedures ;;;

to go
  if mouse-down? [
    ;; Get the mouse(mass) position.
    set m-xc mouse-xcor
    set m-yc mouse-ycor
    ask turtles [ gravitate ]

to gravitate ;; Turtle Procedure

to update-force ;; Turtle Procedure
  ;; Similar to 'distancexy', except using an unbounded plane.
  set r-sqrd (((xc - m-xc) * (xc - m-xc)) + ((yc - m-yc) * (yc - m-yc)))

  ;; prevents divide by zero
  ifelse (r-sqrd != 0)
    ;; Calculate component forces using inverse square law
    set fx ((cos (atan (m-yc - yc) (m-xc - xc))) * (mass / r-sqrd))
    set fy ((sin (atan (m-yc - yc) (m-xc - xc))) * (mass / r-sqrd))
    ;; if r-sqrd = 0, then it's at the mass, thus there's no force.
    set fx 0
    set fy 0

to update-velocity ;; Turtle Procedure
  ;; Now we update each particle's velocity, by taking the old velocity and
  ;; adding the force to it.
  set vx (vx + (fx * g))
  set vy (vy + (fy * g))

to update-position ;; Turtle Procedure
  set xc (xc + vx)
  set yc (yc + vy)

  ifelse patch-at (xc - xcor) (yc - ycor) != nobody
    setxy xc yc
    ifelse (colors?)
      if (color = white)
      [ set color 5 + 10 * random 14 ]
    [ set color white ]
    if (fade-rate != 100)
    [ ifelse (color = white)
      [ set pcolor red + 3 ]
      [ set pcolor color + 3 ]
  [ hide-turtle ]

to fade-patches
  ask patches with [pcolor != black]
  [ ifelse (fade-rate = 100)
    [ set pcolor black ]
    [ if (fade-rate != 0)
      [ fade ]

to fade ;; Patch Procedure
  let new-color pcolor - 8 * fade-rate / 100
  ;; if the new-color is no longer the same shade then it's faded to black.
  ifelse (shade-of? pcolor new-color)
  [ set pcolor new-color ]
  [ set pcolor black ]

; Copyright 1998 Uri Wilensky.
; See Info tab for full copyright and license.

There are 10 versions of this model.

Uploaded by When Description Download
Uri Wilensky about 11 years ago Updated to NetLogo 5.0.4 Download this version
Uri Wilensky over 11 years ago Updated version tag Download this version
Uri Wilensky almost 12 years ago Updated to version from NetLogo 5.0.3 distribution Download this version
Uri Wilensky over 12 years ago Updated to NetLogo 5.0 Download this version
Uri Wilensky about 14 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky about 14 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky about 14 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky about 14 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky about 14 years ago Model from NetLogo distribution Download this version
Uri Wilensky about 14 years ago Gravitation Download this version

Attached files

File Type Description Last updated
Gravitation.png preview Preview for 'Gravitation' over 11 years ago, by Uri Wilensky Download

This model does not have any ancestors.

This model does not have any descendants.