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 )
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 )
Any insight as to what i'm doing wrong? i've been looking for a while, but haven't found anything. Thanks!
(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 () );
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 () );
}
{
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!