views:

62

answers:

1

Hello, newbie to iPhone game development and Box2D here.

I'm developing a game in which I can move (drag) otherwise stationary objects that can collide with other objects.

How do I make these objects stationary in a Box2D world with gravity while maintaining their ability to collide with dynamic bodies?

One random thought is to exert a force equal to gravity on these objects all the time. Any better or simpler approaches? Will static bodies help?

+3  A: 

Static bodies will collide with dynamic objects but not other static objects. That sounds like what you want, but it's not clear from your description what the "other bodies" in question are.

You cannot disable gravity on a per-object basis; exerting a force equal to the opposite of gravity will usually work but can, due to rounding errors, accumulate small velocities. A better approach is to set gravity to 0 and manually apply a gravitational force on the objects you do want affected by gravity.

Joe
Thanks for the answer, which makes a lot of sense. I think setting gravity on objects that require it is a cleaner approach. If static bodies cannot collide with other static bodies, that can still be a problem because I will definitely want a number of these stationary objects, which *should* collide with one another in the game (i.e. the user shouldn't be able to drag one through another). The main difference between these objects and the rest of the objects is gravity.
Delta2038