Box2D Forums

It is currently Sun May 19, 2013 2:43 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Problem with gravity
PostPosted: Mon Apr 16, 2012 12:11 am 
Offline

Joined: Sun Apr 15, 2012 12:13 pm
Posts: 3
Hi everyone, i have a problem with gravity. The problem is that I do not see any acceleration of the bodies after they have gained a little speed.
example yuo can see here http://mosfgs.ru/Box2DExample. I create box2d world with the following parameters:
Code:
var world:b2World=new b2World(new b2Vec2(0,10),true);//no winds

after that i create some b2Bodies with the following parameters:
Circle 1
Code:
var somebodydef1:b2BodyDef=new b2BodyDef();
somebodydef1.active=true;
somebodydef1.type=2;//dinamic body
somebodydef1.position.Set(490,30);
var somefixturedef1:b2FixtureDef=new b2FixtureDef();
somefixturedef1.density=0.0001;
somefixturedef1.friction=3;
somefixturedef1.restitution=0.3;
somefixturedef1.shape=new b2CircleShape(50);
world.CreateBody(somebodydef1).CreateFixture(somefixturedef1);

Circle 2
Code:
var somebodydef2:b2BodyDef=new b2BodyDef();
somebodydef2.active=true;
somebodydef2.type=2;//dinamic body
somebodydef2.position.Set(480,100);
var somefixturedef2:b2FixtureDef=new b2FixtureDef();
somefixturedef2.density=100;
somefixturedef2.friction=1;
somefixturedef2.restitution=0.3;
somefixturedef2.shape=new b2CircleShape(5);
world.CreateBody(somebodydef2).CreateFixture(somefixturedef2);

Box 1
Code:
var somebodydef3:b2BodyDef=new b2BodyDef();
somebodydef3.active=true;
somebodydef3.type=2;//dinamic body
somebodydef3.position.Set(104,254);
var somefixturedef3:b2FixtureDef=new b2FixtureDef();
somefixturedef3.density=0.01;
somefixturedef3.friction=0.3;
somefixturedef3.restitution=0.3;
somefixturedef3.shape=new b2PolygonShape();
(somefixturedef3.shape as b2PolygonShape).setAsBox(50,50);
world.CreateBody(somebodydef3).CreateFixture(somefixturedef3);

Static Circle 1
Code:
var somebodydef4:b2BodyDef=new b2BodyDef();
somebodydef4.active=true;
somebodydef4.type=0;//static body
somebodydef4.position.Set(1081,808);
var somefixturedef4:b2FixtureDef=new b2FixtureDef();
somefixturedef4.density=1;
somefixturedef4.friction=1;
somefixturedef4.restitution=0.3;
somefixturedef4.shape=new b2CircleShape(500);
world.CreateBody(somebodydef4).CreateFixture(somefixturedef4);

Static Circle 2
Code:
var somebodydef5:b2BodyDef=new b2BodyDef();
somebodydef5.active=true;
somebodydef5.type=0;//static body
somebodydef5.position.Set(284,912);
var somefixturedef5:b2FixtureDef=new b2FixtureDef();
somefixturedef5.density=1;
somefixturedef5.friction=1;
somefixturedef5.restitution=0.3;
somefixturedef5.shape=new b2CircleShape(500);
world.CreateBody(somebodydef5).CreateFixture(somefixturedef5);

Static Circle 3
Code:
var somebodydef6:b2BodyDef=new b2BodyDef();
somebodydef6.active=true;
somebodydef6.type=0;//static body
somebodydef6.position.Set(-293,817);
var somefixturedef6:b2FixtureDef=new b2FixtureDef();
somefixturedef6.density=1;
somefixturedef6.friction=1;
somefixturedef6.restitution=0.3;
somefixturedef6.shape=new b2CircleShape(500);
world.CreateBody(somebodydef6).CreateFixture(somefixturedef6);


Top
 Profile  
 
 Post subject: Re: Problem with gravity
PostPosted: Mon Apr 16, 2012 12:29 am 
Offline

Joined: Sun Oct 25, 2009 3:28 am
Posts: 242
Does this happen with any shape of any density? Have you tried doing a circle with a 2.5 radius and a density of 1.0?

Seems as though you're not scaling anything. It is suggested by the Box2D manual to use a scale that the sizes of shapes of the dynamic bodies are between 0.1 and 10.0 meters (Box2D units), then just scale accordingly (for example, 30 pixels per meter. To convert pixels to meter, divide by 30. to convert meter to pixel, multiply by 30). Box2D works best if the sizes of dynamic bodies are within 0.1 and 10.0 meters.

The scaling thing may not be the actual issue, but it may help.

Are you passing through a fixed time step? And did you change anything in b2Settings.as? And what are the position and velocity iterations are you passing in the time step?


Top
 Profile  
 
 Post subject: Re: Problem with gravity
PostPosted: Mon Apr 16, 2012 11:21 pm 
Offline

Joined: Sun Apr 15, 2012 12:13 pm
Posts: 3
Thanks for the reply, it helped me a lot. Indeed scaling is necessary. Now I can see the acceleration of b2objects and their speed has become higher as I wanted. :D


Top
 Profile  
 
 Post subject: Re: Problem with gravity
PostPosted: Fri Apr 20, 2012 3:03 am 
Offline

Joined: Sat Apr 14, 2012 7:40 pm
Posts: 5
i got the same problem too, my object didn't affected by gravity..
actualy, my object still normal while it move and bouncing as usuall, until i activate fly mode, when i want it fall back, the object just move with constant speed and also when it go up, it never go down again..

here my files :
.swf file
.as file

there will be much 'junk' line of code in .as file, since i tried a lot of way to make it work..


Top
 Profile  
 
 Post subject: Re: Problem with gravity
PostPosted: Fri Apr 20, 2012 11:56 am 
Offline

Joined: Sun Oct 25, 2009 3:28 am
Posts: 242
Yours (gondai) is a different issue, but by the looks of it, you just need to reset the gravityOffset.y to 0 (zero), if you want it to fall back down again (do gravityOffset.y = 0 in the part where you turn off flying).


Top
 Profile  
 
 Post subject: Re: Problem with gravity
PostPosted: Sat Apr 21, 2012 6:34 pm 
Offline

Joined: Sat Apr 14, 2012 7:40 pm
Posts: 5
i try this
Code:
flying = false;
gravityOffset.y = 0;
body.ApplyForce(gravityOffset , body.GetWorldCenter() );

and it just stay with a small movement to up

then i add applyImpulse
Code:
flying = false;
gravityOffset.y = 0;
body.ApplyForce(gravityOffset , body.GetWorldCenter() );
body.ApplyImpulse(new b2Vec2(0, Math.abs(speed * 2)), body.GetWorldCenter() );
// speed is from linear y when flying is turn on..

my object become fall but after bouncing, it stay move up and never go down..
even the linear y is not affected by gravity, because the value is constant

tell me if i need to open new topics since i have a little different problem..
and i will post the fla file too if necessary..


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: Bing [Bot] and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group