|
-
March 26th, 2012, 09:13 AM
#1
[RESOLVED] Trouble Creating Gravity
Hi, sorry for posting another thread. I want this one to be more specific. Also, it's on a different question than my last thread.
I'm making a physics engine for class. The GUI was provided by my professor.
I'm trying to implement gravity.
I'm doing:
Code:
case ASTEROID_ID: //m1 = SPACESHIP_MASS, m2 = ASTEROID_MASS
forceX[i] += GRAV_CONST * ( (SPACESHIP_MASS * ASTEROID_MASS) / ( sqrt( (x[i]-x[u])*(x[i]-x[u]) + (y[i]-y[u])*(y[i]-y[u]) ) ) ) * cos( atan( (y[u]-y[i]) / (x[u]-x[i]) ) );
forceY[i] += GRAV_CONST * ( (SPACESHIP_MASS * ASTEROID_MASS) / ( sqrt( (x[i]-x[u])*(x[i]-x[u]) + (y[i]-y[u])*(y[i]-y[u]) ) ) ) * sin( atan( (y[u]-y[i]) / (x[u]-x[i]) ) );
break;
Force of Gravity = 1/8300((m1*m2)/r^2)
1/8300 is an approximation of the gravitational constant
Then, I have to split that force into a force of X and a force of Y (which I later compute into a velocity of x and a velocity of y)
r is the distance between the two masses (sqrt( (x[i]-x[u])*(x[i]-x[u]) + (y[i]-y[u])*(y[i]-y[u]) )
I have to find the angle for dividing the force into forceX and forceY
So: angle = atan( (y[u]-y[i]) / (x[u]-x[i]) )
Then I have to find forceX:
cos(angle) * gravityForce = forceX
ForceY:
sin(angle) * gravityForce = forceY
The problem is that none of this works.
When I load up the program with this implemented, all objects are stuck on one point (0,0 I believe.. Which is the starting point of the spaceship)
I must be doing this completely wrong, and it's way over my head so I'm not surprised really.
Does anyone know what I might be able to try?
-
March 26th, 2012, 11:11 AM
#2
Re: Trouble Creating Gravity
If the 2 objects are located at the exact same location ... the distance between
them is zero. What does that do to your calculation ?
Care will also need to be taken in the ATAN calculation. Also, I believe ATAN
returns values between -pi/2 and pi/2 ... you might need to worry about the
quadrant that you are in.
-
March 26th, 2012, 11:34 AM
#3
Re: Trouble Creating Gravity
 Originally Posted by Philip Nicoletti
If the 2 objects are located at the exact same location ... the distance between
them is zero. What does that do to your calculation ?
Correct, assuming all the objects actually started out at that particular point with zero velocity. If, OTOH, the objects instantly "snap" to that point, I'd suspect a "using integer when it should have been floating point" kinda problem. (Just a guess, though, of course, given the sparse code provided.)
Care will also need to be taken in the ATAN calculation. Also, I believe ATAN
returns values between -pi/2 and pi/2 ... you might need to worry about the
quadrant that you are in.
Yes, and as this sort of calculation is quite common, there's the practical atan2() function to simplify that scenario.
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
March 27th, 2012, 12:18 PM
#4
Re: Trouble Creating Gravity
Thanks.. I've tried to fix my code with what you two suggested.. I'm doing something wrong, however. I hate to continue posting topics about this, but I really need the help and I want my question to be clear. (I want people to focus on the new problem)
So I'm going to post another thread showing what I'm doing, and hoping to God that someone can help
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|