The applet requires Java 5 or higher. Java must be enabled in your browser settings. Mac users must have Mac OS X 10.4 or higher. Windows and Linux users may obtain the latest Java from Oracle's Java site.


powered by NetLogo

view/download model file: ball4.nlogo

WHAT IS IT?

This Demo is an example of the 3-Body problem. Any 3 movable, mutually attracted bodies will end up with unpredictable positions and velocities after a number of iterations, or delay of time.

HOW IT WORKS

This demo consists of basic math for the computing of the distance between each set of balls, the force between each set of balls because of gravity over that distance, the change of the velocity vector due to that force, and the change in Position due to the change in velocity vector.

HOW TO USE IT

A slider allows changing the value of mass of each ball.
Another slider allows the changing of number of iterations.
The “setup” button puts the three balls in fixed positions.
The “go” Button starts the movement of the balls.

THINGS TO NOTICE

The Red vector line shows the direction and quantity of the total force vector.
The Green vector line shows the direction and quantity of the velocity vector.
Notice how after about 800 iterations the balls are positioned in a random location, with random vectors.

THINGS TO TRY

Varying the mass of the balls results in greater inertia.

EXTENDING THE MODEL

This model can be extended to allow random initial positioning of the balls, which shows the effects of gravity and mass on objects, but makes it hard to see the random positions and vectors that result from a 3-Body problem.

CREDITS AND REFERENCES

Completed 2012/02/19 by Kgwedi Wise

CODE

;;Simulate a field of objects that are attracted to each other
;;ball1 - gets distance between balls
;;balls2 - computes total gravity vector force between all the  5 balls 
;; balls3 - Starts all over with  balls and no list no loops. red force vectors
;; balls4 - adds a velocity vector. OK BUT..sometimes The total energy of the system seems to grow. This one works

breed [balls ball]
breed [link-forces link-force]
breed [link-velocitys link-velocity]

balls-own
[  vx     ;; x-component of velocity vector
  vy     ;; y-component of velocity vector
  dist0
  dist1
  dist2
  forcex0
  forcex1
  forcex2
  forcey0
  forcey1
  forcey2
]


globals
[ g ;;gravity constant
  temp
  num-balls
  ]

to setup
  ;; (for this model to work with NetLogo's new plotting features,
  ;; __clear-all-and-reset-ticks should be replaced with clear-all at
  ;; the beginning of your setup procedure and reset-ticks at the end
  ;; of the procedure.)
  __clear-all-and-reset-ticks 
  set num-balls 3
  set g .1
  set-default-shape turtles "circle"
  create-balls num-balls
 

  [
    set size 5
  set vx 0
  set vy 0
  set color yellow
 
   ask ball 0 [
    set xcor -25
    set ycor -38
  ]
  ask ball 1 [
    set xcor 35
    set ycor 0
  ]
   ask ball 2 [
    set xcor 45
    set ycor -35
  ]
  ]
   create-link-forces num-balls
   create-link-velocitys num-balls
end

to go
get-dist
tick
if ticks  = Time-to-Run [ stop ]
end

;;========================================

to get-dist
  
