# Orbiting Bodies ### 1 collaborator Luke Elissiry (Author)

### Tags

physics

Tagged by Luke Elissiry over 9 years ago

Visible to everyone | Changeable by the author
Model was written in NetLogo 5.0.5 • Viewed 722 times • Downloaded 55 times • Run 0 times Download this modelEmbed this model

# Orbiting Bodies

## WHAT IS IT?

This model attempts to simulate an aritificial star system where the only central acting mass is the central star. 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.

## HOW IT WORKS

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 star). 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 star, 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.

## HOW TO USE IT

For a basic working model, press the SETUP button and the GO button. For different settings, adjust the following before SETUP:

• ALL OBJECTS section: all objects share these variables

• DIE-IF-TOO-CLOSE-TO-STAR? - if an object is within 5 patches of sun, it dies
• GRAVITATIONAL-CONSTANT - universal constant to control the acceleration
• ORIBT TRAIL section: for the objects's trails

• TRAIL? - if true, objects leave a trail of where they've been
• ORBIT-TRAIL-DECAY? - if trail? true and this true, objects's trails decay over time
• TIME-TILL-DECAY - if if trail? true and orbit-trail-decay? true, objects's trails decay over amount of time specified
• PLANET 1 and PLANET 2 section: variables only these objects have

• PLANET_? - turns planet on/off
• INITIAL-RADIUS_ - how far from star planet will spawn
• MASS_ - mass of planet
• INITIAL-PLANET-X-VELOCITY_ and INITIAL-PLANET-Y-VELOCITY_ - the objects initial velocity
• EXTRA-OBJECTS section: variables only the additional "asteroids" have (many asteroid variables are random such as: location, initial velocities, and color)

• EXTRA-OBJECTS - number of additional bodies
• EXTRA-OBJECTS-MASS - mass of all additional bodies

## THINGS TO NOTICE

The most important thing to observe is the behavior of the objects. Notice the objects's orbits around the star. TRAIL? on is recommended.

## THINGS TO TRY

• Add in more extra bodies
• Change masses and initial radii
• Change velocities
• Change the gravitational constant

## EXTENDING THE MODEL

• Have planets affect eachother (difficult)
• Have everything affect eachother, like the [N-Bodies] (http://modelingcommons.org/browse/onemodel/1332#modeltabsbrowseapplet) model, but try to make more usable/stable (difficult)

## NETLOGO FEATURES

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`, keeping every turtle's true coordinates as special turtle variables `xc` and `yc`, and calculations similar to the `distance` primitive but using `xc` and `yc` instead of `xcor` and `ycor`.

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

pd does not have the ability to fade, so workaround was to have the patches under object change color and have the patches own a cooldown time.

## RELATED MODELS

• [Gravitation] (http://modelingcommons.org/browse/onemodel/1330#modeltabsbrowseapplet) by Uri Wilensky - Central body is mouse, hard to see orbit, and few controls

• [N-Bodies] (http://modelingcommons.org/browse/onemodel/1332#modeltabsbrowseapplet) by Uri Wilensky - more accurate without central acting mass, but difficult to use and regular setup works for only 100 ticks

## CREDITS AND REFERENCES

Code for the orbit procedure and information was taken from Uri Wilenskys [Gravitation] (http://modelingcommons.org/browse/onemodel/1330#modeltabsbrowseapplet) model.

Click to Run Model

```breed [star]

breed [planet]

turtles-own
[ 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
mass   ;; mass of object
]

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

;--------------------------------------------
;--------------------------------------------

to setup
ca
reset-ticks
set g gravitational-constant

create-star 1 [ ; central acting mass
set size 15
set shape "sun"
set color yellow
set star-xc xcor
set star-yc ycor]

if planet1? [ ; if allowed, create planet 1
create-planet 1 [
set size 7
set shape "circle"
set color blue
set vx initial-planet-x-velocity1
set vy initial-planet-y-velocity1
set xc xcor
set yc ycor
set mass mass1]]

if planet2? [ ; if allowed, create planet 2
create-planet 1 [
set size 6
set shape "circle"
set color red
set vx initial-planet-x-velocity2
set vy initial-planet-y-velocity2
set xc xcor
set yc ycor
set mass mass2]]

if extra-objects? [ ; if allowed, create extra objects
create-planet extra-objects [
set size 3
set shape "circle"
set color 9 - random-float 6 ; grayish
setxy 0 0
fd random 100 ; like setxy random-xcor random-ycor, but limited to 100 away from sun
ifelse random 2 = 0 ; random positive/negative x-velocity (-0.5 < vx < 0.5)
[set vx random-float .5]
[set vx random-float -.5]
ifelse random 2 = 0 ; random positive/negative y-velocity (-0.5 < vy < 0.5)
[set vy random-float .5]
[set vy random-float -.5]
set xc xcor
set yc ycor
set mass extra-objects-mass]]
end

;--------------------------------------------
;--------------------------------------------

to go
if count planet = 0 [stop]; if no objects orbiting, stop
orbit
let planetcolor color
if trail? [ask patches in-radius .5 [set pcolor planetcolor set fade time-till-decay]] ; if trail? true, create line around object that can decay, unlike pd
if die-if-too-close-to-star? and any? star in-radius 5 [die] ; if too close to star and variable true, die
if ycor = -100 [print "hi"]
]
ask patches [; makes orbit trail decay if decay variable true
ifelse trail?
[if fade > 0 and orbit-trail-decay? [
set pcolor black]]]
[if pcolor != black [set pcolor black]]
]
tick
end

to orbit ;; Turtle Procedure
update-force
update-velocity
update-position
end

to update-force ;; Turtle Procedure
;; Similar to 'distancexy', except using an unbounded plane so it's possible to keep track of turtle when goes offscreen.
set r-sqrd (((xc - star-xc) * (xc - star-xc)) + ((yc - star-yc) * (yc - star-yc)))

;; prevents divide by zero
ifelse (r-sqrd != 0)
[
;; Calculate component forces using inverse square law
set fx ((cos (atan (star-yc - yc) (star-xc - xc))) * (mass / r-sqrd))
set fy ((sin (atan (star-yc - yc) (star-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
]
end

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))
end

to update-position ;; Turtle Procedure
set xc (xc + vx) ; update where turtle should go to
set yc (yc + vy)

ifelse patch-at (xc - xcor) (yc - ycor) != nobody ; if patch exists,
[
setxy xc yc ; go to patch
show-turtle
]
[ hide-turtle ] ; hide turtle but keep working
end
```

There are 2 versions of this model.