🎉 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

@OliverWhiddles , I'm not expecting the object to slow down as it approaches I am expecting it to slow down as it moves away from the static body. I shouldn't need any other force to accomplish this. Gravity will do the accelerating and decelerating for me. In my case the delta time variable mixed with the large values almost ensures that distance will never be exactly zero so I'll never divide by zero. I've stepped through the simulation frame by frame and it never gets close to zero. If you watch the console of the codepen I attached you can see that once the static object is passed my acceleration never bleeds away. I can see the force decreasing but the position variable gets so broken that the whole acceleration equation breaks.

@taby Due to the dt variable being like 10mil it goes from 1 lightyear on one side to 1 lightyear on the other in one frame. I wonder if that is my problem. With dt being so large I'm accelerating for almost a light year farther than I should. That could account for the massive explosion of acceleration and then the breakdown of the equation. I think I need to explore a different integration function like you suggested, but do you think that what I said above might have been the problem with the semi Implicit Euler function? edit: I didn't see your last comment so sorry if this lacks context from that.

None

Advertisement

@taby , I think you broke my brain. I don't understand any of that, lol. I'm having trouble even conceptualizing these integration functions and how they work let alone debugging them let alone applying suggestions to them. I really do appreciate your insight though. I guess this is turning out to be a huge learning opportunity for me!! I'll attempt to convert over to the RK4 like you suggested. If that doesn't work then I'll try limiting acceleration or the Lenard Nimoy proposition…. j/k.

None

@jonny-b Ah OK, sorry, I misunderstood the problem! It seems like the time step is just too large?

None

@OliverWhiddles Ya. I think that might be the problem. I don't understand it all well enough to prove that is the case. It's just my best guess. I'm hoping one of these other approaches that @taby suggested will do the trick. I'm back to reading over some articles on the subject a second time to give myself some more context before moving on. Thanks for your suggestions though!

None

jonny-b said:

@JoeJ The gravitations constant, as far as I can tell, is necessary in determining the force applied to each body by another body. So in your sVec3 f = d * (massI + massJ) / d.SquaredLength(); shouldn't the first d actually be swapped out with the gravitational constant?

Hmmm, now that you say that - i got my math just from my intention and did not look up any resources, probably.
But i have just learned you are right, so sorry for posting confusing bullshit!

Though, replacing d with G would not work. I use d to calculate the direction of the force.
Also, my addition of masses was wrong. They should be multiplied.

I'll try to fix that:

const float G = 1.f; // some number to relate your mass metric to gravity. i just assume we use a mass unit so G becoems 1.

sVec3 diff = comJ - comI;

float dist = diff.Length();
float force = G * (massI * massJ) / (dist * dist);

sVec3 direction = diff / dist;
bodyI.force += direction * force;
bodyJ.force -= direction * force;

But not sure if that's right now. Anybody please correct me if i'm still wrong.

However, my initial code gave quite nice results. I even thought i could give some interesting physics puzzle / section in a game.

@jonny-b I noticed that distance can be negative, and that goes into the power function, I think you should abs() the difference to get distance, and multiply the force by the sign of the difference. I do see it decelerating when I do this, although it still gets a mega push when it goes past, which will take an age to get back :D https://codepen.io/OliverWhiddles/pen/eYVzyom

None

@OliverWhiddles When squaring distance negative values are lost. So in the force calculation distance will always end up positive. However, I preserved the negative by storing it in a variable so I could later subtract velocity when we've passed the object.

None

Distance should never be negative.

@taby This is my simplistic way of preserving direction on a single axis example since I don't yet understand how to apply a directional force to acceleration and position (not sure I said that right). However, since the force calculation squares distance I didn't think it would be a problem for this example.

None

@joej , @taby , and @oliverwhiddles , I appreciate all of your help. This is my first post in this forum and you all have been very kinda and patient and I am very grateful. I'm used to stackoverflow where people are… not as nice.

I concluded that the issue is in fact the delta time (dt) variable in my Euler integrator. There is a bug that arises when I cross paths with the static object. In red are the forces one the “left” of the static object and green is on the “right”. In a perfect world these two forces should be equal and cancel each other out However, since they are not the negative acceleration isn't enough to cancel out the acceleration calc'd from right before and I just keep on going never really slowing down as I move away due to the inverse square law of gravity. I think I might hack the equation to detect this point and adjust the first measurement of the “left” side to be identical but opposite to the last of the “right”. @taby , I don't know enough about RK4 but would it be accurate enough to correct for this or is this something I'll have to cheat on?

None

This topic is closed to new replies.

Advertisement