ask balls[

        if who = 0 [
          set dist1 sqrt ((( pxcor - [pxcor] of ball 1) ^ 2 ) + ((pycor - [pycor] of ball 1) ^ 2 ))
            ifelse dist1 != 0 [
               set temp atan([pycor] of ball 1 - pycor)([pxcor] of ball 1 - pxcor) 
               set forcex1 ((cos(temp))*(mass / dist1))
               set forcey1 ((sin(temp))*(mass / dist1))
            ]
            [ set forcex1 0
              set forcey1 0
         ;     set vx (vx + [vx] of ball 1 )
         ;     set vy (vy + [vy] of ball 1 )
         ;      ask ball 1 [
         ;       set vx (vx + [vx] of ball 0 )
         ;       set vy (vy + [vy] of ball 0 )   
         ;    ]
            ]
           set dist2 sqrt ((( pxcor - [pxcor] of ball 2) ^ 2 ) + ((pycor - [pycor] of ball 2) ^ 2 ))
           ifelse dist2 != 0 [
                set temp atan([pycor] of ball 2 - pycor)([pxcor] of ball 2 - pxcor)
                set forcex2 ((cos(temp))*(mass / dist2))
                set forcey2 ((sin(temp))*(mass / dist2))
            ]
            [ set forcex2 0
              set forcey2 0
         ;     set vx (vx + [vx] of ball 2 )
         ;     set vy (vy + [vy] of ball 2 )
         ;      ask ball 2 [
         ;       set vx (vx + [vx] of ball 0 )
         ;       set vy (vy + [vy] of ball 0 )   
         ;    ]
            ]
         set vx (vx + ((forcex1 + forcex2 ) * g ))
         set vy (vy + ((forcey1 + forcey2 ) * g ))
         setxy (pxcor + vx) (pycor + vy)
         
         ask link-force 3 [
            setxy (( [xcor] of ball 0) + ((([forcex1] of ball 0) * g ) * 1000 ) + ((([forcex2] of ball 0) * g ) * 1000 ))  (([ycor] of ball 0) + ((([forcey1] of ball 0) * g ) * 1000 ) + ((([forcey2] of ball 0) * g ) * 1000 ))
            create-link-to ball 0 [ set color red ] ]
        ask link-velocity 6 [
             setxy ((([xcor] of ball 0) + ([vx] of ball 0) * 30)) ((([ycor] of ball 0) + ([vy] of ball 0) * 30))
         create-link-to ball 0 [ set color green ] ]
         
        ] ;;end of if who = 0
        
        
        if who = 1 [
          set dist0 sqrt ((( pxcor - [pxcor] of ball 0) ^ 2 ) + ((pycor - [pycor] of ball 0) ^ 2 ))
            ifelse dist0 != 0 [
               set temp atan([pycor] of ball 0 - pycor)([pxcor] of ball 0 - pxcor) 
               set forcex0 ((cos(temp))*(mass / dist0))
               set forcey0 ((sin(temp))*(mass / dist0))
            ]
            [
             set forcex0 0
             set forcey0 0 
        ;     set vx (vx + [vx] of ball 0 )
        ;     set vy (vy + [vy] of ball 0 )
        ;      ask ball 0 [
        ;        set vx (vx + [vx] of ball 1 )
        ;        set vy (vy + [vy] of ball 1 )   
       ;      ]
            ]
          set dist2 sqrt ((( pxcor - [pxcor] of ball 2) ^ 2 ) + ((pycor - [pycor] of ball 2) ^ 2 ))
            ifelse dist2 != 0 [
                set temp atan([pycor] of ball 2 - pycor)([pxcor] of ball 2 - pxcor)
                set forcex2 ((cos(temp))*(mass / dist2))
                set forcey2 ((sin(temp))*(mass / dist2))
            ]
            [ set forcex2 0
              set forcey2 0
         ;     set vx (vx + [vx] of ball 2 )
         ;    set vy (vy + [vy] of ball 2 )
         ;     ask ball 2 [
         ;       set vx (vx + [vx] of ball 1 )
         ;       set vy (vy + [vy] of ball 1 )   
         ;    ]
            ]
         
          set vx (vx + ((forcex0 + forcex2 ) * g ))
          set vy (vy + ((forcey0 + forcey2 ) * g ))
          setxy (pxcor + vx) (pycor + vy)
           ask link-force 4 [
            setxy (( [xcor] of ball 1) + ((([forcex0] of ball 1) * g ) * 1000 ) + ((([forcex2] of ball 1) * g ) * 1000 ))  (([ycor] of ball 1) + ((([forcey0] of ball 1) * g ) * 1000 ) + ((([forcey2] of ball 1) * g ) * 1000 ))
            create-link-to ball 1  [ set color red ] ]
           ask link-velocity 7 [
             setxy ((([xcor] of ball 1) + ([vx] of ball 1) * 30)) ((([ycor] of ball 1) + ([vy] of ball 1) * 30))
         create-link-to ball 1 [ set color green ] ]
        ] ;; end of if who = 1
        
       
        if who = 2 [
          set dist1 sqrt ((( pxcor - [pxcor] of ball 1) ^ 2 ) + ((pycor - [pycor] of ball 1) ^ 2 ))
             ifelse dist1 != 0 [
               set temp atan([pycor] of ball 1 - pycor)([pxcor] of ball 1 - pxcor) 
               set forcex1 ((cos(temp))*(mass / dist1)) 
               set forcey1 ((sin(temp))*(mass / dist1))
            ]
            [
             set forcex1 0
             set forcey1 0
         ;    set vx (vx + [vx] of ball 1 )
        ;     set vy (vy + [vy] of ball 1 )
         ;    ask ball 1 [
         ;       set vx (vx + [vx] of ball 2 )
         ;       set vy (vy + [vy] of ball 2 )   
         ;    ]
            ] 
            
          set dist0 sqrt ((( pxcor - [pxcor] of ball 0) ^ 2 ) + ((pycor - [pycor] of ball 0) ^ 2 ))
              ifelse dist0 != 0 [
                  set temp atan([pycor] of ball 0 - pycor)([pxcor] of ball 0 - pxcor)
                  set forcex0 ((cos(temp))*(mass / dist0))
                  set forcey0 ((sin(temp))*(mass / dist0))
            ]
            [
             set forcex0 0
             set forcey0 0 
         ;    set vx (vx + [vx] of ball 0 )
         ;    set vy (vy + [vy] of ball 0 )
         ;     ask ball 0 [
         ;       set vx (vx + [vx] of ball 2 )
         ;       set vy (vy + [vy] of ball 2 )   
         ;    ]
            ]
           set vx (vx + ((forcex1 + forcex0 ) * g ))
           set vy (vy + ((forcey1 + forcey0 ) * g ))
           setxy (pxcor + vx) (pycor + vy)
             ask link-force 5 [
            setxy (( [xcor] of ball 2) + ((([forcex1] of ball 2) * g ) * 1000 ) + ((([forcex0] of ball 2) * g ) * 1000 ))  (([ycor] of ball 2) + ((([forcey1] of ball 2) * g ) * 1000 ) + ((([forcey0] of ball 2) * g ) * 1000 ))
            create-link-to ball 2  [ set color red ] ]
             ask link-velocity 8 [
             setxy ((([xcor] of ball 2) + ([vx] of ball 2) * 30)) ((([ycor] of ball 2) + ([vy] of ball 2) * 30))
         create-link-to ball 2 [ set color green ] ]
        ];; end of if who = 2
       
  ]
end