Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by matthew

From the Box2D document, it sounds like gravity is set at the world level, whereas Impact defines it as the entity level. I would like to use Box2D and have some entities that honor gravity and some that do not. Is this not possible?

1 decade ago by dominic

I googled a bit - it seems to be a common problem with Box2D. In short, Box2D doesn't support "lighter than air" bodies directly, but you can apply a force to them for each frame to push them up. E.g. if you want to have entities that float, do this in your entity's update() method:
this.body.ApplyForce( new b2.Vec2(0, -ig.game.gravity * b2.SCALE), this.body.GetPosition() );

Note that when you apply a force to an entity it won't come to "rest". So having a lot of these entities will impact the performance of your game.

If you want your entities to stay fixed all the time, just skip the this.body.SetMassFromShapes(); in your createBody() method (have a look at it in lib/plugins/box2d/entity.js).

Or, if you don't need collision for these entities at all (e.g. for items that are just "collected" when touched) you can always subclass them from ig.Entity instead of from ig.Box2DEntity.

1 decade ago by matthew

The 2nd option seems to fit me, I want certain objects to stay fixed all the time. So I'll give that a shot and if it works I'll post my code here. Should just be able to add it as an option in the init.

1 decade ago by matthew

Yes, this works for me. I'm able to get around it by created a property in the Box2D entity.js file called "isGravitated" defaulted to true. Then I pass a value through the settings object and set isGravitated in the init. Then in the createBody function I do:


		if(this.isGravitated) {
			this.body.SetMassFromShapes();
		}


Works flawlessly. Thank you!

1 decade ago by Montana

Ho do you do this for a non box 2d entity, so for example i don't want my coin entity to be affected by gravity changes in the level?

thx

1 decade ago by Montana

:-D

this.gravityFactor =0;
Page 1 of 1
« first « previous next › last »