🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Gravitational acceleration calculation

Started by
143 comments, last by taby 1 year, 10 months ago

@JoeJ Thanks. I'm currently writing up a blog on this experience. It's helping me slow down and understand better what I've worked on up to this point. Once I'm done writing up my progress up to this point I think, like you suggested, I'm going to bail on the static and move on to dynamic bodies moving in both X and Y. You are right that I'm not used to working with vectors ,this is my first time (more or less)! I appreciate your willingness to help. I'll read through your examples but being so new to all this I'm struggling to follow it all. Don't be surprised if I ask a question you might have already answered above.

None

Advertisement

taby said:
Barnes-Hut exists to reduce the complexity to below O(n^2).

Yeah, looked that up and it is what i mean.

There is also the ‘Fast Multipole Method’, which i think also is a similar or the same idea.

jonny-b said:
I'll read through your examples but being so new to all this I'm struggling to follow it all.

Probably a language issue. Although, i use C++ lambdas for small functions, which can have access to variables declared earlier in the code. So now, using modern C++, we can write the same messy but comfortable spaghetti code as JavaScript guys can do. : )

You can just post your interpretation in JavaScript. I should be able to spot it if there are mistakes from translating using vectors to not using them.

@JoeJ, haha as a friend of mine once said. “Javascript gives you a noose and lets you hang yourself with it.”

Reading you code isn't the problem it's understanding the math that's going on that gets me.

None

jonny-b said:
Reading you code isn't the problem it's understanding the math that's going on that gets me.

Some points about vector math:
* It helps to distinguish points (a location in space) and vectors (a direction with a certain magnitude) at least mentally. We usually use the same data structures for both, being just called vec2 or vec3, so that's not obvious.
* The difference between two points is a vector. The magnitude of this vector gives us the distance between then, which is always positive. (There was confusion when you called the signed ‘difference’ on the x axis ‘distance’. In higher dimensions those are just easier to distinguish)

The basic operations we do on vectors:

vec2 +(or-) vec2: (a.x + b.x, a.y + b.y) // Addition: We have two lines on paper and put the second to the end point of the first (or vice versa). The new end point of the second line is the sum.
vec2 *(or/) scalar: (x * scalar, y * scalar) // By multiplying with a scalar (single number), the direction of the vector still has the same angle, but it's length changes.
Length(): // magnitude calculated using Pythagoras: square root (x*x + y*y + z*z)

Dot Product (and 3D Cross Product / perpendicular 2D direction) is also very important, but not used here.

My code really does the same math as yours, just in 3 dimensions.

@OliverWhiddles had a point there. Maybe I'll try to explain what is happening:

Imagine that someone pierced the earth right through, thus making a tunnel from one side of Earth to the other. Now, imagine you are staying on one of the entrances and drop a ball into the tunnel. What will happen?

If your answer is “it will speed up and when it passes the middle point it will slow down and finally stops on the other side, where it will be accelerated back and so again until it will return to my hands” then you will be technically correct. But I have to ask you: “why?”.

Well, the fact that the ball's acceleration towards the Earth center drops to zero as it approaches it is because the amount of Earth mass affecting the ball drops also to zero. So when the ball is in the middle there is no mass that generates gravity.

Now imagine something else: the Earth has been compressed to a zero-sized point. If you are near where Earth used to be, you will be of course affected by the gravity of the point. You will be accelerated towards that point and the amount of acceleration will be raising. As you approach the point you will be going faster and faster. If you are lucky you will pass the point at some distance and become slowed down thus effectively entering an orbit around the point. But if you are unlucky you will “touch” the point and will been “torpedoed” off it at almost infinite speed.

The point I'm trying to make (sorry for the pun) is: the problem lies in the fact your source of gravity in your game is a point. Also, no matter what an integration method you use, it will not remove this “phenomena”.

If I may suggest, to solve the problem, you have to add something that “blocks” objects from touching their centers. Try to add some collision or, as someone here suggested, try to limit what speed your particles can have by some sort of clamping method.

None

@felix.speagel , In this case that never became an issue. If you look a couple posts back I post a picture of the stars force, acceleration, velocity and position as it passes by the static object. The issue arises due to errors that occur because the delta time variable being so large in my function. This leads to errors in the acceleration being applied to my star as it passes from one side to the other side of the static object.

None

felix.speagel said:
Well, the fact that the ball's acceleration towards the Earth center drops to zero as it approaches it is because the amount of Earth mass affecting the ball drops also to zero. So when the ball is in the middle there is no mass that generates gravity.

Agree with the rest of your post, but this one feels wrong or badly phrased to me. It's the opposite: The the center, the force from gravity raises to infinity, because distance to com is zero, so we divide by zero.
We can not calculate a direction for the force, yes, but the mass and force does not vanish because of that?

The example is very nice. Usually we could say: This can't happen, due to collisions. But due to the hole, the coms of both bodies can indeed have the same location.
And even if i agree the simple point mass model lacks some details, at the center there would be no other way to be more accurate. The net com of all particles making earth would still end up at this point.
Pretty confusing : )

Actually, gravitation inside of planet is measured by multiplying by r, not dividing by r squared.

https://profoundphysics.com/gravity-underground/

It goes to zero because there is a balance in the forces.

@jonny-b Sorry I skipped over the rest of the posts not reading it carefully ;^_^ . So, if the problem was solved then I have noting more to say ?. However, if You feel there is still a problem, then maybe you should watch this video:

The guy in this video writes code in JS (with some framework added, but I think it doesn't impact understanding the code much). Just ignore the “infantile” tone of this video ?

@joej

JoeJ said:
It's the opposite: The the center, the force from gravity raises to infinity, because distance to com is zero, so we divide by zero.

You are right, but don't forget one thing: in the formula for the force of gravity there is also a mass. As a ball approaches the middle point the mass that's attracting the ball also drops to zero. In the middle point there is simply nothing that makes a gravity field.

None

This topic is closed to new replies.

Advertisement