FUNCTION GravitationalAcceleration * Adjust the acceleration of every object has on every other object LOCAL lo1, lo2, lng, ; lx, ly, ld, ; lxp, lyp FOR EACH lo1 IN aBodies IF lo1.lSkipGravity LOOP ENDIF FOR EACH lo2 IN aBodies IF lo1 = lo2 LOOP ENDIF * First, find out the acceleration due to gravitation * between these two objects lng = Newton(lo1, lo2) * Now apply the acceleration to the second body DO CASE * lo2 is below lo1 CASE lo1.X = lo2.X AND lo1.Y > lo2.Y lo2.SpeedY = lo2.SpeedY + lng * lo2 is above lo1 CASE lo1.X = lo2.X AND lo1.Y < lo2.Y lo2.SpeedY = lo2.SpeedY - lng * lo2 is left of lo1 CASE lo1.Y = lo2.Y AND lo1.X > lo2.X lo2.SpeedX = lo2.SpeedX + lng * lo2 is right of lo1 CASE lo1.Y = lo2.Y AND lo1.X < lo2.X lo2.SpeedX = lo2.SpeedX - lng *Otherwise, we're at an angle OTHERWISE * Step 1, find out the distance between o1 and o2 lx = lo1.X - lo2.X ly = lo1.Y - lo2.Y ld = SQRT(lx^2 + ly^2) * Step 2, find the legs of the new triangle lxp = lx * (lng / ld) lyp = ly * (lng / ld) lo2.SpeedX = lo2.SpeedX + lxp lo2.SpeedY = lo2.SpeedY + lyp ENDCASE ENDFOR ENDFOR RETURN FUNCTION Newton LPARAMETERS to1, to2 LOCAL lnResult * This should calculate the force of gravity based on G * (m1*m2)/r^2 * But for the apple/earth example we know the accelation is 4.9m/s/s lx = to1.X - to2.X ly = to1.Y - to2.Y ld = SQRT(lx^2 + ly^2) lnResult= 6.67300 * 10^-11 * ((to1.Mass * to2.Mass)/ld^2) *WAIT WINDOW TRANSFORM(lnResult) RETURN lnResultwhere its pretty obvious I could get by only using it once. I can also remove all CASEs except for the otherwise, since the otherwise handles the CASEs :-) like so:
FUNCTION GravitationalAcceleration * Adjust the acceleration of every object has on every other object LOCAL lo1, lo2, lng, ; lx, ly, ld, ; lxp, lyp FOR EACH lo1 IN aBodies IF lo1.lSkipGravity LOOP ENDIF FOR EACH lo2 IN aBodies IF lo1 = lo2 LOOP ENDIF * Step 1, find out the distance between o1 and o2 and * the acceleration due to gravitation * between these two objects lx = lo1.X - lo2.X ly = lo1.Y - lo2.Y ld = SQRT(lx^2 + ly^2) lng = Newton2(lo1.Mass, lo2.Mass, ld) * Now apply the acceleration to the second body * Step 2, find the legs of the new triangle lxp = lx * (lng / ld) lyp = ly * (lng / ld) lo2.SpeedX = lo2.SpeedX + lxp lo2.SpeedY = lo2.SpeedY + lyp ENDFOR ENDFOR RETURN FUNCTION Newton2 LPARAMETERS tn1, tn2, tnd LOCAL lnResult * This should calculate the force of gravity based on G * (m1*m2)/r^2 lnResult= 6.67300 * 10^-11 * ((tn1 * tn2)/tnd^2) RETURN lnResult