Modeling stellar orbits

Started by
94 comments, last by taby 13 years, 4 months ago
Yeah, I wanna add the ability to lock on to a star at some point. I'm working on trying to get a rewind ability working so that I can aid myself in figuring out why every once in a while my physics go haywire.
Advertisement
Try using the velocity verlet instead, here in pseudocode:

// half velocity update ( old accel)Velocity += 0.5 * Accel * delta// acceleration updateAccel = Force / Mass// half velocity update (new accel)Velocity += 0.5 * Accel * delta// position updatePosition += Velocity * delta + 0.5 * Accel * delta * delta


If it still goes haywire, the error is somewhere else in your code.

Cheers,
Mike
Thanks for that h4tt3n, your 2-step velocity update is actually the same as what I had in my last code sample, but your position update is more accurate than mine, and it helped significantly actually. Orbits seem to be much more stable!

EDIT: Actually, while it seems more "stable" in the sense that things look more realistic, I'm noticing that all my stars eventually fall into the middle star now...perhaps I need to tweak my gravitational constant?

[Edited by - ChugginWindex on November 20, 2010 10:28:23 AM]
Quote:Original post by ChugginWindex
Actually, while it seems more "stable" in the sense that things look more realistic, I'm noticing that all my stars eventually fall into the middle star now...perhaps I need to tweak my gravitational constant?


Hmmm, that's strange. Your G constant probabl hasn't anything to do with that. It sounds like your system is loosing energy. Could you in pseudocode describe exactly how you loop through your stars in order to obtain a gravitational pull for each unique pair of stars. It should look something like this:

for a = 1 to number_of_stars - 1 for b = a to number_of_stars  // calculate pull bewt. stars a & b next bnext a


(look up "hand shake problem")

cheers,
Mike
Here's pseudocode for the update function of my BodySystem class, which represents the "universe":

for(bodyIter = allBodies.begin(); bodyIter != allBodies.end(); bodyIter++) {   bodyIter->update(deltaTime); //this calculates the acceleration, velocity etc. EXACTLY as you posted before h4tt3n, at the moment}//now I calculate physicsfor(bodyIter = allBodies.begin(); bodyIter != allBodies.end(); bodyIter++) {   for(bodyIter2 = allBodies.begin(); bodyIter2 != allBodies.end(); bodyIter2++) {      if(bodyIter == bodyIter2) continue; //skip itself      bodyIter->applyGravitationalPull(bodyIter2);      //check for collisions, not important to note here I think   }}


For good measure, here's my applyGravitationalPull() method inside the Body class

void Body::applyGravitationalPull(Body& otherBody) {	double forceG = (GRAVITY_CONSTANT) * otherBody.getMass() * m_mass;	vector2f dist = GetDistanceBetweenBodies(*this, otherBody);	forceG /= (pow(dist.x, 2) + pow(dist.y, 2)); //finish gravity calculation	        //normalize the distance vector	double absDistance = sqrt(pow(dist.x, 2) + pow(dist.y, 2));	dist.x /= absDistance;	dist.y /= absDistance;		//generate the final force vector	otherBody.applyImpulse(dist.x * forceG, dist.y * forceG);}
All looks ok at first glance. What exactly does the ApplyImpulse function do? Remember we're applying *forces* here, not impulse. If you've been doing so, then that would explain the sim failure.

Cheers,
Mike

[Edited by - h4tt3n on November 20, 2010 3:20:16 PM]
Quote:I'm noticing that all my stars eventually fall into the middle star now...perhaps I need to tweak my gravitational constant?


Does this happen when you only have two stars - a center one and a satellite?

Systems are often not stable. The Solar System has had a very long time to settle out, and there's still evidence of a messier early history (e.g. formation of Moon) and the planets still effect each other (Jupiter effects our orbit and climate in 100,000 year cycles).

The satellites of the Jovian system are locked in resonance with each other, so actually achieving stable orbits with many bodies is probably tricky.
ApplyImpulse is actually just bad language on my part. It's been about a year since I took Physics and I didn't feel like looking up terminology when I originally wrote that function (impulse is the change in acceleration isn't it?).

Either way, the ApplyImpulse() function actually just adds the forces that it is passed to the total X and Y forces on the object that frame. I should probably just rename it.

It's come quite a ways (technically) since when I uploaded an EXE, so here's a newer version if someone wants to take a look at the behavior that I'm talking about: Download

My apologies to all of you non-windows users, I'm Win7 only at the moment but if someone's really interested I've got no problem putting up a link to download the source as well, especially if it helps me solve this problem!

Also JoeCooper, it happens with as many stars as I put in there, whether its 2 or 20. Run the exe and you'll see for yourself.

Thanks for all the help from you guys too, I appreciate it.
I have two here - spawned mass of about 130 and 5, respectively - and it just keeps goin round and round in what appears to be a stable orbit. There's even a coresponding wobble on the parent star, which is neat. Should it take a very long time?

But I do have the previous version.

Maybe some test rig could show useful info. Autospawn two stars in what should be a stable orbit, have a monitor of the satellite's energy.
That's actually some stuff I'm currently working on. I need more information visible to me as the sim runs. But yeah you should try it with the new download I put up to see what I mean. The version you have is actually just Euler's integration method, whereas the new one uses velocity verlet integration.

EDIT: ...the wobble. The wobble should be just that, right? A wobble? In the newest version the stars don't just wobble, they end up shifting ever onward slowly in the same general direction as the "satellite" star's initial velocity when it was spawned. If I wasn't losing energy, the star would be staying in relatively the same place, correct? But just with a little wobble? Perhaps that's another sign that something is wrong...

This topic is closed to new replies.

Advertisement