
-----------------------------------
Homer_simpson
Tue Dec 02, 2008 12:42 am

Solar system phyisics
-----------------------------------
random objects appear in space with random velocities as the older ones lose orbit..
View.Set ("offscreenonly,graphics:1000;800")

const gconst := .005
function distance (x1, y1, x2, y2 : real) : real
    result sqrt (((x2 - x1) ** 2) + ((y2 - y1) ** 2))        %y

end distance
type Particle_Type :
    record
        x, y, vx, vy, w : real
    end record

const MaxParticles := 30

var Particles : array 1 .. MaxParticles of Particle_Type


procedure RenewParticle (var p : Particle_Type, x, y : int, a, r, w : real)
    p.x := x
    p.y := y
    p.vx := a
    p.vy := r
    p.w := w
end RenewParticle

for i : 1 .. MaxParticles
    RenewParticle (Particles (i), Rand.Int (1, 1000), Rand.Int (0, 1000), Rand.Int (0, 8) - 4, Rand.Int (0, 8) - 4, Rand.Int (10, 100))
end for
colorback (black)
cls


var chars : array char of boolean

var gx, gy := 400
var forceg : real
color (white)
loop
    Input.KeyDown (chars)
    if chars ('+') then
    end if
    %    put Math.Distance (gx, gy, Particles (1).x, Particles (1).y)
    %    put distance (gx, gy, Particles (1).x, Particles (1).y, false)

    for i : 1 .. MaxParticles
        if gx > Particles (i).x then
            Particles (i).vx += ((gconst) * (Particles (i).w * 10000) / (distance (gx, gy, Particles (i).x, Particles (i).y)) ** 2) / 1
        else
            Particles (i).vx -= ((gconst) * (Particles (i).w * 10000) / (distance (gx, gy, Particles (i).x, Particles (i).y)) ** 2) / 1
        end if

        if gy > Particles (i).y then
            Particles (i).vy += ((gconst) * (Particles (i).w * 10000) / (distance (gx, gy, Particles (i).x, Particles (i).y)) ** 2) / 1
        else
            Particles (i).vy -= ((gconst) * (Particles (i).w * 10000) / (distance (gx, gy, Particles (i).x, Particles (i).y)) ** 2) / 1
        end if
        if distance (gx, gy, Particles (i).x, Particles (i).y) > 1000 then
            RenewParticle (Particles (i), Rand.Int (1, 1000), Rand.Int (0, 1000), Rand.Int (0, 8) - 4, Rand.Int (0, 8) - 4, Rand.Int (10, 100))
        end if
        %put Particles (i).vx
        %put Particles (i).vy
        drawfilloval (round (Particles (i).x), round (Particles (i).y), 2, 2, black)
        drawdot (round (Particles (i).x), round (Particles (i).y), 15+round (Particles (i).w)mod 40)

        Particles (i).x += Particles (i).vx
        Particles (i).y += Particles (i).vy

        drawfilloval (round (Particles (i).x), round (Particles (i).y), 2, 2, white)
        %drawdot (round (Particles (i).x), round (Particles (i).y), white)

        %drawfillstar (round (Particles (i).x) - 5, round (Particles (i).y) - 5, round (Particles (i).x) + 5, round (Particles (i).y) + 5, white)
        %drawline (round (Particles (i).x), round (Particles (i).y), round (Particles (i).x) + round (Particles (i).v -> x) * 5, round (Particles (i).y) + round (Particles (i).v -> y) * 5, white)
        %drawfillbox (round (Particles (i).x), round (Particles (i).y), round (Particles (i).x) + round (Particles (i).v -> x) * 5, round (Particles (i).y) + round (Particles (i).v -> y) * 5, white)
        %drawfilloval (round (Particles (i).x), round (Particles (i).y), round (Particles (i).v -> x) * 3, round (Particles (i).v -> y) * 3, white)
        %drawfilloval (round (Particles (i).x), round (Particles (i).y), 1, 1, white)

    end for
    drawfilloval (gx, gy, 10, 10, 14)
    View.Update
    %cls
