Can I dynamically set an Physics object to sensor?
mandre96

When the enemy change his animation to the 'explosion', I wanted to change his collision type to an sensor.Is there any way to do it?

 

Thanks in advance!

All 7 Comments
Gio

Hi

As the physics plug-in is a bit of a work in progress at the moment, there isn't a very nice interface to do that. However, you can do it directly through box2d by getting the fixture and calling SetSensor on it. This is possible because getFixtureList returns a raw b2dFixture object.

So something like this:

myPhysicsObject.getBehavior().getFixtureList().SetSensor(true);

 

mandre96

It worked like a charm, but it stops colliding with the ground.Is there any way from preventing it from happen?

 

Thanks in advance!

mandre96

Is there an way to SetGravityScale(0)?

Shri

Mandre96,

You might want to look into using filters for collisions, which are set on the fixture

Check out this link http://www.iforce2d.net/b2dtut/collision-filtering

Check out this link for setting the gravity scale which is set on the b2dbody http://www.iforce2d.net/b2dtut/custom-gravity

These tutorials are geared for  C++, but if you were able to figure out how to set a fixture as a sensor,  you will have no problem understanding how to  translate to javascript. As a matter of fact, if you are going to do a lot of physics stuff, then I suggest you walk through all the tutorials on this site. They will really help you understand what box2d is doing.

Lastly, if you look in the parameter setting for the PhysicsObject, one of the settings is gravity scale.

cheers,

Shri

 

mandre96

I know, but I cant figure it out how to set dynamically my Physics object gravity scale to 0 when my animation starts.The sensor Worked, but now i just wanna know if I can set the sensor and the Gravity Scale to a diferent value when the explosion animation starts.The gravity scale would be a way to prevent my object from sinking in to the ground.I know its possible, but i do not know the correct syntax..I tried :

myPhysicsObject.getBehavior().getFixtureList().SetGravityScale(0);

But it isn't the correct function.

Thanks in advance.

Shri

Mandre96,

Gravity Scale is set on the b2dbody, not the fixture.

So you would set it like you set all properties on the physics object, i.e. the b2d physics body.

So your code should be something like myPhysicsObject.getBehavior().gravityScale = 0;

cheers,

Shri

mandre96

Thank you very much for the fast reply!I'm doing an game for an university project, and your help was crucial!I now know I cant count on the community support for any further questions.

Marcos André.

 

Post a reply
Add Attachment
Submit Reply
Login to Reply