Help with making a gravity well.

Started by
18 comments, last by taby 11 years, 11 months ago
I'm trying to make a gravity well so some object with a velocity will spiral in towards the center of the well as it orbits it. What I have appears to start to work, but then the object circling the well suddenly will fly off away from the well. Here is the code I am using



Vector3 gravDir = curGravWellPos - transform.position;
gravDir = gravDir.normalized;

float dT = Time.time - startTime; //current time minus the start time
float dTSqrd = dT * dT;
float deltaY = (velocity.y * dT) + (.5f * grav * gravDir.y * dTSqrd);
float deltaX = (velocity.x * dT) + (.5f * grav * gravDir.x * dTSqrd);

transform.position = startPos + new Vector3(deltaX, deltaY, 0);


Another issue I'm having is that when calculating deltaY and deltaX, I'm pretty sure I should be using subtraction. But if I do that, the object doesn't move towards the gravity well at all.
Advertisement
Vector3 gravDir = curGravWellPos - transform.position;
should be
Vector3 gravDir = transform.position - curGravWellPos;
as you want to translate your objects so that the gravity well is at the origin, not the other way around. This might resolve your subtraction dilemma, but your equation here doesn't make much sense to me. The acceleration used in the kinematic equations should be derived from Newton's law of gravitational attraction (which takes into account the distance of the object to the gravity well). It is not a constant, but this:

F_g = G * (object.mass * well.mass) / (object-well distance)^2 where G is the gravitational constant (it can also be arbitrary if you have accuracy problems with large masses).

And you obtain the acceleration on the object by dividing F_g by the object's mass. Maybe you are already doing this as you don't show what "grav" is. You will probably also want to apply energy conservation to prevent objects slingshotting themselves and some kind of handling in case the object comes really close to the well (infinite gravitational force).

Can you give a bit more detail (what does "flies off away" mean, does it just stop responding to the well and keep going at constant velocity in a straight line or accelerates away from the well? etc...).

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

I thought you subtracted the position of the other guy from yourself to get a vector going from yourself to the other guy.

Using the projectile motion equation to get the distance traveled made sense to me. But instead of only accounting for gravity on the Y, I also added in X modified gravity's proportion in each equation by the normalized vector that points to the gravity well. But I really wasn't sure what physics equation would be best to use to do this simulation.

Getting the acceleration based on the distance the object is from the well isn't all that important to me. A constant acceleration will work just fine.

As for flying away: The thrown object starts in the top center of the screen and is thrown down and to the left. The well is in the center of the screen. For a few seconds, the object moves towards the gravity well as expected, but then it just starts moving in a straight line up and to the left.
I noticed that your version doesn't update the velocity vector at all. I think it might be better to go back to basics, often the best way when calculations don't pan out:

1. Calculation the force due to gravity, as per Bacterius's post.
2. Convert it to acceleration, using F=ma.
3. Make the acceleration into a vector, e.g. the unit vector towards the gravity source times a.
4. Update the velocity with the acceleration and time, e.g. add the acceleration vector times the time.
5. Add the velocity to the position.

You can get fancier on top of that, e.g. interpolate the acceleration from one frame to another, then interpolate the velocity based on that, then get the position. Calculus in the house. ;) But that should be a good start if you run your physics frequently.

Also note the slingshot effect if an orbiting body gets too close. If your objects lack a physical radius they can get arbitrarily close, which yields funky results. Also note that at extreme close range treating a gravitational source as a point doesn't work well, unless it is in fact a point like a black hole.
Also note the slingshot effect if an orbiting body gets too close. If your objects lack a physical radius they can get arbitrarily close, which yields funky results. Also note that at extreme close range treating a gravitational source as a point doesn't work well, unless it is in fact a point like a black hole.[/quote]
Actually black holes do have a physical radius too, called the event horizon (once an object crosses that boundary its state is in fact irrelevant as per relativity which dictates it should become unobservable), and it can be quite large. Newton's law of gravitational is mathematically sound but is simply not designed to work at arbitrary close ranges because then you need increasingly many time steps to keep the simulation stable. Fortunately naked singularities don't exist in reality (as far as we can tell). In general this isn't a problem though unless you actually start playing with n-body simulations in which a robust simulation is critical.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

Damn my loose tongue. ;) I didn't literally mean they have no radius, but compared to a sun their radius is negligible, and you would likely fall into the event horizon before the traditional gravitational effects of close range to a massive object become apparent. I doubt the OP wants to get into relativity either way.

I thought you subtracted the position of the other guy from yourself to get a vector going from yourself to the other guy.


You have it backwards.


Getting the acceleration based on the distance the object is from the well isn't all that important to me. A constant acceleration will work just fine.



Then you are not modeling a gravity well, but something else... which might explain why it doesn't behave like you expect.
Another thing worth mentioning is that a real, physically accurate gravity field cannot "catch" stray objects and make them spiral into itself. This is only possible if you add some sort of friction that dampens motion over time. Would you like to see some simple code samples (100-200 lines) of gravitational interaction?

Cheers,
Mike

I noticed that your version doesn't update the velocity vector at all. I think it might be better to go back to basics, often the best way when calculations don't pan out:

1. Calculation the force due to gravity, as per Bacterius's post.
2. Convert it to acceleration, using F=ma.
3. Make the acceleration into a vector, e.g. the unit vector towards the gravity source times a.
4. Update the velocity with the acceleration and time, e.g. add the acceleration vector times the time.
5. Add the velocity to the position.

You can get fancier on top of that, e.g. interpolate the acceleration from one frame to another, then interpolate the velocity based on that, then get the position. Calculus in the house. ;) But that should be a good start if you run your physics frequently.


I didn't update the velocity because dY = Vy0*t = .5*a*t^2 doesn't call for it. I'll try out your method of simply updating the velocity based on the acceleration instead of getting dY and see how that works out.


[quote name='homer_3' timestamp='1336532148' post='4938565']
I thought you subtracted the position of the other guy from yourself to get a vector going from yourself to the other guy.


You have it backwards.
[/quote]

Say the well is at 8,2 and the object is at 4,6.

8-4 = 4
2-6 = -4

This gives me a vector of 4,-4. Adding that vector to 4,6 (the object caught in the well) will put me at the center of the well. I don't see how subtracting my target's position from my position will give me a vector going from me to the target.


[quote name='homer_3' timestamp='1336532148' post='4938565']
Getting the acceleration based on the distance the object is from the well isn't all that important to me. A constant acceleration will work just fine.



Then you are not modeling a gravity well, but something else... which might explain why it doesn't behave like you expect.
[/quote]

I'd expect a constant acceleration to still pull the object towards the well...


Another thing worth mentioning is that a real, physically accurate gravity field cannot "catch" stray objects and make them spiral into itself. This is only possible if you add some sort of friction that dampens motion over time. Would you like to see some simple code samples (100-200 lines) of gravitational interaction?

Cheers,
Mike


So you are saying that if you were out in space and tried to throw a baseball tangent to Earth and the basball wasn't moving at escape velocity, it wouldn't fall to Earth?

So you are saying that if you were out in space and tried to throw a baseball tangent to Earth and the basball wasn't moving at escape velocity, it wouldn't fall to Earth?
[/quote]

Yup, it would make a circular or elliptic orbit around the Earth and hit you in the neck. If you went out of the way, it would stay in that orbit forever (unless influenced by other forces, like friction, gravity from other bodies ect.)

If this is for some "angy birds space" clone, then be warned that the game is very far away form beeing physically accurate.

I won't mind finding some code for you to look at if that is any help.

Cheers,
Mike

This topic is closed to new replies.

Advertisement