end loop



-----------------------------------
Nick
Tue Dec 02, 2008 7:20 am

RE:Solar system phyisics
-----------------------------------
bad:
1) the code's messy
2) there seems to be a slight glitch when the objects collide with the planet (they pick up too much momentum and zoom across the screen)
good:
1) I like the physics
improvements:
1) fix the glitch
2) add collision with the objects

-----------------------------------
SNIPERDUDE
Tue Dec 02, 2008 8:16 am

RE:Solar system phyisics
-----------------------------------
I think Nick just about covered it.

-----------------------------------
Parker
Tue Dec 02, 2008 8:29 am

RE:Solar system phyisics
-----------------------------------
I think you did quite a good job. I wish I knew the mathmatics to make a program like that :(

I did notice how they fly across the screen but was unsure if that was supposed to happen or not lol :P

-----------------------------------
[Gandalf]
Tue Dec 02, 2008 8:40 am

Re: RE:Solar system phyisics
-----------------------------------
2) there seems to be a slight glitch when the objects collide with the planet (they pick up too much momentum and zoom across the screen)
No, that's how the physics are supposed to work, it's just an extreme case of the normal orbit motion.  Well, unless the particle crashes into the object.

If you want to learn about the physics behind this, [url=http://en.wikipedia.org/wiki/Newton%27s_law_of_universal_gravitation]this Wikipedia article should have everything relevant.

-----------------------------------
Homer_simpson
Tue Dec 02, 2008 11:17 am

Re: Solar system phyisics
-----------------------------------
objects pick up momentum as they get close to the large mass because the closer the distance between the two objects the greater the gravitational force become between the objects and therefore greater acceleration...
added collision to the star to the code:
View.Set ("offscreenonly,graphics:1000;800")

const gconst := .005
function distance (x1, y1, x2, y2 : real) : real
    result sqrt (((x2 - x1) ** 2) + ((y2 - y1) ** 2)) %y

end distance
type Particle_Type :
    record
        x, y, vx, vy, w : real
    end record

const MaxParticles := 30

var Particles : array 1 .. MaxParticles of Particle_Type


procedure RenewParticle (var p : Particle_Type, x, y : int, a, r, w : real)
    p.x := x
    p.y := y
    p.vx := a
    p.vy := r
    p.w := w
end RenewParticle

for i : 1 .. MaxParticles
    RenewParticle (Particles (i), Rand.Int (1, 1000), Rand.Int (0, 1000), Rand.Int (0, 8) - 4, Rand.Int (0, 8) - 4, Rand.Int (10, 100))
end for
colorback (black)
cls


var chars : array char of boolean

var gx, gy := 400
var forceg : real
color (white)
loop

    for i : 1 .. MaxParticles
        if gx > Particles (i).x then
            Particles (i).vx += ((gconst) * (Particles (i).w * 10000) / (distance (gx, gy, Particles (i).x, Particles (i).y)) ** 2) / 1
        else
            Particles (i).vx -= ((gconst) * (Particles (i).w * 10000) / (distance (gx, gy, Particles (i).x, Particles (i).y)) ** 2) / 1
        end if

        if gy > Particles (i).y then
            Particles (i).vy += ((gconst) * (Particles (i).w * 10000) / (distance (gx, gy, Particles (i).x, Particles (i).y)) ** 2) / 1
        else
            Particles (i).vy -= ((gconst) * (Particles (i).w * 10000) / (distance (gx, gy, Particles (i).x, Particles (i).y)) ** 2) / 1
        end if
        if distance (gx, gy, Particles (i).x, Particles (i).y) > 1000 then
            RenewParticle (Particles (i), Rand.Int (1, 1000), Rand.Int (0, 1000), Rand.Int (0, 8) - 4, Rand.Int (0, 8) - 4, Rand.Int (10, 100))
        end if

        if distance (gx, gy, Particles (i).x, Particles (i).y) 