orbits

new topic     » topic index » view thread      » older message » newer message

OK, this is for games programmers and physics majors,

I have my little orbitting planets program here that I've been messing
around with lately.  After about 4 revolutions the sun runs off and
leaves the rest of the solar system behind.  I can't find any reason for
this so any help would be appreciated.  Also, my physics may not be
quite right so any suggestions of how to make it more realistic would be
considdered as well.
Note that the little lines sticking out of the planets shows their
current speed and direction.

Sincerely,
Lewis Townsend
LewisArt

---------------------CODE------------------------
include graphics.e

constant pos = 1, dir = 2, mass = 3, kuler = 4,
        scale = 15,     -- scales view down
        X = 1, Y = 2,   -- generic x/y positions in ordered pair
        center = {320, 240}     -- center of screen

object universe, junk, n -- n is the number of the centered planet

junk = graphics_mode (18)

universe = {{{2500, 3000}, {0, 0}, 1000, YELLOW},--sets up everything
        {{500, 3000}, {0, 20}, 100, CYAN},
        {{1000, 3000}, {0, 30}, 100, GREEN},
        {{1500, 3000}, {0, 40}, 100, WHITE},
        {{3500, 3000}, {0, -40}, 100, BLUE},
        {{4000, 3000}, {0, -30}, 100, BROWN},
        {{4500, 3000}, {0, -20}, 100, MAGENTA}}

function convert (sequence c)-- converts a vector into a distance of 1
        object s        -- speed (distance of vector)
        while 1 do
            s = sqrt (c [X] * c [X] + c [Y] * c [Y]) --finds distance
--of vector
            if s > 1 then
                c = c/s         -- resets vector
            else return c
            end if
        end while
end function

function dist2d (sequence p1, sequence p2) --find 2 diminsional
--distance
    return sqrt (((p1 [1] - p2 [1]) * (p1 [1] - p2 [1])) +
        ((p1 [2] - p2 [2]) * (p1 [2] - p2 [2])))
end function

procedure delay ()      -- unoptimized delay routine
    object t            -- I don't want to include machine.e
    t = time ()
    while time () - t < .0010 do end while
end procedure

function recalc (sequence U)    -- calculates new directions and
-- positions
    object this, that, other    -- for each object effected by the
-- gravity
    for i = 1 to length (U) do  -- and positions of all the others
        this = U [i]
        other = U [1..i-1] & U [i+1..length(U)]
        for j = 1 to length (other) do
            that = other [j]
-- THESE COMENTED LINES DEAL WITH COLLISION DETECTION AND WHAT TO DO
-- WITH THE OBJECTS IF THEY COLLIDE.  UNCOMENT THEM AND THEY STICK
-- TOGETHER WHEN THEY HIT INSTEAD OF BOUNCING OFF AT THE CORRECT --
ANGLES
--          if dist2d (this [pos], that [pos]) <= this [mass]/2 + that
--[mass]/2 then
--              this [dir] = this [dir] * (this [dir] - that [dir]) / --(this
[mass]/2)
--          else
                this [dir] = this [dir] - convert(this [pos] - that [pos])
                *( that [mass] - this [mass])
                /(dist2d (this [pos], that [pos]) +.0001)
--          end if
        end for
        this [pos] = (this [pos] + this [dir])
        U [i] = this
    end for
    return U
end function

procedure drawstuff (sequence U, atom c) -- draw the objects
        for i = 1 to length (U) do
            ellipse (c * U [i] [kuler], 1, -- draw objects
((U [i] [pos]) - (U [i] [mass]/2) - (U [n] [pos])) /scale
                +center,
((U [i] [pos]) + (U [i] [mass]/2) - (U [n] [pos])) /scale
                +center)
            draw_line (c * (U [i] [kuler]+8), -- draw direction lines
                {((U [i] [pos]) - (U [n] [pos])) /scale +center,
(((U [i] [pos]) - (U [n] [pos])) /scale +center + (U [i]
                [dir]))})
        end for
end procedure
n = 1   -- begin counter is on the sun (or first object)
while 1 do
    junk = get_key ()
    if junk = 27 then exit
    elsif junk = 32 then n = n + 1
        if n > length (universe) then n = 1 end if
        --clear_screen () -- this doesn't seem to be necesary
    end if
    universe = recalc (universe) -- recalculate universe
    drawstuff (universe ,1)     -- draw the universe
    position (30,1) puts (1,"Spacebar cycles through objects and ESC
exits")
    delay ()    -- delay while the stuff is drawn
    drawstuff (universe ,0)     -- erase the universe
end while

junk = graphics_mode (-1)       -- revert back to original graphics
                                -- mode so windows can kill the window

______________________________________________________
Get Your Private, Free Email at http://www.hotmail.com

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu