Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Programming challenge, gravity
Message
De
24/06/2004 15:50:51
 
 
À
24/06/2004 12:55:12
Hilmar Zonneveld
Independent Consultant
Cochabamba, Bolivie
Information générale
Forum:
Visual FoxPro
Catégorie:
Autre
Divers
Thread ID:
00916106
Message ID:
00916999
Vues:
28
>>>Pretty interesting stuff. I think there's another way to do these calcs. Using Sin/Cos you can determine the x and y. You can build a table of sin/cos values in an array and get the value based on the angle between the two objects. This is supposed to be much faster than SQRT.
>>
>>Do you suppose you could post an alternate algorythm that we could compare?
>>
>>I used the Pythagorean theorem just because it was the simplest, my trig is sooooo rusty. :-)
>
>The gravitational formula should give you a force vector, consisting of a) A force as a number, b) a direction (of course, the direction of the other object attracting the object in question). (It is easiest, for internal calculations, to have all angles in radians, not degrees.)
>
>From this force vector, you can calculate the x-component as cos(angle) * force, and the y-component as sin(angle) * force (here, "force" means the scalar part of the force, that is, the magnitude, without the angle).
>
>Next, you can apply the force in the x-direction, and the force in the y-direction, separately, to calculate the acceleration. (Acceleration = Force / Mass, and Change in Position = Velocity * Time, are applicable to the individual components of the vector).

Correct me if I'm wrong here, but the Gravitational formula is G*(m1*m2)/r^2. In this case, no matter how I apply the acceleration due to gravity I still need to use the pythagorean (which needs sqrt()) to find the distance (the r in the formula).

In my hastily thrown together example I use Sqrt() twice:
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 lnResult 
where 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 
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform