Check out my
new game!

jlongster

Simulation of orbits made by gravitational pull

August 6, 2009

After implementing basic velocity and acceleration in my Scheme game, I realized that I have everything I need for making "planets" which attract every other object in the scene. Of course, in the real world, all objects are attracted to each other for some reason which I can't remember, but for most things it's rather minuscule. Planets have the most obvious affect because of their huge mass.

Here's a quick demo of my scene with planets which create orbiting objects:

Click here if you can't see the above video.

Maybe I should do some research on how objects are attracted to each other. There might be a specific equation I can implement which takes mass into account. Right now, I'm using a logarithmic algorithm to calculate the amount a of pull a planet has on an object:

(/ (log (exp 1)) (max 1.01 (log distance)))

In face, here's my whole implementation of what I call "masses," objects which attract other objects:

(define-type mass
  position)

(define masses '())

(define (add-mass mass)
  (set! masses (cons mass masses))
  (scene-list-add
   (make-scene-object mass-mesh
                      (make-vec3d 1. 1. 1.)
                      (mass-position mass)
                      (make-vec4d 0.5 0.5 0. (* (random-real) 360.))
                      (make-vec3d 3. 3. 3.))))

(define (mass-apply-gravity)
  (for-each
   (lambda (mass)
     (for-each
      (lambda (el)
        (if (scene-object-velocity el)
            (let* ((v (vec3d-sub (mass-position mass)
                                 (scene-object-position el)))
                   (d (vec3d-length v))
                   (accel (vec3d-scalar-mul
                           (vec3d-unit v)
                           (/ (log (exp 1)) (max 1.01 (log d))))))
              (scene-object-velocity-set!
               el
               (vec3d-add (scene-object-velocity el)
                          accel)))))
      scene-list))
   masses))

This could turn into a fun iPhone game.

Comments are not implemented yet. In the meanwhile, send me an email.