Box2D stalls - multiple sets of ApplyForce

Hi, so i'm using Box2D in my game engine, and for the most part, it works just fine. I have implemented a Radial gravity function that runs through an array of b2Bodys However, as i tried to add a utility to move my player with the keyboard, it would result in the engine slowing down by a large amount. Each step takes just under one second to calculate. My movement code has almost exactly the save code as my gravity, except the angle of force applied is + or - 90 degrees from the gravity, so as to provide clockwise and counter-clockwise motion around the aforementioned array of b2Bodys. How it works in my gravity code is i loop through the array "Wrd", applying gravity to the player base on the angle and distance between the b2Body and player's b2Body, as seen below.
(Plo is the player b2Body, Mss is the array of user specified mass values, which is parallel to the Wrd array. Fce is the pull of gravity nominally. Usually done as 9.8 )

Code:
Ang = GAN ( Wrd [ i ], Plo, -180 );
Pll = -Fce * Mss [ i ] / Dst;
Plo.ApplyForce ( new b2Vec2 ( Math.sin ( Ang ) * Pll, Math.cos ( Ang ) * Pll ), Plo.GetPosition () );


This works just fine. I get a fairly nice simulation of planetary gravity, as can be seen [url="http://therelentlessvoid.deviantart.com/#/d4ins13"]HERE[/url]
However, when i implement movement, it stalls. I'm sure it has something to do with the way i'm calling ApplyForce on the player ( Also seeing as it does this twice for each planet in the game, once for gravity, and one for movement ) Movement is done like this:

( all the arrays and variables ar the same, except Mfc is substituted for Fce )

Code:
if ( Kdv.isDown ( "LEFT" ) )
{

   trace ( "LEFT Key is down" );
   
   Ang = GAN ( Wrd [ r ], Plo, -90 );
   Pll = ( Mfc / Math.sqrt ( GDTS ( Wrd [ r ], Plo ) ) ) / 60;
   
   Plo.ApplyForce( new b2Vec2 ( Math.sin ( Ang ) * Pll, Math.cos ( Ang ) * Pll ), Plo.GetPosition () );
      
}


Any insight as to what i'm doing wrong? i've been looking for a while, but haven't found anything. Thanks!
I don't see anything wrong, as you can call ApplyForce multiple times to any one body without lag. Does it still lag when you release the left arrow key, or does it only happen while you're holding the key?
The moment I start holding the Left key, it is stuck, and never gets out of it. This also happens for every other direction.
One important detail i forgot is that once this happens, the debug draw shows all the objects move to the upper left corner, so that their position is at half their width for x, and half their height for y.
Hm k. And when you do let go of an arrow key, the debug trace (i.e. "LEFT key is down") stops as well, while the game is still lagging? And the force for moving the player is applied only to the player body, nothing else, right? And when you comment out the Plo.ApplyForce lines in the keyboard input movements, it doesn't lag?

Sorry, I'm just confused about what the problem might be.
Yeah, the trace stops going, but i'm pretty sure it's still running, just extremely slowly, because i have the script timeout set to 1 second. I know that the trace utility waits for free space to record, so it's obviously not getting any time. And yes, the applyForce only happens to the player. I run this every frame before world.step ()
Commenting out Plo.applyForce solves the infinite stall. ( Although it won't very much work if i leave that out. XD )
TheVoid wrote:
Yeah, the trace stops going, but i'm pretty sure it's still running, just extremely slowly, because i have the script timeout set to 1 second. I know that the trace utility waits for free space to record, so it's obviously not getting any time. And yes, the applyForce only happens to the player. I run this every frame before world.step ()
Commenting out Plo.applyForce solves the infinite stall. ( Although it won't very much work if i leave that out. XD )

Haha x] I was just making sure that it's that particular line that's causing the infinite stall.

Is there any chance that the amount of force is extremely high, or there are some "infinity" values (i.e. dividing by zero)? There might be something that Box2D doesn't like.

Also, try flipping the Math.sin with the Math.cos, and vice versa (at least for the second bit), since cosine is usually the x value and sine the y value.

And what does the GAN function do, and what does the Wrd[r] contain (like what is it for)? That seems to be the only obvious difference between the first and the second bit (using -90 instead of -180), other than the force calculation.
Oh yeah. forgot to explain what those did.
GAN get's the angle between the two objects,
GDTS is the distance squared between the two objects.
"Wrd [ r ]" are the b2Bodies around which the device travels. "r" is the index as it loops through the objects.
These are essentially circles. I can't see any situation in which there would be a 0 division, but you never know.
( while the left/right keys are down )
I thought it was odd, but the only way i got the gravity to work was flip sin and cos from the usual way.
go figure.
The -180 provides a pull toward the planets, while -90 provides one perpendicular to the outward vector, so that it goes around the planet.
Small update:
I've tried adding a second instance of the gravity loop, It works just fine, so there must be something wrong with the math. (Considering they are almost EXACTLY the same loop, movement and gravity. ) 🙁
I guess i'm going to spend an hour debugging. Hoo-ray.
At least we can get some good info from this. 🙂

EDIT:
I'm such an idiot sometimes.
Debugging was the answer. I discovered Mfc was null, because i forgot to set it in the movement constructor.
On the bright side, we know what happens when you pass Nan to Box2D. XD

Thank you for providing help, i really appreciate it.
Ah, Mfc was null? That would definitely make Box2D stumble. x]

As for having to flip the sin and cos, it may be because you were calculating according to right-handed coordinate system (positive Y is upwards) and applying it to the screen, which is left-handed coordinate system (positive y is downwards), or vice versa (then somehow rotate the entire coordinate system 90 degrees). x]

But yeah, I'm glad you figured it out. -]

Original thread: https://box2d.org/forum/viewtopic.php?t=7957