A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: stable gravity integration?

  1. #1
    Senior Member realMakc's Avatar
    Join Date
    Oct 2002
    Posts
    923

    stable gravity integration?

    I tried smth like:

    a = -r^-2;
    v += a * dt;
    x += v * dt;

    but, major problem with this is when particles go close to each other they gain a LOT of speed.

    I tried constrain acceleration like:

    a = -Math.max (const, r)^-2

    But this does not help much. I was thinking about advanced integration like RK etc, but does it really help in this situation? Anyone with experience in this please comment.
    Last edited by realMakc; 03-07-2008 at 12:06 PM.
    who is this? a word of friendly advice: FFS stop using AS2

  2. #2
    Senior Member realMakc's Avatar
    Join Date
    Oct 2002
    Posts
    923
    Quote Originally Posted by realMakc
    major problem with this is when particles go close to each other they gain a LOT of speed
    so I thought maybe limit the speed then? smth like:

    a = -r^-2;
    v + a * dt < v_max => dt;

    I will try and come back.
    who is this? a word of friendly advice: FFS stop using AS2

  3. #3
    Senior Member realMakc's Avatar
    Join Date
    Oct 2002
    Posts
    923
    I played with foam gravity demo to see how RK4 actually behaves and, while it does extremely well, it also has it's limits. Up to source mass 1e5 it works without notable error, but higher mass starts to suck planet into tighter orbits, until ~6.1651e5 where it has bifurcation point (planet gets thrown out).

    This, one would think, can be ignored, but when you have 9000 particles interacting with two masses, chances for some of them to come close enough to masses are almost 100%

    EDIT: I just re-wrote foam Euler integrator with solution loop that looks like this:
    Code:
    override public function step( dt:Number ) : void
    {
    			
    	//get the original state
    	var state:Array = _ode.state;
    	//get the derivative based on that state
    	_ode.getDerivative( state, derivative ); 
    
    	// here is where things get different - get max d state[i]
    	var ds:Number, max_ds:Number = -1; var i:int = -1;
    	for (i = 0; i < state.length; i++)
    	{
    		ds = derivative[ i ] * dt;
    		max_ds = Math.max (max_ds, Math.abs (ds));
    	}
    
    	// force state variables to change in amounts comparable to dt
    	var N:int = Math.ceil (max_ds / Math.abs (dt));
    	for (var k:int = 0; k < N; k++)
    	{
    		// derivative
    		_ode.getDerivative( state, derivative ); 
    
    		// advance the state by the derivative
    		for (i = 0; i < state.length; i++)
    		{
    			state[ i ] += derivative[ i ] * dt / N;
    		}
    	}
    			
    }
    Its accuracy easily outperforms RK4 at mass 5e5, but the number of sub-iterations that it takes makes player freeze when orbiter get very close to mass, so this is not feasible solution. So I keep thinking... where is newblack when one needs him?
    Last edited by realMakc; 03-12-2008 at 06:36 PM.
    who is this? a word of friendly advice: FFS stop using AS2

  4. #4
    Senior Member
    Join Date
    May 2006
    Location
    Manhattan
    Posts
    246
    Your demo looks really cool.

    By "outperforms" do you mean in terms of execution speed? This augmentation should not be more accurate than an rk4 solver...

    The problem is easily identifiable in the gravitational equation (f = g * (m1m1 / r^2))- the right-hand side denominator represents the distance between the 2 objects and as it gets smaller, the force gets larger- as it approaches zero, the force nears infinity.

    I unfortunately don't have a good solution for you... A good solver can keep the illusion of orbit up much longer. Try higher initial tangential velocity for objects closer to the gravitational source.

    And if you do stick with your carefully stepping Euler, I could help you improve performance if you're interested.

  5. #5
    Senior Member realMakc's Avatar
    Join Date
    Oct 2002
    Posts
    923
    Quote Originally Posted by newblack
    By "outperforms" do you mean in terms of execution speed? This augmentation should not be more accurate than an rk4 solver...
    neverless it is, check attached swf... however, cpu freeze-ups make this impractical. I tried to do same thing with RK4 but it was even worse.

    Quote Originally Posted by newblack
    I unfortunately don't have a good solution for you...
    Arent you working with electromagnetic fields, there things should behave quite similar, no?

    Quote Originally Posted by newblack
    A good solver can keep the illusion of orbit up much longer.
    Yep, I just do not really see how would I write one... I read quite a few pages since yesterday on subject, where it says that it is possible to construct solver adapted for specific type of equation, but unfortunately I haven't found detailed "how to"-s.

    Quote Originally Posted by newblack
    And if you do stick with your carefully stepping Euler, I could help you improve performance if you're interested.
    If you think you can, please do.
    Last edited by realMakc; 08-21-2009 at 07:05 AM.
    who is this? a word of friendly advice: FFS stop using AS2

  6. #6
    Senior Member realMakc's Avatar
    Join Date
    Oct 2002
    Posts
    923
    Quote Originally Posted by realMakc
    Arent you working with electromagnetic fields
    My mistake, I was thinking about this guy.
    who is this? a word of friendly advice: FFS stop using AS2

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center