Box2D Forums

It is currently Sat May 18, 2013 11:40 am

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Mon Dec 27, 2010 8:39 am 
Offline

Joined: Thu Nov 11, 2010 8:14 am
Posts: 18
Big Stink wrote:
Hello,

Is it possible to change attributes like friction, restitution or density on the fly?


EDIT:: Just found this, seems to work fine :)
Code:
// Change property of body, in this case friction (there are other methods for other properties)
this.b2body.GetFixtureList().SetFriction(friction);


Hi,

the above doesn't work for me. I would like to change density after the creation of the bodies like this:
Code:
b2Def.fixture.friction = 0.5;
         b2Def.fixture.restitution = 1.0;
         b2Def.fixture.density = 0.2;
         b2Def.fixture.shape = new b2CircleShape();
         b2CircleShape(b2Def.fixture.shape).m_radius = 10 /Constants.RATIO;
         b2Def.body.type = b2Body.b2_dynamicBody;
         body11 = Constants._myWorld.CreateBody(b2Def.body);
         body11.CreateFixture(b2Def.fixture);
         
         b2Def.body.type = b2Body.b2_dynamicBody;
         body22 = Constants._myWorld.CreateBody(b2Def.body);
         body22.CreateFixture(b2Def.fixture);
         
         body11.SetTransform(new V2(100 / Constants.RATIO, 0 / Constants.RATIO), 0);
         body22.SetTransform(new V2(300 / Constants.RATIO, 0 / Constants.RATIO), 0);
         
         body22.GetFixtureList().SetDensity(0.9); //<-- DOESN'T WORK
         //body22.m_fixtureList.SetDensity(0.9);   //<-- DOESN'T WORK EITHER
         
         addEventListener(Event.ENTER_FRAME,goWorld);
         
         body11.ApplyForce(new V2(0,5), body11.GetWorldCenter());
         body22.ApplyForce(new V2(0,5), body22.GetWorldCenter());


The density keeps being the one defined on the creation of the bodies.
Anyone knows why or how to do it work??

Thanks you in advance and happy holidays.


Top
 Profile  
 
PostPosted: Mon Dec 27, 2010 8:53 am 
Offline

Joined: Thu Nov 11, 2010 8:14 am
Posts: 18
ok, I found out that I can destroy the fixture of a body and re-create it with a new density. However, isn't there a way to alter the density value of a current fixture of a body without destroying and re-creating it ?


Top
 Profile  
 
PostPosted: Mon Dec 27, 2010 9:02 am 
Offline

Joined: Thu Nov 11, 2010 8:14 am
Posts: 18
OK, I found that I have to recalculate the mass data after a change of density.

So after, for example, do this: body22.m_fixtureList.SetDensity(0.9);
I have also to do this: body22.ResetMassData();
in order to apply the change.


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

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users 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