Hey Guys,
I've seen the topic of having different gravities for different bodies, or turning off gravity for specific objects come up multiple times. It doesn't really seem to make much sense to me to have box2d support gravity if a lot of people just feel the need to disable it and code gravity themselves.
If people think it's a good idea, I'd like to submit a feature request to add a Vec2 gravity multiplier to b2Body or b2BodyDef. Then if someone wants to disable gravity then can provide multipliers of (0,0), double gravity would be (2,2), reverse gravity (-1,-1), and it would default to the standard (1,1). It seems doing this would give users enough flexibility.
Thoughts?
-Dave
Without commenting on the feature itself, let me just ask, what's the purpose of having separate multipliers for the x and y elements of the gravity vector, rather than just a single scalar? In your examples the two multipliers are always the same, so in those cases at least there's really no point in duplicating them. I suppose you could exercise more 'fine-grained' control by using different multipliers for the x and y elements (e.g. set one of them to zero in order to cancel one of the elements), but that seems sort of roundabout; at that point, it seems it would be just as easy (or easier) to allow each body to have its own gravity vector. Also, gravity vectors tend to be aligned with one of the cardinal axes (IMX, at least), so I'm not sure if the 'per-element multiplier' feature would see much use.
I was just going to ask about individual gravity for different bodies, so I second this request.
The extra memory/computation cost is most-likely so small it won't be noticeable in a game if this was added to box2d.
In my game I have stuff like bullets that I don't want to be effected by gravity. I also got water in some parts of my world, and I want the objects in the water to behave with reduced gravity.
Thank you Erin for posting that code, I guess I'll use that for now.
Think about this in terms of physical simulation. In reality no bodies ever have gravity "disabled" they just provide counter forces eg; A bird hovering is constantly flapping its wings to counter the effects of gravity, providing an equal and opposite force, and the matter with objects floating in water is buoyancy countering the effects of gravity, applying a constant upwards force. I think if it was done in another way than this, there would be points where the simulation looks odd, such as if a weightless bird flew into a wall the effects wouldn't make as much sense as they would if it was simulated realistically.
There's very little reason for not just computing gravity separately for each type of gameobject. Each body might have a gameobject as a userdata. That gameobject's update method handles gravity in my game. Any gameobject whose update does not add gravity acceleration to a player's linearvelocity won't be affected by gravity. Its pretty easy.
But if people want fine grained control of gravity, they'll generally be applying the forces manually anyway.